Windows apps
Collapse the table of content
Expand the table of content
Information
The topic you requested is included in another documentation set. For convenience, it's displayed below. Choose Switch to see the topic in its original location.

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:
property bool IsReadOnly {
	bool get();
	void set(bool value);
}

Property Value

Type: System::Boolean

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

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.

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 namespace System;
using namespace System::IO;

namespace FileSystemExample
{
    // Sets the read-only value of a file.
    void SetFileReadAccess(String^ fileName, bool setReadOnly)
    {
        // Create a new FileInfo object.
        FileInfo^ fInfo = gcnew FileInfo(fileName);

        // Set the IsReadOnly property.
        fInfo->IsReadOnly = setReadOnly;
    }

    // Returns whether a file is read-only.
    bool IsFileReadOnly(String^ fileName)
    {
        // Create a new FileInfo object.
        FileInfo^ fInfo = gcnew FileInfo(fileName);

        // Return the IsReadOnly property value.
        return fInfo->IsReadOnly;
    }
}

int main()
{
    try
    {
		String^ fileName = "c:\\test.xml";

        if (File::Exists(fileName))
        {
            // Get the read-only value for a file.
            bool isReadOnly = FileSystemExample::IsFileReadOnly(fileName);

            // Display whether the file is read-only.
            Console::WriteLine("The file read-only value for {0} is:" +
                "{1}", fileName, isReadOnly);

            Console::WriteLine("Changing the read-only value for {0}" +
                " to true.", fileName);

            // Set the file to read-only.
            FileSystemExample::SetFileReadAccess(fileName, true);

            // Get the read-only value for a file.
            isReadOnly = FileSystemExample::IsFileReadOnly(fileName);

            // Display that the file is read-only.
            Console::WriteLine("The file read-only value for {0} is:" +
                "{1}", fileName, isReadOnly);
        }
        else
        {
            Console::WriteLine("The file {0} doesn't exist.", fileName);
        }
    }
    catch (IOException^ ex)
    {
        Console::WriteLine(ex->Message);
    }
};
//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:False
//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.

Universal Windows Platform
Available since 10
.NET Framework
Available since 2.0
Return to top
Show:
© 2017 Microsoft