FileInfo.IsReadOnly プロパティ

定義

現在のファイルが読み取り専用であるかどうかを判断する値を取得または設定します。

public:
 property bool IsReadOnly { bool get(); void set(bool value); };
public bool IsReadOnly { get; set; }
member this.IsReadOnly : bool with get, set
Public Property IsReadOnly As Boolean

プロパティ値

true 現在のファイルが読み取り専用であるか、見つからなかった場合は 。それ以外の場合は false

例外

IsReadOnly プロパティを設定しようとしたときに、現在 FileInfo のオブジェクトによって記述されたファイルが見つかりませんでした。 (存在しないファイルに対してこのプロパティを取得すると、例外はスローされず、代わりに が返 trueされることに注意してください)。

ファイルを開くときに、I/O エラーが発生しました。

この操作は、現在のプラットフォームではサポートされていません。

または

呼び出し元に、必要なアクセス許可がありません。

ユーザーには書き込みのアクセス許可がありませんが、このプロパティを false に設定しようとしました。

次の例では、一時ファイルを作成し、 プロパティをIsReadOnly使用して最初にチェックし、読み取り専用の状態を設定し、最後に読み取り/書き込みとしてマークして削除します。

using namespace System;
using namespace System::IO;

int main()
{
    // Create a temporary file.
    String^ filePath = Path::GetTempFileName();
    Console::WriteLine("Created a temp file at '{0}.", filePath);

    // Create a new FileInfo object.
    FileInfo^ fInfo = gcnew FileInfo(filePath);
    
    // Get the read-only value for a file.
    bool isReadOnly = fInfo->IsReadOnly;

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

    // Set the file to read-only.
    Console::WriteLine("Setting the read-only value for '{0}' to true.", fInfo->Name);
    fInfo->IsReadOnly = true;

    // Get the read-only value for a file.
    isReadOnly = fInfo->IsReadOnly;

    // Display that the file is now read-only.
    Console::WriteLine("The file read-only value for '{0}' is {1}.", fInfo->Name, isReadOnly);

    // Make the file mutable again so it can be deleted.
    fInfo->IsReadOnly = false;

    // Delete the temporary file.
    fInfo->Delete();

    return 0;
};

// This code produces output similar to the following,
// though results may vary based on the computer, file structure, etc.:
//
// Created a temp file at 'C:\Users\UserName\AppData\Local\Temp\tmpB438.tmp'.
// The file read-only value for 'tmpB438.tmp' is False.
// Setting the read-only value for 'tmpB438.tmp' to true.
// The file read-only value for 'tmpB438.tmp' is True.
//
using System;
using System.IO;

public class FileExample
{
    public static void Main()
    {
        // Create a temporary file.
        string filePath = Path.GetTempFileName();
        Console.WriteLine($"Created a temp file at '{filePath}'.");

        // Create a new FileInfo object.
        FileInfo fInfo = new FileInfo(filePath);

        // Get the read-only value for a file.
        bool isReadOnly = fInfo.IsReadOnly;

        // Display whether the file is read-only.
        Console.WriteLine($"The file read-only value for '{fInfo.Name}' is {isReadOnly}.");

        // Set the file to read-only.        
        Console.WriteLine($"Setting the read-only value for '{fInfo.Name}' to true.");
        fInfo.IsReadOnly = true;

        // Get the read-only value for a file.
        isReadOnly = fInfo.IsReadOnly;

        // Display that the file is now read-only.
        Console.WriteLine($"The file read-only value for '{fInfo.Name}' is {isReadOnly}.");

        // Make the file mutable again so it can be deleted.
        fInfo.IsReadOnly = false;

        // Delete the temporary file.
        fInfo.Delete();
    }
}

// This code produces output similar to the following,
// though results may vary based on the computer, file structure, etc.:
//
// Created a temp file at 'C:\Users\UserName\AppData\Local\Temp\tmpB438.tmp'.
// The file read-only value for 'tmpB438.tmp' is False.
// Setting the read-only value for 'tmpB438.tmp' to true.
// The file read-only value for 'tmpB438.tmp' is True.
//
Imports System.IO

Module FileExample

    Sub Main()

        ' Create a temporary file.
        Dim filePath As String = Path.GetTempFileName()
        Console.WriteLine($"Created a temp file at '{filePath}'.")

        ' Create a new FileInfo object.
        Dim fInfo As New FileInfo(filePath)

        ' Get the read-only value for a file.
        Dim isReadOnly As Boolean = fInfo.IsReadOnly

        ' Display whether the file is read-only.
        Console.WriteLine($"The file read-only value for '{fInfo.Name}' is {isReadOnly}.")

        ' Set the file to read-only.
        Console.WriteLine($"Setting the read-only value for '{fInfo.Name}' to true.")
        fInfo.IsReadOnly = True

        ' Get the read-only value for a file.
        isReadOnly = fInfo.IsReadOnly

        ' Display that the file is now read-only.
        Console.WriteLine($"The file read-only value for '{fInfo.Name}' is {isReadOnly}.")

        ' Make the file mutable again so it can be deleted.
        fInfo.IsReadOnly = False

        ' Delete the temporary file.
        fInfo.Delete()
    End Sub

End Module

' This code produces output similar to the following,
' though results may vary based on the computer, file structure, etc.:
'
' Created a temp file at 'C:\Users\UserName\AppData\Local\Temp\tmpB438.tmp'.
' The file read-only value for 'tmpB438.tmp' is False.
' Setting the read-only value for 'tmpB438.tmp' to true.
' The file read-only value for 'tmpB438.tmp' is True.
'

注釈

プロパティを IsReadOnly 使用して、現在のファイルが読み取り専用かどうかを迅速に判断または変更します。

現在のファイルが存在しない場合、このプロパティを取得すると常に が返 trueされますが、 を設定しようとすると が FileNotFoundExceptionスローされます。

最初に呼び出されると、 が呼び出Refreshされ、FileInfoファイルに関する情報がキャッシュされます。 以降の呼び出しでは、 を呼び出 Refresh して、情報の最新のコピーを取得する必要があります。

適用対象