Export (0) Print
Expand All

FileInfo.IsReadOnly Property

Note: This property is new in the .NET Framework version 2.0.

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 */
public boolean get_IsReadOnly ()

/** @property */
public void set_IsReadOnly (boolean value)

public function get IsReadOnly () : boolean

public function set IsReadOnly (value : boolean)

Property Value

true if the current file is read only; otherwise, false.

Exception typeCondition

FileNotFoundException

The file described by the current FileInfo object could not be found.

IOException

An I/O error occurred while opening the file.

UnauthorizedAccessException

The file described by the current FileInfo object is read-only.

-or-

This operation is not supported on the current platform.

-or-

The caller does not have the required permission.

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

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 = "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);

            Console.ReadLine();

        }

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

        }

    }
}

  • FileIOPermission  Associated enumerations: Read, Write Security action: Demand. For permission to read and write to the file described by the current FileInfo object.

Windows 98, Windows 2000 SP4, Windows CE, Windows Millennium Edition, Windows Mobile for Pocket PC, Windows Mobile for Smartphone, Windows Server 2003, Windows XP Media Center Edition, Windows XP Professional x64 Edition, Windows XP SP2, Windows XP Starter Edition

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

.NET Framework

Supported in: 2.0

Community Additions

ADD
Show:
© 2014 Microsoft