How to run some CMD commands one by one (As admin) - c#

I have a few commands I want to run through the CMD (admin):
ipconfig/flushdns
ipconfig /registerdns
ipconfig /release
ipconfig /renew
netsh winsock reset
Anyone can tell me how?

Question of is sort answered here: Running cmd commands with Administrator rights
The code could be duplicated (as shown below):
System.Diagnostics.Process process = new System.Diagnostics.Process();
System.Diagnostics.ProcessStartInfo startInfo = new
System.Diagnostics.ProcessStartInfo();
startInfo.WindowStyle = System.Diagnostics.ProcessWindowStyle.Hidden;
startInfo.FileName = "cmd.exe";
startInfo.Arguments = "/C ipconfig /flushdns";
startInfo.Verb = "runas";
process.StartInfo = startInfo;
process.Start();
System.Diagnostics.Process process2 = new System.Diagnostics.Process();
System.Diagnostics.ProcessStartInfo startInfo2 = new
System.Diagnostics.ProcessStartInfo();
startInfo2.WindowStyle = System.Diagnostics.ProcessWindowStyle.Hidden;
startInfo2.FileName = "cmd.exe";
startInfo2.Arguments = "/C ipconfig /renew";
startInfo2.Verb = "runas";
process2.StartInfo = startInfo2;
process2.Start();

If you are looking to use powershell, you can create a script which will include your commands something like this:
& "command 1";
& "command 2";
etc.

Related

How to run 2 files c# without opening window

