Trying to use cmd via c# - c#

I am trying to use a cmd command prompt that works just fine when I use it in a normal cmd window via Windows --> Start --> cmd
My code looks like this
"C:\Program Files\Rapid-I\RapidMiner5\scripts\rapidminer.bat" -f
"C:\Users\user\.RapidMiner5\repositories\Local Repository\test.rmp"
That works just fine.
The problem is when I am trying to write this code in C# by opening a cmd window via C#
This is the c# code
ProcessStartInfo processStartInfo = new ProcessStartInfo("cmd", "/c \"C:\\Program Files\\Rapid-I\\RapidMiner5\\scripts\\rapidminer.bat\" -f \"C:\\Users\\user\\.RapidMiner5\\repositories\\Local Repository\\test.rmp\"");
You can see I quoted the same way like the code that work in the regular cmd
But I am getting a can't recognize program files.

Try this:
string fullPath = #"C:\Program Files\Rapid-I\RapidMiner5\scripts\rapidminer.bat";
ProcessStartInfo p = new ProcessStartInfo();
p.FileName = Path.GetFileName(fullPath);
p.WorkingDirectory = Path.GetDirectoryName(fullPath);
p.Arguments = #"/c -f ""C:\Users\user\.RapidMiner5\repositories\Local Repository\test.rmp""";
Process.Start(p);
I modified my original code to add arguments to your process.
One last thing you could try is putting your original command into a batch file and calling the batch file from your ProcessStartInfo object like so:
C#:
string fullPath = #"C:\Program Files\Rapid-I\RapidMiner5\scripts\run.bat";
ProcessStartInfo p = new ProcessStartInfo();
p.FileName = Path.GetFileName(fullPath);
p.WorkingDirectory = Path.GetDirectoryName(fullPath);
Process.Start(p);

Related

How use cmd command in c# program?

