FileInfo.IsReadOnly Eigenschaft

Definition

Ruft einen Wert ab, der bestimmt, ob die aktuelle Datei schreibgeschützt ist, oder legt diesen Wert fest.

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

Eigenschaftswert

true , wenn die aktuelle Datei schreibgeschützter Ist oder nicht gefunden werden konnte; falseandernfalls .

Ausnahmen

Die vom aktuellen FileInfo Objekt beschriebene Datei konnte beim Festlegen der IsReadOnly-Eigenschaft nicht gefunden werden. (Beachten Sie, dass das Abrufen dieser Eigenschaft für eine nicht vorhandene Datei keine Ausnahme auslöst, sondern stattdessen zurückgibt true.)

Beim Öffnen der Datei ist ein E/A-Fehler aufgetreten.

Dieser Vorgang wird von der aktuellen Plattform nicht unterstützt.

- oder -

Der Aufrufer verfügt nicht über die erforderliche Berechtigung.

Der Benutzer hat keine Schreibberechtigung, aber versucht, diese Eigenschaft auf false festzulegen.

Beispiele

Im folgenden Beispiel wird eine temporäre Datei erstellt, die IsReadOnly -Eigenschaft verwendet, um zuerst die schreibgeschützte status zu überprüfen und dann festzulegen, und schließlich markiert sie als Lese-/Schreibzugriff, um sie zu löschen.

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

Hinweise

Verwenden Sie die IsReadOnly -Eigenschaft, um schnell zu bestimmen oder zu ändern, ob die aktuelle Datei schreibgeschützte Datei ist.

Wenn die aktuelle Datei nicht vorhanden ist, gibt das Abrufen dieser Eigenschaft immer zurück true, aber versuche, festzulegen, lösen einen aus FileNotFoundException.

Ruft beim ersten Aufruf FileInfoRefresh Informationen zur Datei auf und speichert sie zwischen. Bei nachfolgenden Aufrufen müssen Sie aufrufen Refresh , um die neueste Kopie der Informationen zu erhalten.

Gilt für: