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 回 。

第一次呼叫時, FileInfo 會呼叫 Refresh 和 快取檔案的相關資訊。 在後續的呼叫中,您必須呼叫 Refresh 以取得資訊的最新複本。

適用於