I got 1 mapper (.exe) and 1 driver (.sys) and i want to do so that when i execute them i want to run mapper as admin with the driver and not create a window (keep it hidden). can anyone help. I have the following code down but nor does it run the mapper as admin with the spoof and it still also creates a window! help!
string map = "C:\\SCSpoofer\\mapper.exe";
Process myProcess = new Process();
myProcess.StartInfo.UseShellExecute = false;
myProcess.StartInfo.CreateNoWindow = true;
myProcess.StartInfo.Verb = "runas";
myProcess = Process.Start(map, sys);
System.Threading.Thread.Sleep(150);
myProcess.Kill();```
// Create a Process to launch a command window (hidden) to create the item templates
Process process = new Process();
ProcessStartInfo startInfo = new ProcessStartInfo();
startInfo.WindowStyle = ProcessWindowStyle.Hidden;
startInfo.FileName = "cmd.exe";
startInfo.WorkingDirectory = Project.ServicesFolder;
startInfo.Arguments = "/C " + CreateServices;
process.StartInfo = startInfo;
process.Start();
This is from my open source project DataTier.Net github.com/DataJuggler/DataTier.Net

How to run cmd command under administrator rights? [duplicate]

This question already has answers here:
How to start a Process as administrator mode in C# [duplicate]
(9 answers)
Closed 8 years ago.
How I can run below command under administrator approval in visual c#? Also I need to hide console windows while running console.
Thanks.
private void button5_Click(object sender, EventArgs e)
{
Process process = new Process();
ProcessStartInfo startInfo = new ProcessStartInfo();
startInfo.FileName = "cmd.exe";
startInfo.Arguments = "/C netsh wlan set hostednetwork mode=allow ssid=HotSpot key=12345678";
startInfo.Verb = "runas";
startInfo.UseShellExecute = true;
startInfo.WindowStyle = ProcessWindowStyle.Hidden;
process.StartInfo = startInfo;
process.Start();
Process wifiStart = new Process();
ProcessStartInfo wifiStartInfo = new ProcessStartInfo();
wifiStartInfo.FileName = "netsh.exe";
wifiStartInfo.Arguments = "/C wlan start hostednetwork";
wifiStartInfo.Verb = "runas";
wifiStartInfo.UseShellExecute = true;
wifiStartInfo.WindowStyle = ProcessWindowStyle.Hidden;
process.StartInfo = wifiStartInfo;
wifiStart.Start();
}
Process.Start(new ProcessStartInfo {
FileName = "netsh",
Arguments = "wlan set hostednetwork mode=allow ssid=HotSpot key=12345678",
Verb = "runas",
UseShellExecute = true,
WindowStyle = ProcessWindowStyle.Hidden
});
This starts a process using the "runas" verb, which makes the shell try to execute it in elevated privileges mode. But we actually need the shell to be involved in this in the first place, hence the UseShellExecute = true value.
The last property tells the shell to hide the new process' window, but I'm not sure this will work for a console program.

Executing batch file command within C#

I have the following code in my C# application which loaded a batch file silently using command prompt and executed and returned the result to a string:
System.Diagnostics.Process proc = new System.Diagnostics.Process();
proc.StartInfo.FileName = #"C:\files\send.bat";
proc.StartInfo.RedirectStandardError = false;
proc.StartInfo.RedirectStandardOutput = true;
proc.StartInfo.UseShellExecute = false;
proc.StartInfo.CreateNoWindow = true;
proc.Start();
string strGetInfo = proc.StandardOutput.ReadToEnd();
strCMDOut = strGetInfo.Substring(strGetInfo.Length - 5, 3);
proc.WaitForExit();
I am trying to avoid my application going out to a different file to execute the batch file, rather I wanted to embed it inside my application. So I changed the above code to this:
System.Diagnostics.Process proc = new System.Diagnostics.Process();
System.Diagnostics.ProcessStartInfo startInfo = new System.Diagnostics.ProcessStartInfo();
startInfo.WindowStyle = System.Diagnostics.ProcessWindowStyle.Hidden;
startInfo.FileName = "cmd.exe";
startInfo.Arguments = "#ECHO ON java com.this.test567 send";
startInfo.RedirectStandardOutput = true;
startInfo.UseShellExecute = false;
proc.StartInfo = startInfo;
proc.Start();
string strGetInfo = proc.StandardOutput.ReadToEnd();
strCMDOut = strGetInfo.Substring(strGetInfo.Length - 5, 3);
When the code executes, I can see the command prompt window for a brief moment before it closes and the execution is not working correctly. How can I fix the issue?
Instead of using cmd.exe just use java directly, you should also redirect standard error and check that after the process ends.
System.Diagnostics.Process proc = new System.Diagnostics.Process();
proc.StartInfo.FileName = #"java.exe";
proc.StartInfo.Arguments = "com.this.test567";
proc.StartInfo.RedirectStandardError = true;
proc.StartInfo.RedirectStandardOutput = true;
proc.StartInfo.UseShellExecute = false;
proc.StartInfo.CreateNoWindow = true;
proc.Start();
string strGetInfo = proc.StandardOutput.ReadToEnd();
if(string.IsNullOrEmpty(strGetInfo))
strGetInfo = proc.StandardError.ReadToEnd();
proc.WaitForExit();
Note that by calling cmd directly, you're effectively making a batch script with whatever you use for in the Arguments Property. Like a .bat file, the command window closes as soon as it's done. To fix this, add a pause command to the end.
startInfo.Arguments = "#ECHO ON java com.this.test567 send\npause";
& seperates commands on a line.
&& executes this command only if previous command's errorlevel is 0.
|| (not used above) executes this command only if previous command's errorlevel is NOT 0
> output to a file
>> append output to a file
< input from a file
| output of one command into the input of another command
^ escapes any of the above, including itself, if needed to be passed to a program
so seperate commands with &
"/k #ECHO ON&java com.this.test567&send"
/k keeps a window open.
so you'll get in cmd
cmd /k #ECHO ON&java com.this.test567&send

Run CMD command without displaying it?

I have created a Process to run command in CMD.
var process = Process.Start("CMD.exe", "/c apktool d app.apk");
process.WaitForExit();
How can I run this command without displaying actual CMD window?
You can use the WindowsStyle-Property to indicate whether the process is started in a window that is maximized, minimized, normal (neither maximized nor minimized), or not visible
process.StartInfo.WindowStyle = ProcessWindowStyle.Hidden
Source:
Property:MSDN
Enumartion: MSDN
And change your code to this, becaeuse you started the process when initializing the object, so the properties (who got set after starting the process) won't be recognized.
Process proc = new Process();
proc.StartInfo.FileName = "CMD.exe";
proc.StartInfo.Arguments = "/c apktool d app.apk";
proc.StartInfo.WindowStyle = ProcessWindowStyle.Hidden;
proc.Start();
proc.WaitForExit();
There are several issues with your program, as pointed out in the various comments and answers. I tried to address all of them here.
ProcessStartInfo psi = new ProcessStartInfo();
psi.FileName = "apktool";
//join the arguments with a space, this allows you to set "app.apk" to a variable
psi.Arguments = String.Join(" ", "d", "app.apk");
//leave it to the application, not the OS to launch the file
psi.UseShellExecute = false;
//choose to not create a window
psi.CreateNoWindow = true;
//set the window's style to 'hidden'
psi.WindowStyle = ProcessWindowStyle.Hidden;
var proc = new Process();
proc.StartInfo = psi;
proc.Start();
proc.WaitForExit();
The main issues:
using cmd /c when not necessary
starting the app without setting the properties for hiding it
Try this :
proc.StartInfo.CreateNoWindow = true;
proc.StartInfo.WindowStyle = ProcessWindowStyle.Hidden;
proc.WaitForExit();
ProcessStartInfo startInfo = new ProcessStartInfo();
startInfo.CreateNoWindow = true;
startInfo.UseShellExecute = false;
startInfo.FileName = "dcm2jpg.exe";
startInfo.WindowStyle = ProcessWindowStyle.Hidden;
startInfo.Arguments = "-f j -o \"" + ex1 + "\" -z 1.0 -s y " + ex2;

C# New process window does not hide [duplicate]

This question already has answers here:
Closed 10 years ago.
Possible Duplicate:
Hide Command Window in C# Application
in the console app, I want to run the cmd command, but a new window is being created, why ?
Process process = new Process();
ProcessStartInfo startInfo = new ProcessStartInfo();
startInfo.WindowStyle = ProcessWindowStyle.Hidden;
startInfo.CreateNoWindow = true;
startInfo.UseShellExecute = false;
startInfo.FileName = "cmd.exe";
startInfo.WorkingDirectory = #"C:\m_f";
startInfo.Arguments = "/c START _creator.bat";
process.StartInfo = startInfo;
process.Start();
edit
I had to change some code to:
startInfo.FileName = #"C:\m_f\_creator.bat";
startInfo.WorkingDirectory = #"C:\m_f\";
startInfo.Arguments = "some_args";
now it works
start spawns a new window, regardless of whether the parent shell has a window or not.
To run a batch file cmd /c foo.cmd suffices, or even foo.cmd.

Categories

Resources