0

I have code running that needs to access a file that cannot be accessed without administrator permissions. When I run my app as administrator, there is no problem. When I run it normally, I get an "Access is denied" error. I would prefer to not have the entire app require administrative permissions unless this functionality is utilized.

I am running the methods needing administrative access through a background worker. Is there a way for just the background worker or this group of methods to request administrative access? The class actually accessing the file is ZipFile, so if there's a way to request the permissions there, that is also an option.

Thank you. Below is the code that throws an error if not running the app as administrator. The code is opening an existing .zip file, modifying a file within the zip, then saving the zip.

using (var zip = new ZipFile(filePath))
{
    zip.UpdateEntry("file.xml", theXElement.ToString(), encoding: Encoding.UTF8);
    zip.Save();
}
MatthewHagemann
  • 1,167
  • 2
  • 9
  • 20
  • 3
    Administrator permissions require an elevated user token, which cannot be achieved without running a process as administrator. If you don't want to run the whole program as administrator, split out the small tasks into a small utility program and then call it whenever necessary as administrator from the main program. – Lex Li Apr 09 '21 at 19:49
  • Do you get the `Access is denied` error on the construction of `new ZipFile` and or the update/save? Also are you using the `System.IO.Compression` ZipFile class, I have a feeling you are not, possibly `DotNetZip`? – Trevor Apr 09 '21 at 20:07

0 Answers0