4 Mart 2009, ÇARŞAMBA
Nasıl C salt-okunur dosya bir dizin silerim#?
Salt okunur dosyalarını içeren bir dizini silmek istiyorum. Hangi yaklaşım daha iyi
DirectoryInfo.Delete()kullanarakManagementObject.InvokeMethod("Delete")?
DirectoryInfo.Delete() ile el ile salt okunur her dosya için öznitelik kapatmak için var, ama ManagementObject.InvokeMethod("Delete") gerek görünmüyor. Başka bir tane daha tercih edilir herhangi bir durum var mı?
Örnek kod (test.txt salt okunur).
Birinci yol:
DirectoryInfo dir = new DirectoryInfo(@"C:\Users\David\Desktop\");
dir.CreateSubdirectory("Test");
DirectoryInfo test = new DirectoryInfo(@"C:\Users\David\Desktop\Test\");
File.Copy(@"C:\Users\David\Desktop\test.txt", @"C:\Users\David\Desktop\Test\test.txt");
File.SetAttributes(@"C:\Users\David\Desktop\Test\test.txt", FileAttributes.Archive);
test.Delete(true);
İkinci yol:
DirectoryInfo dir = new DirectoryInfo(@"C:\Users\David\Desktop\");
dir.CreateSubdirectory("Test");
DirectoryInfo test = new DirectoryInfo(@"C:\Users\David\Desktop\Test\");
File.Copy(@"C:\Users\David\Desktop\test.txt", @"C:\Users\David\Desktop\Test\test.txt");
string folder = @"C:\Users\David\Desktop\Test";
string dirObject = "Win32_Directory.Name='" folder "'";
using (ManagementObject managementObject = new ManagementObject(dirObject))
{
managementObject.Get();
ManagementBaseObject outParams = managementObject.InvokeMethod("Delete", null,
null);
// ReturnValue should be 0, else failure
if (Convert.ToInt32(outParams.Properties["ReturnValue"].Value) != 0)
{
}
}
CEVAP
3 Ocak 2012, Salı
En basit kaçıyor özyinelemeli arama yolu gibi FileSystemInfo s, alırken AllDirectoriesseçeneği kullanarak
public static void ForceDeleteDirectory(string path)
{
var directory = new DirectoryInfo(path) { Attributes = FileAttributes.Normal };
foreach (var info in directory.GetFileSystemInfos("*", SearchOption.AllDirectories))
{
info.Attributes = FileAttributes.Normal;
}
directory.Delete(true);
}
Bunu PaylaÅŸ:

Nasıl Dosya Active Directory üzerinde ...
Nasıl salt okunur bir dosya silebiliri...
Nasıl bir dizin C veya C kullanarak do...
Nasıl okunur bir metin Python string b...
Alan salt okunur yapmak için Nasıl dja...