Hello fellow developers,
I'm trying to give a C# application that I'm creating the ability to delete the files from a flash-drive or other folder, for which admin priv is needed. But it's not working!
I am an admin user on my Win10 box. I can delete the files in File Explorer. But the program throws an IOException when it tries to do it (I'm using any of several methods to delete the files, including interoping to Win32).
I did create and embed an Application Manifest for the program project (I'm using Visual Studio 2017), and set the level attribute of the requestedExecutionLevel element to "requireAdministrator". And within the project-properties it does show this file, app.manifest, as part of it. But that seems to accomplish nothing -- the program can still be launched NOT as administrator.
Btw, I see two different methods to detect whether the program is running as with administrator priv:
public bool IsRunAsAdmin1()
{
return Thread.CurrentPrincipal.IsInRole( WindowsBuiltInRole.Administrator.ToString() );
}
This does NOT seem to be working (always returning false). and..
public bool IsRunAsAdmin2()
{
WindowsIdentity id = WindowsIdentity.GetCurrent();
WindowsPrincipal principal = new WindowsPrincipal( id );
return principal.IsInRole( WindowsBuiltInRole.Administrator );
}
- which does work.
Regardless, even when I right-click on the program and run it as administrator -- it returns Win32 error 5 (access denied) for Hidden files.
I want to make this delete files robustly, regardless of the file's attributes. How can I do this?
Thank you for your help and insights, James