C# get file owner in Windows

我只是一个虾纸丫 提交于 2021-01-27 04:41:29

问题


I want to get the owner of a file using the code below

File.GetAccessControl(filename).GetOwner(typeof(SecurityIdentifier)).Translate(typeof(NTAccount))

However, it gives me BUILTIN\Administrators as the owner, but I can see in the file explorer the owner is Domain\MyUserName.

Why this happens and how can fix it?

Edit: This link explain what happen. It is to do with the files created by users in the Administrator Group and how Windows handle the owner of these files.


回答1:


I was able to get the actual owner of a file by this... not sure if this is what you need or not. System.IO.FileInfo Fi = new System.IO.FileInfo(@"path2file");

MessageBox.Show(Fi.GetAccessControl().GetOwner(typeof(System.Security.Principal.SecurityIdentifier)).Translate(typeof(System.Security.Principal.NTAccount)).ToString());



来源:https://stackoverflow.com/questions/31741786/c-sharp-get-file-owner-in-windows

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!