2012-10-21 25 views
5

我想取得一個文件的所有權並通過C#刪除它。 該文件爲iexplorer.exe,默認爲當前所有者 - TrustedInstaller。 FileSecurity.SetOwner方法似乎設置了指定的所有權,但實際上並沒有更改最初的所有者,也沒有引發異常。 顯然,下次嘗試刪除文件會引發異常。 代碼中應該更改什麼來獲取文件的所有權並將其刪除?取得一個文件的所有權c#

var fileS = File.GetAccessControl(@"C:\Program Files (x86)\Internet Explorer\iexplore.exe"); 
fileS.SetOwner(new System.Security.Principal.NTAccount(Environment.UserDomainName, Environment.UserName)); 
File.Delete(@"C:\Program Files (x86)\Internet Explorer\iexplore.exe"); 
+2

這是怎麼回事了UAC在這裏?你在跑步嗎? –

+0

是的,並沒有幫助。您需要設置權限,然後刪除給定的文件。解決方案在這裏:http://msdn.microsoft.com/en-us/magazine/cc164701.aspx?code=true&level=root%2cPrivilege11 – alternative

回答

4

必須明確啓用SeTakeOwnershipPrivilege

需要採取的對象的所有權,而不被授予 任意訪問。此特權允許所有者值僅被設置爲 ,持有者可合法地將其指定爲對象的所有者。 用戶權限:取得文件或其他對象的所有權。

我建議你閱讀馬克諾瓦克撰寫的偉大文章:Manipulate Privileges in Managed Code Reliably, Securely, and Efficiently

和/或看看他的sample

更新

用法示例:

var fileS = File.GetAccessControl(@"C:\Program Files (x86)\Internet Explorer\iexplore.exe"); 

Privilege p; 
bool ownerChanged = false; 
try 
{ 
    p = new Privilege(Privilege.TakeOwnership); 
    p.Enable(); 

    fileS.SetOwner(new System.Security.Principal.NTAccount(
     Environment.UserDomainName, Environment.UserName)); 

    ownerChanged = true; 
} 
catch(PrivilegeNotHeldException e) 
{ 
    // privilege not held 
    // TODO: show an error message, write logs, etc. 
} 
finally 
{ 
    p.Revert(); 
} 

if (ownerChanged) 
    File.Delete(@"C:\Program Files (x86)\Internet Explorer\iexplore.exe"); 
+0

謝謝,尼古拉。有人的工作代碼仍然值得歡迎。 – alternative

+0

@alternative我的答案包含指向具有示例代碼的Mark特權級別實現的鏈接。我不認爲我應該在這裏複製並粘貼大約1k行代碼。雖然我已經用示例用法更新了我的答案。 –

+0

文章鏈接已損壞,但您仍然可以通過滾動到此頁面的底部獲取CHM格式:https://msdn.microsoft.com/magazine/msdn-magazine-issues並在2005年3月之後單擊。點擊它,在屬性中你需要解除鎖定,否則它會變成空白。此外,源代碼在CHM中以.exe文件形式鏈接,無論出於何種原因,它都需要安裝。 – TripleAntigen

1
 string filepath = @"C:\Program Files (x86)\Internet Explorer\iexplore.exe"; 

     //Get Currently Applied Access Control 
     FileSecurity fileS = File.GetAccessControl(filepath); 

     //Update it, Grant Current User Full Control 
     SecurityIdentifier cu = WindowsIdentity.GetCurrent().User; 
     fileS.SetOwner(cu); 
     fileS.SetAccessRule(new FileSystemAccessRule(cu, FileSystemRights.FullControl, AccessControlType.Allow)); 

     //Update the Access Control on the File 
     File.SetAccessControl(filepath, fileS); 

     //Delete the file 
     File.Delete(filepath); 

添加以下進口

 using System.IO; 
     using System.Security.AccessControl; 
     using System.Security.Principal; 

運行的代碼在提升模式。

1

技術在Windows 8.1採用階級的特權從例如: Manipulate Privileges in Managed Code Reliably, Securely, and Efficiently

private bool TryDeleteFile(string fileName) 
    { 
     string filePath = Path.GetFullPath(fileName); 
     var fi = new FileInfo(filePath); 

     bool ownerChanged = false; 
     bool accessChanged = false; 
     bool isDelete = false; 

     FileSecurity fs = fi.GetAccessControl(); 
     Privilege p = new Privilege(Privilege.TakeOwnership); 

     try 
     { 
      p.Enable(); 
      fs.SetOwner(WindowsIdentity.GetCurrent().User); 
      File.SetAccessControl(filePath, fs); //Update the Access Control on the File 
      ownerChanged = true; 
     } 
     catch (PrivilegeNotHeldException ex) { } 
     finally { p.Revert(); } 

     try 
     { 
      fs.SetAccessRule(new FileSystemAccessRule(WindowsIdentity.GetCurrent().User, FileSystemRights.FullControl, AccessControlType.Allow)); 
      File.SetAccessControl(filePath, fs); 
      accessChanged = true; 
     } 
     catch (UnauthorizedAccessException ex) { } 

     if (ownerChanged && accessChanged) 
     { 
      try 
      { 
       fi.Delete(); 
       isDelete = true; 
      } 
      catch (Exception ex) { } 
     } 

     return isDelete; 
    } 
相關問題