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);
Related
I am currently trying to "convert" a bash script I wrote to C#.
This script starts a program in a shell and then executes a few commands in this shell and looks similar to this:
cd /$MYPATH
./executible -s << EOF
start_source_probe -hardware_name "USB" -device_name "#1: EP3C(10|5)"
write_source_data -instance_index 0 -value "11111"
write_source_data -instance_index 0 -value "10111"
write_source_data -instance_index 0 -value "00111"
exit
EOF
Now I would like to do the same using Visual Studio C#.
At the moment my attempt looks like this:
ProcessStartInfo startInfo = new ProcessStartInfo();
startInfo.WorkingDirectory = "pathToExe\\";
startInfo.FileName= "executible.exe";
startInfo.Arguments= "-s";
//startInfo.UseShellExecute = false;
//startInfo.RedirectStandardInput = true;
Process proc = new Process();
proc.StartInfo = startInfo;
proc.Start();
//StreamWriter myStreamWriter = proc.StandardInput;
//Console.WriteLine("start_source_probe - hardware_name \"USB\" -device_name \"#1: EP3C(10|5)\"");
//Console.WriteLine("write_source_data -instance_index 0 -value \"11111\"");
proc.WaitForExit();
With the comments activated (so with the "//" in code) I manage to open the shell (-s stands for shell) but I wonder how I am able to execute commands in this shell additionally.
I managed to execute multiple commands with something like this (as long as I am not starting the shell because destination output differs I guess)
const string strCmdText = "/C command1&command2";
Process.Start("CMD.exe", strCmdText);
I would appreciate it if someone could tell me how to add the argument "-s" and execute commands in the started process.
I am not sure if i did understood your question, but you can create a batch file outside your c# code and call It from your c# code like the following :
System.Diagnostics.ProcessStartInfo ProcStartInfo = new System.Diagnostics.ProcessStartInfo("cmd");
ProcStartInfo.RedirectStandardOutput = true;
ProcStartInfo.UseShellExecute = false;
ProcStartInfo.CreateNoWindow = false;
ProcStartInfo.RedirectStandardError = true;
System.Diagnostics.Process MyProcess = new System.Diagnostics.Process();
ProcStartInfo.Arguments = "/c start /wait batch.bat ";
MyProcess.StartInfo = ProcStartInfo;
MyProcess.Start();
MyProcess.WaitForExit();
I added the /wait so your c# code is going to wait for your batch to finish, to pursuit the c# code execution
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"
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");
I am working on a project of remotely receiving commands from a server, but I am facing a problem when working with the command prompt locally. Once I get it working locally, then I will move to remote communication.
Problem:
I have to completely hide the console, and client must not see any response when the client is working with the command line but it will show a console for a instance and then hide it.
I had to use c# to send a command to cmd.exe and receive the result back in C#. I have done it in one way by setting the StandardOutput... and input to true.
The commands are not working. For example, D: should change the directory to D and it does, but after that, if we use dir to see the directories in D, it does not show the appropriate directories.
Here is my code:
First Method
Process process = new Process();
ProcessStartInfo startInfo = new ProcessStartInfo();
startInfo.WindowStyle = ProcessWindowStyle.Hidden;
startInfo.FileName = "cmd.exe";
startInfo.Arguments = "/C " + textBoxInputCommand.Text + " >> " + " system";
process.StartInfo = startInfo;
process.Start();
Second Method
ProcessStartInfo procStartInfo = new ProcessStartInfo("cmd", "/c " + textBoxInputCommand.Text);
procStartInfo.WorkingDirectory = #"c:\";
procStartInfo.RedirectStandardOutput = true;
procStartInfo.UseShellExecute = true;
procStartInfo.UseShellExecute = false;
Process proc = new Process();
proc.StartInfo = procStartInfo;
proc.Start();
string result = proc.StandardOutput.ReadToEnd();
richTextBoxCommandOutput.Text += result;
I want the program to run as administrator because the exe it generates does not run commands when it runs from the C drive.
Try not to run the commands by passing them to cmd instead write the commands passed by the client to a.bat file execute the .bat. file from your program this will probably hide your command prompt window.
You can also use process.OutputDataRecieved event handler to do anything with the output.
If you want to execute command using administrator rights you can use runascommand. It is equivalent to the sudo command in Linux. Here is a piece of code may be it will help you
var process = new Process();
var startinfo = new ProcessStartInfo(#"c:\users\Shashwat\Desktop\test.bat");
startinfo.RedirectStandardOutput = true;
startinfo.UseShellExecute = false;
process.StartInfo = startinfo;
process.OutputDataRecieved += DoSomething;
process.Start();
process.BeginOutputReadLine();
process.WaitForExit();
//Event Handler
public void DoSomething(object sener, DataReceivedEventArgs args)
{
//Do something
}
Hope it helps you.
You could hide command prompt window by adding this line of code:
startInfo.WindowStyle = System.Diagnostics.ProcessWindowStyle.Hidden;
or do not create it at all
startInfo.CreateNoWindow = true;
Here can be found a few awarding solutions:
Run Command Prompt Commands
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);