none
'System.UnauthorizedAccessException' with administrative privileges RRS feed

  • Question

  • I wrote a simple program that attempts to delete (via File.Delete) a file in a directory underneath ProgramData.

    Even though my account has administrative privileges I get the 'System.UnauthorizedAccessException' and my program closes without deleting the file.

    Is there any way that I can delete the file?

    Thanks in advance for any help.

    - Michael Bate

    Monday, December 10, 2018 2:50 AM

All replies

  • Hi MichaelBate,

    Thank you for posting here

    For your question, what is the file you want to delete?

    Normally, File.Delete () method would work well except the file is special. Please try the code below.

    File.SetAttributes(filePath, FileAttributes.Normal);
    File.Delete(filePath);

    Best Regards,

    Wendy


    MSDN Community Support
    Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.

    • Proposed as answer by Stanly Fan Monday, December 17, 2018 9:50 AM
    Tuesday, December 11, 2018 7:04 AM
    Moderator
  • Wendy, THANK YOU for your reply

    Unfortunately setting the file attributes to normal, as in:

    File.SetAttributes(filePath, FileAttributes.Normal);

    does not solve the problem.

    The file that I am trying to delete is one that I created myself, it is:

    c:\ProgramData\MX4590XULBP\MULBaagghhX4590X (I don't think the actual names are material - they are nonsense names made up as part of a licensing scheme.)

    The critical thing is probably that they are placed under c:\ProgramData which is a hidden system directory.  It is puzzling that I am able to create this directory and file, but not allowed to delete the file.

    It looks like I will have to instruct my clients to "Run ad Administrator."

    Tuesday, December 11, 2018 4:02 PM
  • Hi MichaelBate,

    According to your feedback, I test the delete method. It works well under ProgramData Directory even though I do not run as admin.

    Please check the file in your clients again.

    Best Regards,

    Wendy


    MSDN Community Support
    Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.

    Wednesday, December 12, 2018 7:14 AM
    Moderator
  • This is very puzzling.  I keep getting the exception if I do not set "Run ad Administrator."

    I'm wondering if there is something about the way I set up the account, which has Administrative privileges.   I am running Windows 7 Pro 64-bit.  There does not seem to be many options to set in the user account.  Under User Account Settings - what do do if someone makes changes to the computer, I chose the Default setting.  I don't see anything else that might be relevant that I can change.

    Monday, December 17, 2018 10:02 PM
  • First, with UAC enabled members of the Administrators group have no more privileges than a standard user unless a process they are running has been elevated (i.e., run as administrator).

    Windows distinguishes between write permissions and delete permissions. The ACL on the folder/file created beneath ProgramData probably does not grant delete permission.  You can check this by examining the permissions displayed by viewing the property page for the folder/file and clicking on the Security tab.

    Monday, December 17, 2018 10:43 PM