Click here to Skip to main content
15,889,176 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
hi,
i have this code, to create .bat file with c# and run that.

C#
var gt = txt_gateway.text;
            System.IO.StreamWriter SW = new System.IO.StreamWriter("route.bat");
            SW.WriteLine("route add 88.88.88.88 \"" + gt + "\"");
            SW.WriteLine("route add 89.89.89.89 \"" + gt + "\"");
            SW.Flush();
            SW.Close();
            SW.Dispose();
            SW = null;


this code create a file route.bat

in the directory and want to run that .bat file az administrator

how can i use?

if any code can help me to run that cmd command as administrator please help me.

thanks

What I have tried:

i try this methods but not working.

C#
ProcessStartInfo sti = new ProcessStartInfo();
sti.FileName = "myScript.bat";
sti.Verb = "runas";
Process.Start(sti);


and

C#
ProcessStartInfo psi = new ProcessStartInfo();
               psi.WorkingDirectory = "C:\\Diego\\PublishCore";
               //   psi.CreateNoWindow = true;
               psi.FileName = @"cmd.exe";
               psi.Arguments = "/c start /wait " + "C:\\Diego\\PublishCore\\Startup_InomCore.bat";
               //     psi.UseShellExecute = true;
               psi.Verb = "runas";
               var process = Process.Start(psi);
Posted
Updated 9-Jun-20 5:57am

1 solution

At the risk of quoting myself:
Quote:
See here: UAC Elevation in Managed Code: Starting Elevated Processes[^]

If you had googled, you would have found this question from 2016: Answer for: How to run bat file run as administrator using C# .NET?[^]

In future, please try to do basic research for yourself, and not waste your time or ours ...
 
Share this answer
 
v2
Comments
Richard Deeming 9-Jun-20 12:25pm    
Your second link was going to the form to edit your answer, rather than linking to the answer itself. I've fixed it now. :)

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900