FileInfo.IsReadOnly Egenskap
Definition
Viktigt
En del information gäller för förhandsversionen av en produkt och kan komma att ändras avsevärt innan produkten blir allmänt tillgänglig. Microsoft lämnar inga garantier, uttryckliga eller underförstådda, avseende informationen som visas här.
Hämtar eller anger ett värde som avgör om den aktuella filen är skrivskyddad.
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
Egenskapsvärde
trueom den aktuella filen är skrivskyddad eller inte kunde hittas; annars . false
Undantag
Det gick inte att hitta filen som beskrivs av det aktuella FileInfo objektet när du försöker ange egenskapen IsReadOnly. (Observera att om du hämtar den här egenskapen för en obefintlig fil genereras inget undantag, utan returnerar truei stället .)
Ett I/O-fel uppstod när filen öppnades.
Den här åtgärden stöds inte på den aktuella plattformen.
-eller-
Anroparen har inte den behörighet som krävs.
Användaren har inte skrivbehörighet, men försökte ange den här egenskapen till false.
Exempel
I följande exempel skapas en tillfällig fil, egenskapen används IsReadOnly för att först kontrollera och sedan ange dess skrivskyddade status och markerar den slutligen som skrivskyddad för att ta bort den.
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.
'
Kommentarer
Använd egenskapen IsReadOnly för att snabbt avgöra eller ändra om den aktuella filen är skrivskyddad.
Om den aktuella filen inte finns returnerar truehämtar den här egenskapen alltid , men försök att ange genererar en FileNotFoundException.
När den först anropas FileInfo anropas Refresh och cachelagrar information om filen. Vid efterföljande anrop måste du anropa Refresh för att få den senaste kopian av informationen.