C# eğer kullanıcı bir klasöre yazma erişimi varsa Test
Eğer bir kullanıcı aslında bunu denemeden önce bir klasöre yazabilirsiniz eğer test etmem lazım.
Klasörü Directory.GetAccessControl() yöntem kullanarak güvenlik izinleri almaya çalışan aşağıdaki yöntemi (C# 2.0) uygulanan ettik.
private bool hasWriteAccessToFolder(string folderPath)
{
try
{
// Attempt to get a list of security permissions from the folder.
// This will raise an exception if the path is read only or do not have access to view the permissions.
System.Security.AccessControl.DirectorySecurity ds = Directory.GetAccessControl(folderPath);
return true;
}
catch (UnauthorizedAccessException)
{
return false;
}
}
Yazma erişimi için test etmek için nasıl araştırıyordum ne zaman böyle bir şey geldi ve aslında Windows izinleri test etmek için çok karmaşık göründü. Aşırı basitleştirme şeyler duyuyorum ve bu yöntem işe yarıyor gibi olmasa da sağlam olduğunu düşünüyorum.
Eğer geçerli kullanıcı yazma erişimi varsa test etmek için benim yöntem doğru çalışır mı?
CEVAP
Bu biraz bu yazı için geç olduğunun farkındayım, ama bu kod biraz faydalı olabilir.
string path = @"c:\temp";
string NtAccountName = @"MyDomain\MyUserOrGroup";
DirectoryInfo di = new DirectoryInfo(path);
DirectorySecurity acl = di.GetAccessControl(AccessControlSections.All);
AuthorizationRuleCollection rules = acl.GetAccessRules(true, true, typeof(NTAccount));
//Go through the rules returned from the DirectorySecurity
foreach (AuthorizationRule rule in rules)
{
//If we find one that matches the identity we are looking for
if (rule.IdentityReference.Value.Equals(NtAccountName,StringComparison.CurrentCultureIgnoreCase))
{
//Cast to a FileSystemAccessRule to check for access rights
if ((((FileSystemAccessRule)rule).FileSystemRights & FileSystemRights.WriteData)>0)
{
Console.WriteLine(string.Format("{0} has write access to {1}", NtAccountName, path));
}
else
{
Console.WriteLine(string.Format("{0} does not have write access to {1}", NtAccountName, path));
}
}
}
Console.ReadLine();
İçine bir uygulama Konsol ve eğer ihtiyacınız ne olursa bakın o damla.

Eğer bir Sınıf özniteliği varsa Test?...
Eğer bir giriş varsa test etmek için k...
Nasıl eğer çalıştırılabilir bir dosya ...
Eğer kullanıcı push bildirimleri etkin...
Dosyaya yazma, ama eğer varsa, üzerine...