13 EYLÜL 2011, Salı
Nasıl tek bir Öznitelik (örneğin, salt Okunur bir Dosyayı kaldırmak için?
Diyelim ki, bir dosya aşağıdaki özelliklere sahiptir: salt Okunur, Gizli, Arşiv, Sistem. Nasıl sadece bir özellik kaldırabilir miyim?(örneğin salt Okunur) Eğer kullanırsam:
Io.File.SetAttributes("File.txt",IO.FileAttributes.Normal)
tüm öznitelikleri kaldırır.
CEVAP
13 EYLÜL 2011, Salı
MSDN: Bu gibi herhangi bir öznitelik kaldırabilirsiniz
(ama @sll cevabı için sadece salt Okunur özniteliği) için daha hayırlıdır
using System;
using System.IO;
using System.Text;
class Test
{
public static void Main()
{
string path = @"c:\temp\MyTest.txt";
// Create the file if it exists.
if (!File.Exists(path))
{
File.Create(path);
}
FileAttributes attributes = File.GetAttributes(path);
if ((attributes & FileAttributes.ReadOnly) == FileAttributes.ReadOnly)
{
// Make the file RW
attributes = RemoveAttribute(attributes, FileAttributes.ReadOnly);
File.SetAttributes(path, attributes);
Console.WriteLine("The {0} file is no longer RO.", path);
}
else
{
// Make the file RO
File.SetAttributes(path, File.GetAttributes(path) | FileAttributes.Hidden);
Console.WriteLine("The {0} file is now RO.", path);
}
}
private static FileAttributes RemoveAttribute(FileAttributes attributes, FileAttributes attributesToRemove)
{
return attributes & ~attributesToRemove;
}
}
Bunu Paylaş:
Nasıl Dosya Active Directory üzerinde ...
Alan salt okunur yapmak için Nasıl dja...
Nasıl gıt dizinden bir dosyayı kaldırm...
Bir depo dosyaları silmeden dizinden d...
Nasıl silmeden sürüm kontrolü Bir dosy...