[英]C# folder permissions
我需要擁有該文件夾及其子文件夾的所有權才能刪除它。 手動處理對我來說很好:
但是我有大約一百個這樣的文件夾( 原因 ),所以我想以編程方式刪除它們。
我的代碼獲得了文件夾的所有權,而不是子文件夾的所有權。
var identity = WindowsIdentity.GetCurrent().User;
var dirInfo = new DirectoryInfo(dirPath);
var dirSecurity = dirInfo.GetAccessControl();
dirSecurity.SetOwner(identity);
var fullControlRule = new FileSystemAccessRule(identity,
FileSystemRights.FullControl,
InheritanceFlags.ContainerInherit | InheritanceFlags.ObjectInherit,
PropagationFlags.None,
AccessControlType.Allow);
dirSecurity.AddAccessRule(fullControlRule);
dirInfo.SetAccessControl(dirSecurity);
之后嘗試刪除文件夾時,出現System.UnauthorizedAccessException:拒絕訪問路徑'C:\\ a4f31eae62bde1cb4d49834897 \\ Sandbox'。
任何幫助將非常感激! 謝謝。
對我有用的最終解決方案是使用命令行(PowerShell)的接管服務 。 雖然我仍然對C#解決方案感到迷惑...
感謝用戶后來刪除了他的建議。
最終的PowerShell代碼清除提到的文件夾:
$dirNames = Get-ChildItem "C:\" | Select-String -Pattern "[a-f0-9]{18,30}"
foreach ($dirName in $dirNames) {
TakeOver("C:\$dirName")
}
function TakeOver($dirPath) {
takeown /f $dirPath /r /d Y
#icacls $dirPath /reset /T
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.