windows 取得文件或文件夹的所有权

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/5368825/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-15 16:28:00  来源:igfitidea点击:

Taking ownership of a file or folder

c#windowspermissions

提问by Steinthor.palsson

Before I pull out the rest of my hair I'd like to get some input on this.
I'm trying to take ownership of a folder. I'm running the program as administrator of course and I do have rights to take ownership since I can change the owner in explorer.

在我拔出其余的头发之前,我想就此获得一些意见。
我正在尝试取得文件夹的所有权。我当然以管理员身份运行该程序,并且我确实有权获得所有权,因为我可以在资源管理器中更改所有者。

I can however change the owner if either administrator or my account owns it, and I can change permissions if I already have ownership.
If I try to give myself ownership of a file, lets say owned by SYSTEM, then I get an unauthorizedexception.

但是,如果管理员或我的帐户拥有它,我可以更改所有者,如果我已经拥有所有权,我可以更改权限。
如果我尝试给自己一个文件的所有权,比如说由 SYSTEM 拥有,那么我会得到一个未经授权的异常。

I've tried some different things with the accesscontrol methods but nothing works, this latest method I think is directly by the book.

我已经用访问控制方法尝试了一些不同的东西,但没有任何效果,我认为这个最新的方法直接来自这本书。

        private static void makePerm(string file, NTAccount account)
    {
        FileInfo finfo = new FileInfo(file);
        FileSecurity fsecurity = finfo.GetAccessControl();
        //also tried it like this //fsecurity.ResetAccessRule(new FileSystemAccessRule(string.Format(@"{0}\{1}", Environment.UserDomainName.ToString(), Environment.UserDomainName.ToString()), FileSystemRights.FullControl, AccessControlType.Allow));
        fsecurity.SetOwner(account);
        finfo.SetAccessControl(fsecurity);
    }

I'm trying this on Windows 7 btw.
What am I missing here?

顺便说一句,我正在 Windows 7 上尝试此操作。
我在这里错过了什么?

回答by Maverik

I had the same problem and just posting here for anybody else who may come here searching like me:

我遇到了同样的问题,只是在这里发布给可能像我一样来这里搜索的其他人:

You need to explicitly enable SeTakeOwnershipPrivilege in code as Luke mentions above. I found this Process Privilegesto be really helpful dealing with this sort of thing.

正如 Luke 上面提到的,您需要在代码中显式启用 SeTakeOwnershipPrivilege。我发现这个进程特权对于处理这类事情真的很有帮助。

Here is how it fixed my code:

这是它修复我的代码的方法:

using System;
using System.Diagnostics;

// ...
using (new ProcessPrivileges.PrivilegeEnabler(Process.GetCurrentProcess(), Privilege.TakeOwnership))
{
    directoryInfo = new DirectoryInfo(path);
    directorySecurity = directoryInfo.GetAccessControl();

    directorySecurity.SetOwner(WindowsIdentity.GetCurrent().User);
    Directory.SetAccessControl(path, directorySecurity);    
}

回答by pjulien

Did you elevate your process via UAC first? On Windows 7, without UAC escalation, your process is running with the lower privileged token.

您是否首先通过 UAC 提升了流程?在 Windows 7 上,如果没有 UAC 升级,您的进程将使用较低特权的令牌运行。