I am using a command of a Command Prompt in my application. Application able to run and execute that command of a Command Prompt when I run my application using Visual Studio while debugging but when I take my application's executable file(.exe) and save in my pc drive and then run the file it skips the Command Prompt Command. I research for the topic and get this :
CMD command not running in console
but no success.
My code :
Process process = new Process();
process.StartInfo.FileName = #"cmd.exe";
process.StartInfo.WorkingDirectory = sentencesList;
process.StartInfo.Arguments = "/C findstr /V /I \"" + ListOfSomeWords + "\" " + sentencesList+ ">" + filteredList;
process.Start();
process.WaitForExit();
process.Close();
process.Dispose();
Command remove the sentence/line from a text file(sentenceList) which contains a word(ListOfSomeWords) and make a another text file(filteredList) which contains only those line which not contains any of word specify in ListOfSomeWords.
You are not escaping filteredList with quotes. If it contains a space, then it could not be interpreted correctly by cmd.exe .
Also make sure that you are setting WorkingDirectory to an existing directory path(variable name file_path looks suspicious).
Related
I'm trying to reboot a remote computer by button click.
I have been checking multiple posts and i've tried numerous different Process.Start methods but none seem to work.
Most posts just say use this:
Process.Start("shutdown", "-r -f -m" + BxSD.Text + "-t 00");
But i've tried this and variations of it, but all that happens is the cmd window opens for a second then closes and nothing happens.
Any help is appreciated.
UPDATE: I found out that it could not reboot the device based on the hostname and can only reboot by IP address only.
The final iteration of my command is as follows:
Process.Start("cmd.exe", "/k SHUTDOWN /r /f /m \\" + BxSD.Text);
But now however, the cmd prompt window doesn't exit after sending the command.
Any thoughts?
This works for me:
Process process = new();
ProcessStartInfo startInfo = new()
{
WindowStyle = ProcessWindowStyle.Hidden,
FileName = "cmd.exe",
Arguments = $" ___your command here___ ",
UseShellExecute = true,
Verb = "runas"
};
process.StartInfo = startInfo;
process.Start();
I'm writing a a small WPF app that will help me run JMeter tests in non-GUI mode without the hassle of typing JMeter commands and file paths into the console every time I want to run a test. This means that my WPF app needs to open up CMD in location where my JMeter is intalled and then pass an argument (command line).
This is how I open up CMD with a specific path and arguments I pass:
private void RunScript()
{
var process = new Process();
var startInfo = new ProcessStartInfo
{
WorkingDirectory = "#D:\\Programi\\apache-jmeter-5.1\\bin",
WindowStyle = ProcessWindowStyle.Normal,
FileName = "cmd.exe",
Arguments = "/K jmeter -n -t " + scriptDirectoryPath
};
process.StartInfo = startInfo;
process.Start();
}
As you can see, the path where the CMD needs to open is "D:\Projekti\JMeteor\JMeteorApp\JMeteorApp\bin", but the path in CMD is "D:\Projekti\JMeteor\JMeteorApp\JMeteorApp\bin\Debug>"
How do I remove the "Debug" portion in CMD path? I tried switching solution configuration to "Release" but that just replaces "Debug" with "Release" in path.
Do not write the # inside the string
use either
WorkingDirectory = "D:\\Programi\\apache-jmeter-5.1\\bin"
or (I guess you wanted to use the # for a verbatim string)
WorkingDirectory = #"D:\Programi\apache-jmeter-5.1\bin"
I am developing a Windows Form program that has callings to ffmpeg library through the class Process.
It works fine when I run it with the Debug in Visual Studio 2013. But when I install the program and I invoke the operation that call to the ffmpeg Process, it doesn't work. The cmd screen appears an disappears and nothing happens.
I have tried to know what can be happening getting a log file with the output of ffmpeg, in case it was a problem in the ffmpeg libraries. However, after executing it the log is empty, what means that the ffmpeg command has not been executed.
Can someone help me, please?
The code is this:
ProcessStartInfo startInfo = new ProcessStartInfo();
startInfo.FileName = "cmd.exe";
startInfo.Arguments = "/c " + ffmpegPath + " " + commandArguments;
using (Process processTemp = new Process())
{
processTemp.StartInfo = startInfo;
processTemp.EnableRaisingEvents = true;
processTemp.Start();
processTemp.WaitForExit();
}
I am invoking to cmd.exe (not directly ffmpeg.exe) because in the arguments sometimes there can be a pipe (that is why the command starts with "/c").
Are you sure this isn't a privileges issue when trying to execute the cmd.exe (e.g you need administrator privileges)
try adding
startInfo.Verb = "runas";
Paul
Hmm its not a path issue with spaces in file/directory names is it? For ffmpegPath or one of your command parameters (if a file path). Surround all file paths with ' like below.
Try surrounding any file paths with '
startInfo.Arguments = "/c '" + ffmpegPath + "' " + commandArguments;
Also you could try adding /K to the cmd command call to stop if from closing the command prompt when it finishes. It might tell you the error before it closes the window but you wont see it if it closes so quickly
Good luck :)
Paul
So i want to run a bunch of different batch commands that do different things. Currently my software works by running the .bat file that is included with the software, but i want to simply remove the syntax from the .bat file and integrate it inside my software so when i click the button it runs the code directly and not launch the .bat file.
Problem is how do i run multiple batch commands, not only that but also wait for each command to finish running just like batch does?
Overall i wanna execute batch commands like stop services, delete registry files, delete folders and software.
PS : this is a C# Forms application ( so i click one button and it all executes )
Currently i have : with examples
var proc1 = new ProcessStartInfo();
string stopservices =
"net stop \"serviceexample1\"" +
"net stop \"serviceexample2\"";
string deleteregistry =
"REG ADD HKEY_LOCAL_MACHINE\\SYSTEM\\EXAMPLE\\.. / t REG_DWORD / v Start / d 0x00000004 / f" +
"rem REG ADD HKEY_LOCAL_MACHINE\\SYSTEM\\EXAMPLE\\.. /t REG_SZ /v Start /d 5 /f";
string deletesoftware =
"MsiExec.exe /X{EXAMPLESOFTWARE} /quiet" +
"MsiExec.exe /X{EXAMPLESOFTWARE} /quiet";
string removedirectories =
"rmdir /s /q C:\\Program Files\\EXAMPLE" +
"rmdir /s /q C:\\Program Files(x86)\\EXAMPLE";
proc1.UseShellExecute = true;
proc1.WorkingDirectory = #"C:\Windows\System32";
proc1.FileName = #"C:\Windows\System32\cmd.exe";
proc1.Verb = "runas";
proc1.Arguments = "/c " + stopservices + deleteregistry + deletesoftware + removedirectories;
proc1.WindowStyle = ProcessWindowStyle.Normal;
System.Diagnostics.Process processwait = System.Diagnostics.Process.Start(proc1);
while (!processwait.HasExited && processwait.Responding)
{
System.Threading.Thread.Sleep(100);
}
I have tried to look in internet how to run batch code directly from c# and the only way i found how to do it was using a string. But i dont think that will wait for each command to finish to begin the next one because a string is just a line of text so it will take it all at once.
I also wanna make sure that when the code was successfully executed the command prompt will stay untill i close it ( like pause in batch ).
Sorry if this is a simple question, i am a student with less then half a year experience in c#
when i run copy command in my c# code it produce no error or exception because it is not finding the parts path i do not know how to give full directory path or path of every part which i am joining.actually i am merging file parts to a single file using coy/b by using this code...
string strCmdText;
strCmdText = "/C copy/b test.txt.10485760.0000.part" +
"test.txt.10485760.0001.part" +
"test.txt.10485760.0002.part" +
"test.txt.10485760.0003.part" +
"test.txt.10485760.0004.part" +
"test.txt.10485760.0005.part test.txt";
System.Diagnostics.Process.Start("CMD.exe", strCmdText);
You can specify the path of your files as the working path of the process. For example:
var startInfo = new System.Diagnostics.ProcessStartInfo
{
WorkingDirectory = #"THE\PATH\OF\FILES",
WindowStyle = System.Diagnostics.ProcessWindowStyle.Normal,
FileName = "cmd.exe",
Arguments = "YOUR COMAND HERE";
};
Process process = Process.Start(startInfo);
For your command, note that you can copy all files into one using a wildcard for the parts:
copy *.part test.txt
/b is for binary data, so i think is not needed in your case.
You can also set other properties for a process, for more info check the doc: ProcessStartInfo.
For such a complex task, I would use the process as a bash rather then just an execution tool.
Create a Process
Redirect it's streams (Input, Output)
With a StreamReader and StreamWriter u can now access the cmd
Now just control it the way u need it, like setting the working directory to the path and do ur command.
If this is not what u want, u can allways set the working directory on the ProcessStartInfor -> Link