Wednesday, November 10, 2010

Tip: How to remove read-only attribute of a file?

Below are the helper methods used to set or remove read-only attribute of a file. As well as I've provided a method to check the read-only attribute of a file.

       /// <summary>
       /// Sets the passed file as readonly.
       /// </summary>
       /// <param name="filePath">The fully qualified name of a file, or the relative file name.</param>
       public static void SetReadOnly(string filePath)
       {
          SetReadOnly(filePath, true);
       }

       /// <summary>
       /// Sets the passed file to either readonly or not.
       /// </summary>
       /// <param name="filePath">The fully qualified name of a file, or the relative file name.</param>
       /// <param name="readOnly">true to make the file as readonly; otherwise, false.</param>
       public static void SetReadOnly(string filePath, bool readOnly)
       {
          if (String.IsNullOrEmpty(filePath))
              throw new ArgumentNullException("FilePath");

          if (!File.Exists(filePath)) return;
          FileInfo myFile = new FileInfo(filePath);
          myFile.IsReadOnly = readOnly;
       }

       /// <summary>
       /// Gets a value that determines if the passed file is read only.
       /// </summary>
       /// <param name="filePath">The fully qualified name of a file, or the relative file name.</param>
       /// <returns>true if the current file is read only; otherwise, false.</returns>
       public static bool IsReadOnly(string filePath)
       {
          if (String.IsNullOrEmpty(filePath))
              throw new ArgumentNullException("FilePath");

          if (!File.Exists(filePath))
              throw new ArgumentException(String.Format("'{0}' file not exists.", filePath));

          FileInfo myFile = new FileInfo(filePath);
          return myFile.IsReadOnly;
       }

Wednesday, November 3, 2010

Tip: How to get Relative Path in .NET

In .NET, there is no straight method available to find a relative path either in Path class or other classes. Here is the utility method that will help to get you the relative path

       /// <summary>
       /// Get relative path for a given file path
       /// </summary>
       /// <param name="baseDirectory">Base directory</param>
       /// <param name="filePath">A file path</param>
       /// <returns>A relative path location</returns>
       public static string GetRelativePath(string baseDirectory, string filePath)
       {
          if (String.IsNullOrEmpty(baseDirectory))
              throw new ArgumentNullException("BaseDirectory");
          if (String.IsNullOrEmpty(filePath))
              throw new ArgumentNullException("FilePath");

          if (!baseDirectory.EndsWith("\\"))
              baseDirectory += "\\";

          System.Uri uri1 = new Uri(filePath);
          System.Uri uri2 = new Uri(baseDirectory);

          Uri relativeUri = uri2.MakeRelativeUri(uri1);

          return relativeUri.ToString();
       }