I have written a C# tool that will launch an application by running a command in the command prompt for me several times, one after the other. The piece of code doing this for me is as follows:
System.Diagnostics.Process.Start("CMD.exe", strCmdText);
So I have this in a loop and all is good. However each time it executes a new command window is opened, which isnt ideal. Is there a way I can close the command window after the command has executed, or better still, not have the command window open at all? Any help is really appreciated!!
Yes, you have to use the ProcessStartInfo class for doing this:
Process.Start(
new ProcessStartInfo("CMD.exe", strCmdText)
{
CreateNoWindow = true
});
Use ProcessStartInfo to start the process (Start has an overload that takes a ProcessStartInfo).
Set the following properties:
var psi = new ProcessStartInfo("cmd.exe", strCmdText);
psi.CreateNoWindow = true;
psi.UseShellExecute = false;
Process.Start(psi);
ProcessStartInfo allows you to specify no window.
using (var p = new Process())
{
p.StartInfo = new ProcessStartInfo
{
FileName = "CMD.exe",
Arguments = strCmdText,
CreateNoWindow = true,
UseShellExecute = false
};
p.Start();
p.WaitForExit();
p.Close();
}
Related
I need to copy a file from one directory to another and do something with that file. I need to copy it with cmd, rather than File.Copy(), because I need the copy to be done as a part of ProcessStartInfo.
You can use this code and change startInfo.Arguments, but /C should be!
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 copy example.txt backup.txt";
process.StartInfo = startInfo;
process.Start();
You can create a bat-file to copy one or multiple files (using *). Then execute the batch file.
string batFileName = #"C:\{filePath}\copy.bat";
System.IO.File.WriteAllText(batFileName, #"copy {fileName}.{extension} {destination-filePath}");
System.Diagnostics.Process.Start(batFileName);
I was able to formulate this answer using the DOS Copy syntax along with this Stack Overflow QA
Start cmd window and run commands inside
var startInfo = new ProcessStartInfo {
FileName = "cmd.exe",
RedirectStandardInput = true,
RedirectStandardOutput = true,
UseShellExecute = false,
CreateNoWindow = true
};
var process = new Process {StartInfo = startInfo};
process.Start();
process.StandardInput.WriteLine(#"copy c:\Source\Original.ext D:\Dest\Copy.ext");
process.StandardInput.WriteLine("exit");
process.WaitForExit();
I'm trying to write a C# console application that will launch runas.exe through cmd then run another application as that user. I've taken one of the suggestions listed below (and added a little bit) as it seems the most promising.
Process cmd = new Process();
ProcessStartInfo startinfo = new ProcessStartInfo("cmd.exe", #"/K C:\Windows\System32\runas.exe /noprofile /user:DOMAIN\USER'c:\windows\system32\notepad.exe\'")
{
RedirectStandardInput = true,
UseShellExecute = false
};
cmd.StartInfo = startinfo;
cmd.Start();
StreamWriter stdInputWriter = cmd.StandardInput;
stdInputWriter.Write("PASSWORD");
cmd.WaitForExit();
When I launch the application it asks for the password before the command itself causing there to be an error with runas.exe
I'm pretty sure UseShellExecute = false is causing the error but the StreamWriter doesn't work without it so I'm not sure what to do.
The /c argument runs comman line and terminates, so you could not see results (and it's a large C), see her : http://ss64.com/nt/cmd.html
Try to use "/K".
I did it with your command and i see results in another window.
ProcessStartInfo startInfo = new ProcessStartInfo("cmd", "/K ping PC -t");
Process.Start(startInfo);
Your process should have RedirectStandardInput = true
var p = new Process();
var startinfo = new ProcessStartInfo("cmd.exe", #"/C C:\temp\input.bat")
{
RedirectStandardInput = true,
UseShellExecute = false
};
p.StartInfo = startinfo;
p.Start();
StreamWriter stdInputWriter = p.StandardInput;
stdInputWriter.Write("y");
This program launches input.bat, and then sends the value y to it's standard input.
For completeness, input.bat example:
set /p input=text?:
echo %input%
I found a solution in the following post. I had to forgo most of the structure I was working with but I can now successfully run notepad as my desired user with the code listed below.
var pass = new SecureString();
pass.AppendChar('p');
pass.AppendChar('a');
pass.AppendChar('s');
pass.AppendChar('s');
pass.AppendChar('w');
pass.AppendChar('o');
pass.AppendChar('r');
pass.AppendChar('d');
var runFileAsUser = new ProcessStartInfo
{
FileName = "notepad",
UserName = "username",
Domain = "domain",
Password = pass,
UseShellExecute = false,
RedirectStandardOutput = true,
RedirectStandardError = true
};
Process.Start(runFileAsUser);
I am having trouble executing an external console application using Process.Start
Process p = new Process();
p.StartInfo.WorkingDirectory = "dump";
p.StartInfo.FileName = "test.exe";
p.StartInfo.Arguments = s;
p.Start();
When the argument that p generates executes, the external application crashes, although if I copy the exact same argument in a command line window it runs fine.
So my question instead how would I create a new instance of a command window and then add the command test.exe + s to run?
So effectively I am launching cmd and then adding my arguments on to it
If you want to run test.exe prm1 prm2 via cmd, use cmd.exe /c test.exe prm1 prm2. Though I don't really understand what this has to do with the crashes. Sounds like your problem is with test.exe - find out what's causing it to crash, and that will help you fix your C# code so that you don't need the cmd.
One of the places I would examine is the working directory. When you set it to "dump", are you sure the current directory is what you expect? Try using a full path first. It's possible that test.exe happens to be in the system path so it gets executed, but its working directory is not what it expects, and this causes it to crash.
try this:
ProcessStartInfo processToRunInfo = new ProcessStartInfo();
processToRunInfo.Arguments = "Arguments");
processToRunInfo.CreateNoWindow = true;
processToRunInfo.WorkingDirectory = "C:\\yourDir\\";
processToRunInfo.FileName = "test.exe";
//processToRunInfo.CreateNoWindow = true;
//processToRunInfo.WindowStyle = ProcessWindowStyle.Hidden;
Process process = new Process();
process.StartInfo = processToRunInfo;
process.Start();
Process p = new Process();
p.StartInfo.WorkingDirectory = "/full/path/to/dump";
p.StartInfo.FileName = "/full/path/to/test.exe";
p.StartInfo.Arguments = s; // will call 'text.exe s'
p.Start();
Take a look at MSDN.
You need to Create an instance of the StartInfo class and user Start() such as
ProcessStartInfo startInfo = new ProcessStartInfo("IExplore.exe");
startInfo.WindowStyle = ProcessWindowStyle.Minimized;
Process.Start(startInfo);
startInfo.Arguments = "www.example.com";
Process.Start(startInfo);
Try it!
Rewriting your code would look something like this:
ProcessStartInfo startInfo = new ProcessStartInfo("test.exe");
startInfo.WindowStyle = ProcessWindowStyle.Minimized;
startInfo.WorkingDirectory = "dump";
startInfo.Arguments = "s";
Process.Start(startInfo);
I need to start the command window with some arguments and run more commands inside.
For example, launch a test.cmd and run mkdir.
I can launch the test.cmd with processstartinfo , but i am not sure how to run further commands. Can I pass further arguments to the test.cmd process?
How do I go about this?
Unable to add comments to answer... SO writing here.
Andrea, This is what I was looking for. However the above code doesnt work for me.
I am launching a test.cmd which is new command environment (like razzle build environment) and I need to run further commands.
psi.FileName = #"c:\test.cmd";
psi.Arguments = #"arg0 arg1 arg2";
psi.RedirectStandardInput = true;
psi.RedirectStandardOutput = true;
psi.CreateNoWindow = true;
psi.UseShellExecute = false;
Process p = new Process();
p.StartInfo = psi;
p.Start();
p.StandardInput.WriteLine(#"dir>c:\results.txt");
p.StandardInput.WriteLine(#"dir>c:\results2.txt");
You can send further commands to cmd.exe using the process
standard input. You have to redirect it, in this way:
var startInfo = new ProcessStartInfo
{
FileName = "cmd.exe",
RedirectStandardInput = true,
RedirectStandardOutput = true,
UseShellExecute = false,
CreateNoWindow = true
};
var process = new Process {StartInfo = startInfo};
process.Start();
process.StandardInput.WriteLine(#"dir>c:\results.txt");
process.StandardInput.WriteLine(#"dir>c:\results2.txt");
process.StandardInput.WriteLine("exit");
process.WaitForExit();
Remember to write "exit" as your last command, otherwise the cmd process doesn't terminate correctly...
The /c parameter to cmd.
ProcessStartInfo start = new ProcessStartInfo("cmd.exe", "/c pause");
Process.Start(start);
(pause is just an example of what you can run)
But for creating a directory you can do that and most other file operations from c# directly
System.IO.Directory.CreateDirectory(#"c:\foo\bar");
Start a cmd from c# is useful only if you have some big bat-file that you don't want to replicate in c#.
What are you trying to achieve? Do you actually need to open a command window, or do you need to simply make a directory, for example?
mkdir is a windows executable - you can start this program in the same way you start cmd - there's no need to start a command window process first.
You could also create a batch file containing all the commands you want to run, then simply start it using the Process and ProcessStartInfo classes you're already using.
How come this doesn't work?
var startInfo = new ProcessStartInfo
{
FileName = "cmd.exe",
RedirectStandardInput = true,
RedirectStandardOutput = true,
UseShellExecute = false,
CreateNoWindow = false
};
var process = new Process { StartInfo = startInfo };
process.Start();
process.StandardInput.WriteLine(#" dir");
process.WaitForExit();
But it doesn't work -meaning the java code is not executed.
Although the batch file runs fine when clicked in Windows explorer or when run in command line ..
Since this works fine when the batch file is a single DOS command, I think this is somehow related to the fact that the Java code needs ~20 minutes to run.
I'm using the following code
var si = new ProcessStartInfo();
si.CreateNoWindow = true;
si.FileName = batchFileName;
si.UseShellExecute = false;
Process.Start(si);
What am I doing wrong?
Set UseShellExecute to true, so it loads cmd.exe to run the batch file.
Check this - a batch file wrapper of ProcessStartInfo:
C:\>ProcessStartJS.bat "cmd.exe" -arguments "/c pause" -style Minimized -priority High -newWindow yes -useshellexecute yes
Started: cmd.exe /c pause
PID:6540
As Lucas Jones mentioned in the comments, if you don't want to use ShellExecute, do it like this:
string fullBatPath = #"C:\path with space\file.bat";
var process = new Process()
{
StartInfo = new ProcessStartInfo
{
FileName = "cmd.exe",
Arguments = $"cmd /C \"{fullBatPath}\"",
UseShellExecute = false,
CreateNoWindow = true,
}
};
process.Start();