Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/file/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 取得文件c的所有权#_C#_File_Io_File Security - Fatal编程技术网

C# 取得文件c的所有权#

C# 取得文件c的所有权#,c#,file,io,file-security,C#,File,Io,File Security,我正在尝试获取文件的所有权并通过C#删除它。 该文件为iexplorer.exe,默认情况下为当前所有者-TrustedInstaller。 方法FileSecurity.SetOwner似乎设置了指定的所有权,但实际上没有更改初始所有者,也不会引发异常。 显然,下一次尝试删除该文件时会引发异常。 要获得文件的所有权并将其删除,应在代码中更改哪些内容 var fileS = File.GetAccessControl(@"C:\Program Files (x86)\Internet Explo

我正在尝试获取文件的所有权并通过C#删除它。 该文件为iexplorer.exe,默认情况下为当前所有者-TrustedInstaller。 方法FileSecurity.SetOwner似乎设置了指定的所有权,但实际上没有更改初始所有者,也不会引发异常。 显然,下一次尝试删除该文件时会引发异常。 要获得文件的所有权并将其删除,应在代码中更改哪些内容

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");

您必须明确启用:

需要在未被授权的情况下获得对象的所有权 自由访问。此权限允许设置所有者值 仅适用于持有人可合法指定为 对象的所有者。用户权限:获取文件或其他文件的所有权 对象

我建议你阅读马克·诺瓦克写的那篇伟大的文章:

和/或看看他的

更新

用法示例:

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");
添加以下导入

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

在提升模式下运行代码。

在Windows 8.1中启用,使用示例中的类权限:


有关添加上下文菜单的信息,请参见这些注册表项。我可以在Windows 7上重命名该文件夹以及iexplorer_OFF.exe。 您可能可以从代码中shell/执行相同的代码


这里的UAC是怎么回事?你跑得高吗?是的,没有帮助。您需要设置权限,然后删除给定的文件。答案就在这里:谢谢你,尼古拉。某人的工作代码仍然非常受欢迎。@我的回答包含指向Mark的
Privilege
类实现的链接以及示例代码。我认为我不应该在这里复制和粘贴大约1k行代码。虽然我已经用一个示例用法更新了我的答案。文章链接已断开,但您仍然可以通过滚动到此页面底部并单击2005年3月,以CHM形式获得它。保存后,右键单击它,在“属性”中需要取消阻止,否则它将显示为空。此外,源代码从CHM中链接到一个包含所有内容的.exe文件中,看起来它出于任何原因都需要安装。我尝试了所有其他获取文件所有权的建议--在本页上的建议,希望我使用第三方类,以及其他网站上的建议,但只有你的建议对我有效!谢谢(我遗漏了file.delete部分,因为我只是出于其他原因想获得所有权。)
        using System.IO;
        using System.Security.AccessControl;
        using System.Security.Principal;
    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;
    }