I want cmd command in my c# winforms program. I want use command to compress file with 7zip.
In normal cmd i use cd "C:\Program Files(x86)\7-Zip" & 7z.exe a -tzip "C:\xyz\test\txt" "C:\xyz\test\txt" -m0=BZip2 -mx5
string zip = #"/c C:\Program Files(x86)\7-Zip";
string file = #"/c C:\xyz\txt";
string conv = "/c & 7z.exe a -tzip";
string method = "/c -m0=BZip2 -mx5";
System.Diagnostics.ProcessStartInfo proc = new System.Diagnostics.ProcessStartInfo();
proc.FileName = #"C:\windows\system32\cmd.exe";
proc.Arguments = #"/c cd" + zip + conv + file + file + method;
System.Diagnostics.Process.Start(proc);
Doesn't work my code. How can i use this command in my program. I want compress file when i click in button
Something like this:
// File (exe) to start: combination of folder and exe
string fileName = Path.Combine(
// Let's not hardcode "C:\Program Files(x86)" and alike
Environment.GetFolderPath(Environment.SpecialFolder.ProgramFilesX86),
#"7-Zip",
#"7z.exe");
// If desired arguments are
// a -tzip "C:\xyz\test\txt" "C:\xyz\test\txt" -m0=BZip2 -mx5
// we can join them as
string arguments = string.Join(" ",
#"a",
#"-tzip", //TODO: you may want to have these values
#"""C:\xyz\test\txt""", // as variables like file, conv, method etc.
#"""C:\xyz\test\txt""",
#"-m0=BZip2",
#"-mx5");
ProcessStartInfo procInfo = new ProcessStartInfo() {
FileName = fileName, // We want to start fileName
Arguments = arguments, // With arguments
}
// Process is IDisposable, do not forget to Dispose HProcess handle
using (var process = Process.Start(procInfo)) {
// It's fire and forget implementation, if you want to wait for results
// add process.WaitForExit();
// process.WaitForExit(); // uncomment, if you want to pause
}
You seem to have made a mistake with your arguments.
Your current command line will be:
C:\windows\system32\cmd.exe /c cd/c C:\Program Files(x86)\7-Zip/c & 7z.exe a -tzip/c C:\xyz\txt/c C:\xyz\txt/c -m0=BZip2 -mx5
The spurious /c everywhere is not going to help and neither are the lack of spaces.
I'm guessing what you meant to run was these two commands in succession:
cd C:\Program Files(x86)\7-Zip
7z.exe a -tzip C:\xyz\txt C:\xyz\txt -m0=BZip2 -mx5
Which means you should change your code to:
string zip = #" C:\Program Files(x86)\7-Zip";
string file = #" C:\xyz\txt";
string conv = " & 7z.exe a -tzip";
string method = " -m0=BZip2 -mx5";
This will produce:
C:\windows\system32\cmd.exe /c cd C:\Program Files(x86)\7-Zip & 7z.exe a -tzip C:\xyz\txt C:\xyz\txt -m0=BZip2 -mx5

How to execute a linux shell script from a C# application?

I have a linux shell script which I can execute manually with CygWin terminal in windows.
sh E://Work/sync.sh E://Work/Voice/Temp
But I'm having some difficulty when I'm trying to run the shell script in my C# application with the same argument.
string command = "E://Work/sync.sh E://Work/Voice/Temp";
ProcessStartInfo psi = new ProcessStartInfo(#"C:\cygwin64\bin\bash.exe", command);
psi.RedirectStandardError = true;
psi.RedirectStandardOutput = true;
psi.UseShellExecute = false;
Process p = Process.Start(psi);
Console.WriteLine(p.StandardError.ReadToEnd());
Console.WriteLine(p.StandardOutput.ReadToEnd());
It's giving me error:
E://Work/sync.sh: line 47: rm: command not found
E://Work/sync.sh: line 48: mkdir: command not found
E://Work/sync.sh: line 50: ls: command not found
E://Work/sync.sh: line 59: ls: command not found
E://Work/sync.sh: line 60: rmdir: command not found
Then I tried a different approach.
ProcessStartInfo psi = new ProcessStartInfo(#"C:\cygwin64\bin\bash.exe");
psi.Arguments = "--login -c sh E://Work/sync.sh E://Work/Voice/Temp";
Process p = Process.Start(psi);
This initiates a CygWin command prompt like this:
Then if I enter my command manually it works.
E://Work/sync.sh E://Work/Voice/Temp
How can I execute the script automatically from my application without any manual input?
After doing some google search, I found a way to do this. All I had to do is change "--login -c" to "--login -i"
ProcessStartInfo psi = new ProcessStartInfo(#"C:\cygwin64\bin\bash.exe");
psi.Arguments = "--login -i E://Work/sync.sh E://Work/Voice/Temp";
Process p = Process.Start(psi);

CMD command from C# code not working (Windows)

I'm trying to run CMD from my code. This is the line that I run in my command line, and it works when I run it manually:
C:\Dev\MySite\web\Website\comparison-tool\data\ & node csvToJson.js
This is what I have in my code:
string commandText = String.Format("/C {0}{1} & node csvToJson.js", root, csvToJsonFolder);
Process.Start("CMD.exe", commandText);
commandText evaluates to /C C:\Dev\MySite\web\Website\comparison-tool\data\ & node csvToJson.js
It runs without error, but nothing seems to have happened. The command prompt doesn't open, so I can't see any errors that may have occurred. The command is supposed to result in a file being written to a particular folder, and when I run the command manually the file gets written, but when I run my code the file does not get written.
EDIT: I changed my code to this:
Process process = new Process();
ProcessStartInfo startInfo = new ProcessStartInfo();
startInfo.WindowStyle = ProcessWindowStyle.Normal;
startInfo.FileName = "cmd.exe";
startInfo.Arguments = commandText;
startInfo.RedirectStandardOutput = true;
startInfo.UseShellExecute = false;
process.StartInfo = startInfo;
process.Start();
string result = process.StandardOutput.ReadToEnd();
The result is just an empty string. There is no error message or anything.
The problem was in my command text, I forgot "cd". Should have been "/C cd C:\Dev\MySite\web\Website\comparison-tool\data\ & node csvToJson.js"

ffmpeg is not recognized

The following code gives me an error in the CMD window (Visual studio 2013 - c# project):
'ffmpeg' is not recognized as an internal or external command
Process p = new Process();
ProcessStartInfo info = new ProcessStartInfo();
info.FileName = "cmd.exe";
info.RedirectStandardInput = true;
info.UseShellExecute = false;
p.StartInfo = info;
p.Start();
using (StreamWriter sw = p.StandardInput)
{
if (sw.BaseStream.CanWrite)
{
sw.WriteLine("ffmpeg -i test.mp4 test.mp3");
// sw.WriteLine("mypassword");
// sw.WriteLine("use mydb;");
}
}
This error happens only when i run the command "ffmpeg -i test.mp4 test.mp3" from the code. the other way works - running the command straight from CMD.. any suggestions to make it work from the code?
Use the WorkingDirectory property to set where the ffmpeg executable file is.
info.WorkingDirectory = #"C:\myFFMPEGDir";
At the moment, it's trying to look into the same directory as your process for ffmpeg. It sounds like ffmpeg is not there so you need to set the path.
Alternatively, you can write the entire path in the command.
sw.WriteLine("C:\\myFFMPEGDir\\ffmpeg.exe -i test.mp4 test.mp3");

Execute a program from the cmd with arguments

I have a program that needed running from the cmd with arguments, say the execute file called
program.exe
And i need to run it from the cmd with args, the whole command in the cmd look like this:
c:\ram\program.exe /path = c:\program files\NV
As you can see the path is : "c:\ram\"
The execute file is : "program.exe"
The args that i need to send is : /path = c:\program files\NV
How can i do it ?
I try to open process like this :
string strArguments = #"/path = c:\program files\NV";
Process p = new Process();
p.StartInfo.FileName = "program.exe";
p.StartInfo.WorkingDirectory = "c:\\ram\\";
p.StartInfo.Arguments = strArguments;
p.Start();
And its not good, i figure that the problem could be that i'm not accessing the exe file from the CMD, maybe i'm wrong...any body got idea how can i do it ?
Thanks
Try these things
p.StartInfo.FileName = "c:\\ram\\program.exe"; without setting Working Directory
and this one is more likely the source of the problem
string strArguments = #"/path = ""c:\program files\NV""";
When there is a space in a path, you have to enclose the whole path in quotation marks. The complete code is as follows
string strArguments = #"/path=""c:\program files\NV""";
Process p = new Process();
p.StartInfo.FileName = #"c:\ram\program.exe";
p.StartInfo.Arguments = strArguments;
p.Start();
It should do exactly what are you trying to do.
1.run "cmd.exe".
2.go to this dir: "c:\ram\" (in the cmd of course).
3.execute the file "program.exe" with this argument: "/path = c\:program files\NV"
It takes the program.exe in the c:\ram\ folder and executes it using cmd with the specified arguments.

Categories

Resources