0

I have written code to extract zip file to C:\Program Files. But its giving unauthorized access. Please help. Its the requirement. I cant change the folder also.

Code:

ZipFile.ExtractToDirectory(archiveFileName, "C:\\Program Files");
Prasad Telkikar
  • 15,207
  • 5
  • 21
  • 44
  • Normally, ```c:\Program Files``` tend to be restricted to Admins only. Pick a different path or ensure that ```c:\Program files``` is r/w to the user account. Best option actually is to use a different path. – ewokx Mar 07 '22 at 04:51
  • Code is the above. I cant change path. Its the requirement – user14805556 Mar 07 '22 at 04:51
  • You can test if you are an admin, or just catch the exception, then prompt to restart with elevated permissions (`startInfo.Verb = "runas";`) https://stackoverflow.com/questions/133379/elevating-process-privilege-programmatically. – Jeremy Lakeman Mar 07 '22 at 04:55
  • You will need to have your app be running as an admin; no one else can write to that location. There's no way around that. Is there a rationale for that _requirement_? – Flydog57 Mar 07 '22 at 05:10
  • I tried the following pro.Verb = "runas"; pro.Arguments = "x \"" + @"C:\Users\Me\AppData\Local\Temp\123.zip" + "\" -o" + @"C:\Program Files"; Process x = Process.Start(pro); x.WaitForExit(); But it not extracting neither giving exception – user14805556 Mar 07 '22 at 05:26
  • 1
    With regards to @ewong's answer, **do not** make `c:/Program File` writable to non-admins. Doing so would open a big security hole in that system – Flydog57 Mar 07 '22 at 06:12

0 Answers0