0

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?

Mo Patel
  • 2,321
  • 4
  • 22
  • 37
  • 4
    Are you trying to delete your own executable file? No wonder you get an access denied. – Steve Aug 12 '13 at 22:08
  • @Steve It's an auto update software. I download a file called update.exe, which is the new updated software, I kill my current, rename update.exe to my app name, and run it again. And no, thats' not why I'm getting access is denied. – user2674867 Aug 12 '13 at 22:10
  • 5
    If the application is on Program Files that's why you need the Admin privileges. – Rafael Aug 12 '13 at 22:15
  • possible duplicate of [Elevating process privilege programatically?](http://stackoverflow.com/questions/133379/elevating-process-privilege-programatically) – metadings Aug 12 '13 at 22:58

1 Answers1

0

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.

Community
  • 1
  • 1
Peter M
  • 7,309
  • 3
  • 50
  • 91