简体   繁体   中英

Access is denied (Running a .bat file)

This is my code to build up a .bat file and run it.

StringBuilder Batch = new StringBuilder();
Batch.AppendLine("@echo off");
Batch.AppendLine("taskkill /IM " + Process.GetCurrentProcess().ProcessName + ".exe /F");
Batch.AppendLine("ping localhost > nul");
Batch.AppendLine("del /f " +  (char)34 + Application.ExecutablePath + (char)34);
Batch.AppendLine("ren " + (char)34 + Application.StartupPath + @"\update.exe" + (char)34 + " " + Process.GetCurrentProcess().ProcessName);
Batch.AppendLine((char)34 + Application.ExecutablePath + (char)34);
Batch.AppendLine("del %0");

File.WriteAllText(Application.StartupPath + @"\update.bat", Batch.ToString(), Encoding.Default);
        Process.Start(Application.StartupPath + @"\update.bat");

However, I get access is denied, I have to run it as a admin, how can I do it?

Not an answer to your question, but I think you are not approaching it from the best direction. Instead see this question that provides a very robust answer for what you are actually trying to do: what-is-the-best-way-to-auto-update-a-windows-application

TL;DR If your original installation is "All users" then you will have problems with having to manually elevate permissions. Is the installation is "Per user" then this simplifies the permissions issue.

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