可能重复:
在C#中获取/设置文件所有者
我搜索了互联网,但没有发现如何获取文件创建者/所有者在FileInfo类中只有其他属性,如lastAccessTime,Size,但不是所有者/创建者.
有人知道答案吗?
我正在努力克服以下情况.
给定存储在NTFS卷上的目录,其中:
(或者简而言之,所有管理员都被锁定在文件夹之外)
但!
(或者简而言之,我有权修复DACL /所有者)
我对以下代码应该没有问题:
WindowsIdentity privilegedUser = System.Security.Principal.WindowsIdentity.GetCurrent();
// I cannot use File.GetAccessControl() as I get access denied
// (working as intended! I have no access to read the ACL!)
// so I have to write a new ACL:
FileSecurity acl = new FileSecurity();
acl.SetOwner(admin.User);
acl.AddAccessRule(new FileSystemAccessRule(privilegedUser.User, FileSystemRights.FullControl, AccessControlType.Allow));
File.SetAccessControl("c:\\path\\to\\broken", acl);
Run Code Online (Sandbox Code Playgroud)
但是,SetAccessControl调用会抛出UnauthorizedAccessException.当我改变它只调整所有者时,同样的事情发生.当我只尝试调整DACL时,同样的事情.
我通过在Process Explorer中检查生成的进程并验证Administrators组是否设置为"Owner"而不是"Disabled"来验证问题不是UAC.我应该拥有执行此操作所需的所有权限(备份操作员在面对管理员时应该是无关紧要的,但我将其添加进行测试) - 但它只是继续拒绝访问权限.
相关的technet文档:http://technet.microsoft.com/en-us/library/cc783530%28WS.10%29.aspx
在我拔掉剩下的头发之前,我想对此有所了解.
我正在尝试获取文件夹的所有权.我当然是以管理员身份运行程序,因为我可以更改资源管理器中的所有者,所以我有权获得所有权.
但是,如果管理员或我的帐户拥有它,我可以更改所有者,如果我已拥有所有权,我可以更改权限.
如果我试图给自己一个文件的所有权,让我说由SYSTEM拥有,那么我得到一个unauthorizedexception.
我用accesscontrol方法尝试了一些不同的东西,但没有任何效果,我认为这个最新的方法直接来自本书.
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);
}
Run Code Online (Sandbox Code Playgroud)
我正在尝试使用Windows 7顺便说一句.
我在这里错过了什么?
我需要找出谁使用.NET创建了一个文件
我已经尝试过以下方法:
string FileLocation = @"C:\test.txt";
FileInfo droppedFile = new FileInfo(FileLocation);
FileSecurity fileSecurity = droppedFile.GetAccessControl();
IdentityReference identityReference = fileSecurity.GetOwner(typeof(NTAccount));
string userName = identityReference.Value;
Console.WriteLine(userName);
Run Code Online (Sandbox Code Playgroud)
所有这些返回的是"BUILTIN\Administrators"
我在这里做错了吗?因为当我在资源管理器中查看C:\时,所有者会显示正确的用户名,当我执行上面的代码时,它会返回"BUILTIN\Administrators"
哪个甚至不是域名和用户名,我认为它是一个安全组.
任何帮助赞赏.
我知道已经有人问过这个问题,但到目前为止我找不到解决方案。
我想要做的是卸载 Windows 服务并使用 C# 删除带有 Windows 服务的文件夹。
Windows 服务卸载
public static void Uninstall(string exeFilename)
{
var commandLineOptions = new string[1] { "/LogFile=uninstall.log" };
if (!Directory.Exists(exeFilename)) return;
var fileNames = Directory.GetFiles(exeFilename);
var serviceFile = fileNames.FirstOrDefault(f => f.EndsWith(".exe"));
var serviceFileName = Path.GetFileName(serviceFile);
var serviceName = Path.GetFileNameWithoutExtension(serviceFile);
var serviceExists = ServiceController.GetServices().Any(s => s.ServiceName == serviceName);
if (!serviceExists) return;
var installer =
new AssemblyInstaller($"{exeFilename}\\{serviceFileName}", commandLineOptions)
{
UseNewContext = true
};
installer.Uninstall(null);
installer.Dispose();
}
Run Code Online (Sandbox Code Playgroud)
文件夹和文件删除
public static void DeleteFolder(string folderPath)
{
if(!Directory.Exists(folderPath)) return;
try …Run Code Online (Sandbox Code Playgroud)