FileInfo.IsReadOnly Eigenschap

Definitie

Hiermee wordt een waarde opgehaald of ingesteld die bepaalt of het huidige bestand alleen-lezen is.

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

Waarde van eigenschap

true als het huidige bestand alleen-lezen is of niet kan worden gevonden; anders, false.

Uitzonderingen

Het bestand dat door het huidige FileInfo object wordt beschreven, is niet gevonden bij het instellen van de eigenschap IsReadOnly. (Houd er rekening mee dat het ophalen van deze eigenschap voor een niet-bestaand bestand geen uitzondering genereert, maar eerder retourneert true.)

Er is een I/O-fout opgetreden tijdens het openen van het bestand.

Deze bewerking wordt niet ondersteund op het huidige platform.

– of –

De beller heeft niet de vereiste machtiging.

De gebruiker heeft geen schrijfmachtiging, maar heeft geprobeerd deze eigenschap in te stellen op false.

Voorbeelden

In het volgende voorbeeld wordt een tijdelijk bestand gemaakt, wordt de eigenschap gebruikt om eerst de IsReadOnly status alleen-lezen te controleren en vervolgens in te stellen. Ten slotte wordt het gemarkeerd als lezen/schrijven om het bestand te verwijderen.

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

Opmerkingen

Gebruik de IsReadOnly eigenschap om snel te bepalen of het huidige bestand alleen-lezen is of wijzigen.

Als het huidige bestand niet bestaat, wordt het ophalen van deze eigenschap altijd geretourneerd true, maar wordt geprobeerd FileNotFoundExceptioneen in te stellen.

Wanneer u het eerst aanroept, FileInfo worden gegevens over het bestand aangeroepen Refresh en in de cache opgeslagen. Bij volgende oproepen moet u bellen Refresh om de meest recente kopie van de gegevens op te halen.

Van toepassing op