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.
Related
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
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.
Is it possible to redirect just stdin but also allow stdout to be written to the console?
I have a process which starts child processes and needs to read the output of those processes, but also display it in the console. Is it possible? I tried to just create my own console process but I can't write to it unless I UseShellExecute and the purpose is to show the output in the console.
protected void startp() {
Process p = new Process();
ProcessStartInfo psi = new ProcessStartInfo();
psi.FileName = "cmd.exe";
psi.Arguments = "/k";
//Can't redirect output without UseShellExecute = false
psi.RedirectStandardInput = true;
psi.UseShellExecute = false;
psi.CreateNoWindow = false;
p.StartInfo = psi;
p.Start();
//Can't write to StandardInput with UseShellExecute = true
p.StandardInput.WriteLine("#ECHO OFF");
p.StandardInput.WriteLine("echo | set /p=Child Process");
Console.ReadLine();
p.StandardInput.WriteLine("exit");
}
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;
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.