Export (0) Print
Expand All

FileInfo.IsReadOnly Property

Gets or sets a value that determines if the current file is read only.

Namespace:  System.IO
Assembly:  mscorlib (in mscorlib.dll)

public bool IsReadOnly { get; set; }

Property Value

Type: System.Boolean
true if the current file is read only; otherwise, false.

ExceptionCondition
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.

Use the IsReadOnly property to quickly determine or change whether the current file is read only.

When first called, FileInfo calls Refresh and caches information about the file. On subsequent calls, you must call Refresh to get the latest copy of the information.

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 
//

.NET Framework

Supported in: 4.5.3, 4, 3.5, 3.0, 2.0

.NET Framework Client Profile

Supported in: 4, 3.5 SP1

  • FileIOPermission 

    Associated enumerations: Read, Write

    Security action: Demand.

    For permission to read and write to the file described by the current FileInfo object.

Windows 8.1, Windows Server 2012 R2, Windows 8, Windows Server 2012, Windows 7, Windows Vista SP2, Windows Server 2008 (Server Core Role not supported), Windows Server 2008 R2 (Server Core Role supported with SP1 or later; Itanium not supported)

The .NET Framework does not support all versions of every platform. For a list of the supported versions, see .NET Framework System Requirements.

Show:
© 2014 Microsoft