How to hide the console from within this code? Currently the cmd console is shown everytime I run this code.
protected override void OnStart(string[] args)
{
String applicationName = "cmd.exe";
// launch the application
ApplicationLoader.PROCESS_INFORMATION procInfo;
ApplicationLoader.StartProcessAndBypassUAC(applicationName, out procInfo);
}
How can I execute a *.bat file from here? Can I simply can substitute the "cmd.exe" with "xxx.bat"?
Add a System Reference to the code;
using System Diagnostics;
Then use this code to Hide the CMD Window and run.
Process cmd = new Process();
cmd.StartInfo.FileName = "cmd.exe";
cmd.StartInfo.WindowStyle = ProcessWindowStyle.Hidden;
cmd.StartInfo.Arguments = "Your arguments";
cmd.Start();
Try it with the Process Class instead of the ApplicationLoader (I´ve never heard of that class, is it a custom class?)
Code Example:
using System.Diagnostics;
Process pr = new Process();
pr.StartInfo.FileName = "cmd.exe";
pr.StartInfo.WindowStyle = ProcessWindowStyle.Hidden;
pr.Arguments = "xxx.bat";
pr.Start();
Related
I'm working on a small C# app that will import a power plan to the user's PC and set it as active.
It working perfectly with a .bat file when the .pow file is in the same folder and I'm running commands:
powercfg -import "%~dp0\Optimized.pow"
powercfg /setactive 62ffd265-db94-4d48-bb7a-183c87641f85
Now, in C# I tried this:
Process cmd = new Process();
cmd.StartInfo.FileName = "powercfg";
cmd.StartInfo.Arguments = "-import \"%~dp0\\Optimized\"";
cmd.StartInfo.Arguments = "powercfg /setactive 62ffd265-db94-4d48-bb7a-183c87641f85";
cmd.Start();
//and this:
private void button1_Click(object sender, EventArgs e)
{
Process cmd = new Process();
cmd.StartInfo.FileName = "cmd.exe";
cmd.StartInfo.RedirectStandardInput = true;
cmd.StartInfo.RedirectStandardOutput = true;
cmd.StartInfo.CreateNoWindow = true;
cmd.StartInfo.UseShellExecute = false;
cmd.Start();
cmd.StandardInput.WriteLine("powercfg -import \"%~dp0\\Optimized\"");
cmd.StandardInput.WriteLine("powercfg /setactive 6aa8c469-317b-45d9-a69c-f24d53e3aff5");
cmd.StandardInput.Flush();
cmd.StandardInput.Close();
cmd.WaitForExit();
Console.WriteLine(cmd.StandardOutput.ReadToEnd());
}
But the program doesn't see the .pow file in the project folder (I actually tried to put it in each and every folder in the project).
How it can be implemented to let the powercfg see the file?
Any help is much appreciated!
Thanks!
You could try something like this:
var cmd = new Process {StartInfo = {FileName = "powercfg"}};
using (cmd) //This is here because Process implements IDisposable
{
var inputPath = Path.Combine(Environment.CurrentDirectory, "Optimized.pow");
//This hides the resulting popup window
cmd.StartInfo.CreateNoWindow = true;
cmd.StartInfo.WindowStyle = ProcessWindowStyle.Hidden;
//Prepare a guid for this new import
var guidString = Guid.NewGuid().ToString("D"); //Guid without braces
//Import the new power plan
cmd.StartInfo.Arguments = $"-import \"{inputPath}\" {guidString}";
cmd.Start();
//Set the new power plan as active
cmd.StartInfo.Arguments = $"/setactive {guidString}";
cmd.Start();
}
This fixes the Arguments parameter that is being overwritten/used twice, as well as correctly disposes of the cmd variable. Additional lines added to hide the resulting pop-up window, and for generating the Guid upfront and specifying it as part of the command line.
Your first snippet does not work because you're reassigning cmd.StartInfo.Arguments before executing the process. The first assignment is lost when you throw it out in favor of the second assignment.
The first snippet most likely doesn't work because when you set cmd.startInfo.FileName to just a filename with no path, it will search only the directory of your C# app's .exe (likely in project/bin/Debug/). Since the FileName is cmd.exe and there is probably no cmd.exe in your project folder, it can't find anything.
You may also consider setting cmd.StartInfo.WorkingDirectory to an appropriate directory with your .pow file so that your relative paths will resolve correctly.
The second app is a console application and I want to see it's output window.
I know how to use Process.Start() but it doesn't show the console window for the app.
This is what I have tried:
Process.Start("MyApp.exe", "arg1 arg2");
So how to do it?
Perhapse this helps:
ProcessStartInfo info = new ProcessStartInfo(fileName, arg);
info.CreateNoWindow = false;
info.UseShellExecute = true;
Process processChild = Process.Start(info);
I figured it out. I have to run cmd command with /k argument (to keep the console window open) and then my whole command-line:
var command = "MyApp.exe arg1 arg2";
ProcessStartInfo processStartInfo = new ProcessStartInfo("cmd", "/k " + command);
processStartInfo.UseShellExecute = false;
Process process = new Process();
process.StartInfo = processStartInfo;
process.Start();
//In case you need the output. But you have to wait enough for the output
//string text = process.StandardOutput.ReadToEnd();
I need to write a windows form app, WPF, or console application that should run this from a command prompt:
#echo off
echo test
or
del c:\b.txt
The c# code needs to be on a button but It's not important now.
I tried this:
using System;
using System.Diagnostics;
using System.ComponentModel;
private void button1_Click(object sender, EventArgs e)
{
Process process = new Process();
process.StartInfo.FileName = "cmd.exe";
//process.StartInfo.WorkingDirectory = "c:\temp";
//process.StartInfo.Arguments = "somefile.txt";
Process.Start("cmd", "/dir");
}
but i can't put my CMD code in this ^ code.
I believe what you want is this:
Process p = new Process();
ProcessStartInfo psi = new ProcessStartInfo("cmd.exe");
// add /C for command, a space, then command
psi.Arguments = "/C dir";
p.StartInfo = psi;
p.Start();
The above code allows you to execute the commands directly in the cmd.exe instance. For example, to launch command prompt and see a directory, you assign the Arguments property of the ProcessStartInfo instance to "/C dir", which means execute the command called dir.
Another approach you could use is seen here:
// put your DOS commands in a batch file
ProcessStartInfo startInfo = new ProcessStartInfo("action.bat");
startInfo.UseShellExecute = true;
startInfo.WorkingDirectory = "C:\\dir";
Process.Start(startInfo);
Put the above code in your click event.
Create a batch file with your commands using instructions found at http://www.ehow.com/how_6534808_create-batch-files.html
In my code example, if you name the batch file 'action.bat' and place it in th c:dir folder, everything will work the way you want.
Cheers :)
I would like to make an application using C# form. Form has multiple label to show information and a button to click and show that information on that labels. All of the labels will show information which can be found using cmd. cmd will not show when program executes.
For Example:
If I need my motherboard information. It can be done using cmd commands "wmic baseboard get product,manufacturer"(without quotes). I would like to show same information on my C# form label by clicking on button. That need to be done hiding cmd windows.
You can use ProcessStartInfo and Process classes to run this application and redirect standard output to your own method.
Setting RedirectStandardOutput will make Process raise OutputDataReceived event which you can easily handle.
P.S. Pay attention to using Arguments to provide arguments.
var psi = new ProcessStartInfo(#"wmic");
psi.Arguments = #"baseboard get product,manufacturer";
psi.WindowStyle = ProcessWindowStyle.Hidden;
psi.UseShellExecute = false;
psi.RedirectStandardOutput = true;
string val = String.Empty;
var p = Process.Start(psi);
p.BeginOutputReadLine();
p.OutputDataReceived += delegate(object sender, DataReceivedEventArgs eventArgs)
{
val += eventArgs.Data + "\r\n";
};
p.WaitForExit();
MessageBox.Show(val); // Start parsing it here
Reference the code below against http://msdn.microsoft.com/en-us/library/system.diagnostics.processstartinfo%28v=vs.110%29.aspx and http://msdn.microsoft.com/en-us/library/system.diagnostics.process(v=vs.110).aspx
System.Diagnostics.ProcessStartInfo psi = new System.Diagnostics.ProcessStartInfo(yourCmd, yourCmdArguments);
psi.RedirectStandardOutput = false;
psi.WindowStyle = System.Diagnostics.ProcessWindowStyle.Hidden;
psi.UseShellExecute = true;
System.Diagnostics.Process externalProcess;
externalProcess = System.Diagnostics.Process.Start(psi);
externalProcess.WaitForExit();
I am trying to create an app that starts cmd.exe and send command. It is important that the command is visible on cmd. Here is that I got so far but it doesn't seem to be working. Any idea?
Process myProc = new Process();
myProc.StartInfo.FileName = "cmd.exe";
myProc.StartInfo.RedirectStandardInput = true;
myProc.StartInfo.RedirectStandardOutput = true;
myProc.StartInfo.UseShellExecute = false;
myProc.Start();
StreamWriter sendCommand = myProc.StandardInput;
sendCommand.WriteLine("run.exe --forever"); //I want this command to show up in cmd
When the code above is executed, run.exe is ran but the command does not show up in cmd.
What am I doing wrong?
Here's an addendum to my comment to make it clearer:
Process myProc = new Process();
myProc.StartInfo.FileName = "cmd.exe";
myProc.StartInfo.RedirectStandardInput = true;
//myProc.StartInfo.RedirectStandardOutput = true;
myProc.StartInfo.UseShellExecute = false;
myProc.Start();
System.IO.StreamWriter sendCommand = myProc.StandardInput;
sendCommand.WriteLine("run.exe --forever");
This will allow everything outputted by cmd to show in the cmd console.
why you are using the streamwriter ?
you can use the Arguments
myProc.StartInfo.Arguments="run.exe --forever";