FileInfo.IsReadOnly Property
Updated: January 2012
Gets or sets a value that determines if the current file is read only.
Assembly: mscorlib (in mscorlib.dll)
| Exception | Condition |
|---|---|
| FileNotFoundException |
The file described by the current FileInfo object could not be found. |
| IOException |
An I/O error occurred while opening the file. |
| UnauthorizedAccessException |
This operation is not supported on the current platform. -or- The caller does not have the required permission. |
| ArgumentException |
The user does not have write permission, but attempted to set this property to false. |
The following example uses the IsReadOnly property to mark a file as read only and then mark it as read-write.
using System; using System.IO; namespace FileSystemExample { class FileExample { public static void Main() { string FileName = @"c:\test.xml"; // Get the read-only value for a file. bool isReadOnly = IsFileReadOnly(FileName); // Display wether the file is read-only. Console.WriteLine("The file read-only value for " + FileName + " is: " + isReadOnly); Console.WriteLine("Changing the read-only value for " + FileName + " to true."); // Set the file to read-only. SetFileReadAccess(FileName, true); // Get the read-only value for a file. isReadOnly = IsFileReadOnly(FileName); // Display that the file is read-only. Console.WriteLine("The file read-only value for " + FileName + " is: " + isReadOnly); } // Sets the read-only value of a file. public static void SetFileReadAccess(string FileName, bool SetReadOnly) { // Create a new FileInfo object. FileInfo fInfo = new FileInfo(FileName); // Set the IsReadOnly property. fInfo.IsReadOnly = SetReadOnly; } // Returns wether a file is read-only. public static bool IsFileReadOnly(string FileName) { // Create a new FileInfo object. FileInfo fInfo = new FileInfo(FileName); // Return the IsReadOnly property value. return fInfo.IsReadOnly; } } } //This code produces output similar to the following; //results may vary based on the computer/file structure/etc.: // //The file read-only value for c:\test.xml is: True //Changing the read-only value for c:\test.xml to true. //The file read-only value for c:\test.xml is: True //
-
FileIOPermission
Associated enumerations: Read, Write
Security action: Demand.
For permission to read and write to the file described by the current FileInfo object.
Windows 7, Windows Vista SP1 or later, Windows XP SP3, Windows XP SP2 x64 Edition, Windows Server 2008 (Server Core not supported), Windows Server 2008 R2 (Server Core supported with SP1 or later), Windows Server 2003 SP2
The .NET Framework does not support all versions of every platform. For a list of the supported versions, see .NET Framework System Requirements.
- 9/13/2010
- Billy Bob Richert