简体   繁体   中英

Why can .NET app not overwrite file when user is Administrator?

My WPF app downloads and caches files to the C:\\ProgramData\\CompanyName\\ProductName\\Data\\ directory.

If another user was logged in when they downloaded (created) the file, I cannot overwrite the file to update it, even though I am an Administrator.

Why do I not have write access when I am also an Admin user?
The screenshot shows the file permissions of a file I cannot overwrite.

帐号设定

I understand that I have to elevate the process to do an 'admin' task ( as explained here ), but seeing as the other user was an Administrator (and so am I), and the 'Owner' of the file is 'Administrator' - why don't I have write permission on a file where the Owner is in the same group? Why don't we share the same privileges?

After downloading and saving the file you may set full access for all users (or any other appropriate group) by code like this:

var fileSecurity = File.GetAccessControl(path);

fileSecurity.AddAccessRule(
    new FileSystemAccessRule(
        new SecurityIdentifier(WellKnownSidType.BuiltinUsersSid, null),
        FileSystemRights.FullControl,
        AccessControlType.Allow));

File.SetAccessControl(path, fileSecurity);

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM