FileInfo.IsReadOnly Properti
Definisi
Penting
Beberapa informasi terkait produk prarilis yang dapat diubah secara signifikan sebelum dirilis. Microsoft tidak memberikan jaminan, tersirat maupun tersurat, sehubungan dengan informasi yang diberikan di sini.
Mendapatkan atau menetapkan nilai yang menentukan apakah file saat ini hanya dibaca.
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
Nilai Properti
true
jika file saat ini hanya dibaca atau tidak dapat ditemukan; jika tidak, false
.
Pengecualian
File yang dijelaskan oleh objek saat ini FileInfo tidak dapat ditemukan ketika mencoba mengatur properti IsReadOnly. (Perhatikan bahwa mendapatkan properti ini untuk file yang tidak ada tidak akan memberikan pengecualian, melainkan mengembalikan true
.)
Terjadi kesalahan I/O saat membuka file.
Operasi ini tidak didukung pada platform saat ini.
-atau-
Pemanggil tidak memiliki izin yang diperlukan.
Pengguna tidak memiliki izin tulis, tetapi mencoba mengatur properti ini ke false
.
Contoh
Contoh berikut membuat file sementara, menggunakan IsReadOnly properti untuk memeriksa terlebih dahulu lalu mengatur status baca-saja, dan akhirnya menandainya sebagai baca-tulis untuk menghapusnya.
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.
'
Keterangan
IsReadOnly Gunakan properti untuk menentukan atau mengubah dengan cepat apakah file saat ini hanya dibaca.
Jika file saat ini tidak ada, mendapatkan properti ini akan selalu mengembalikan true
, tetapi upaya untuk mengatur akan melemparkan FileNotFoundException.
Ketika pertama kali dipanggil, FileInfo panggilan Refresh dan cache informasi tentang file. Pada panggilan berikutnya, Anda harus memanggil Refresh untuk mendapatkan salinan informasi terbaru.