Trying to call a perl script from my c# app. The perl script has a prompt that requires the user to enter something into it. I can't seem to get it to display. This is the function i use to call it. I have another function that sets the System.Enviroment.SetEnvironmtVariable to have the path of "C:\Perl\bin\;" along with other ones needed for other process'
private void getRevisionAndUpdate()
{
Process myProcess = new Process();
myProcess.StartInfo.FileName = "cmd.exe";
myProcess.StartInfo.WorkingDirectory = #"the directory that has the perl script";
myProcess.StartInfo.Arguments = #"/c SaidPerlScript.pl";
myProcess.StartInfo.UseShellExecute = false;
myProcess.Start();
myProcess.WaitForExit();
}
as i said before it seems to run, but it will either just open the script in a notepad or do nothing at all. Also should note i've tried running cmd.exe and perl.exe. cmd seems to open it in notepad and perl doesn't display the prompt it should.
Not quite sure why you're trying to run cmd.exe, but it works if you run it via perl.exe (my script is called a.pl and prints hello):
static void Main(string[] args)
{
Process myProcess = new Process();
myProcess.StartInfo.FileName = "perl.exe";
myProcess.StartInfo.WorkingDirectory = #".\";
myProcess.StartInfo.Arguments = #"a.pl";
myProcess.StartInfo.UseShellExecute = false;
myProcess.Start();
myProcess.WaitForExit();
}
and it also correctly reads STDIN. For reference this is a.pl:
print("Hello\n");
my $a = <STDIN>;
print "You entered " . $a . "\n";
Related
My windows forms app triggers an event:
using System.Diagnostics;
string strCmdText = "'/C ping server1.example.com > C:\\Users\\myusername\\Desktop\\1\\a.txt";
Process.Start("cmd.exe", strCmdText);
When executing, cmd.exe is getting spawned, runs for a while, the output is not displayed, but it is present in the redirected 1.txt file.
However, I need to run query command:
using System.Diagnostics;
string strCmdText = "'/C query user /server:server1.example.com > C:\\Users\\myusername\\Desktop\\1\\a.txt";
Process.Start("cmd.exe", strCmdText);
When executing, it spawns a cmd.exe but just for 1 second, then it dissapears, and the output is not present in the 1.txt file.
Is there any way to see what the query command does before it disappears, like keep it open when executing? Maybe is something interesting in there.
Or, am I doing something wrong? Maybe I need to run the command otherwise?
This way:
string outputProcess = "";
string errorProcess = "";
using (Process process = new Process())
{
process.StartInfo.FileName = yourPath;
process.StartInfo.Arguments = yourArguments;
process.StartInfo.WindowStyle = ProcessWindowStyle.Maximized;
process.StartInfo.UseShellExecute = false;
process.StartInfo.RedirectStandardOutput = true;
process.StartInfo.RedirectStandardError = true;
process.Start();
outputProcess = process.StandardOutput.ReadToEnd();
errorProcess = process.StandardError.ReadToEnd();
process.WaitForExit();
}
if you really want to run the code as yours. just replace the "CMD" to "CMD /k"
I have been messing around with triggering a bash script via C#. This all works fine when I first call the "open" command with arguments which in turn opens my .command script via Terminal.
Once the "open" command is used once Terminal or iTerm will remain open in the background, at which point calling the "open" command with arguments then has no further effect. I sadly have to manually quit the application to trigger my script again.
How can I pass arguments to an already open terminal application to restart my script without quitting?
I've searched online ad can't seem to work it out, it already took a good amount of time solve the opening code. Your help is much appreciated.
Here is the C# code I'm using to start the process:
var p = new System.Diagnostics.Process();
p.StartInfo.FileName = "open";
p.StartInfo.WorkingDirectory = installFolder;
p.StartInfo.Arguments = "/bin/bash --args \"open \"SomePath/Commands/myscript.command\"\"";
p.Start();
Thanks
EDIT:
Both answers were correct, this might help others:
ProcessStartInfo startInfo = new ProcessStartInfo("/bin/bash");
startInfo.WorkingDirectory = installFolder;
startInfo.UseShellExecute = false;
startInfo.RedirectStandardInput = true;
startInfo.RedirectStandardOutput = true;
Process process = new Process();
process.StartInfo = startInfo;
process.Start();
process.StandardInput.WriteLine("echo helloworld");
process.StandardInput.WriteLine("exit"); // if no exit then WaitForExit will lockup your program
process.StandardInput.Flush();
string line = process.StandardOutput.ReadLine();
while (line != null)
{
Debug.Log("line:" + line);
line = process.StandardOutput.ReadLine();
}
process.WaitForExit();
//process.Kill(); // already killed my console told me with an error
You can try:
before calling p.Start():
p.StartInfo.UseShellExecute = false;
p.StartInfo.RedirectStandardInput = true;
// for the process to take commands from you, not from the keyboard
and after:
if (p != null)
{
p.StandardInput.WriteLine("echo helloworld");
p.StandardInput.WriteLine("executable.exe arg1 arg2");
}
(taken from here)
This is what you may be looking for :
Gets a stream used to write the input of the application.
MSDN | Process.StandardInput Property
// This could do the trick
process.StandardInput.WriteLine("..");
I am coding a program in C# and I need to open cmd.exe, send my commands and get its answers.
I searched around and found some answers to take diagnostics.process in use.
Now, I have two problems:
When I get the output of process, the output is not shown on the cmd consoule itself.
I need to call g95 compiler on the system. When I call it from cmd manually, it is invoked and does well, but when I call it programmatically, I have the this error: "g95 is not recognized as an internal or external ..."
On the other hand, I only found how to send my commands to cmd.exe via arguments and process.standardInput.writeline(). Is there any more convenient method to use. I need to send commands when the cmd.exe is open.
I am sending a part of my code which may help:
System.Diagnostics.Process myProcess = new System.Diagnostics.Process();
myProcess.StartInfo = new System.Diagnostics.ProcessStartInfo("cmd.exe");
//myProcess.StartInfo.Arguments = "/c g95";
myProcess.StartInfo.UseShellExecute = true;
myProcess.StartInfo.RedirectStandardInput = true;
myProcess.StartInfo.RedirectStandardOutput = true;
myProcess.StartInfo.RedirectStandardError = true;
myProcess.OutputDataReceived += new System.Diagnostics.DataReceivedEventHandler(myProcess_OutputDataReceived);
myProcess.ErrorDataReceived += new System.Diagnostics.DataReceivedEventHandler(myProcess_ErrorDataReceived);
myProcess.Start();
myProcess.BeginOutputReadLine();
myProcess.BeginErrorReadLine();
myProcess.StandardInput.WriteLine("g95 c:\\1_2.f -o c:\\1_2.exe");
You can specify the g95 directly and pass the desired command line parameters to it. You don't need to execute cmd first. The command may not be regognized because the settings from the user profile are not loaded. Try setting the property LoadUserProfile in StartInfo to true.
myProcess.StartInfo.LoadUserProfile = true;
This should also set the path variables correctly.
Your code would look something like this:
Process myProcess = new Process();
myProcess.StartInfo = new ProcessStartInfo("g95");
myProcess.StartInfo.Arguments = "c:\\1_2.f -o c:\\1_2.exe"
myProcess.StartInfo.UseShellExecute = true;
myProcess.StartInfo.LoadUserProfile = true;
myProcess.StartInfo.RedirectStandardInput = true;
myProcess.StartInfo.RedirectStandardOutput = true;
myProcess.StartInfo.RedirectStandardError = true;
myProcess.OutputDataReceived += myProcess_OutputDataReceived;
myProcess.ErrorDataReceived += myProcess_ErrorDataReceived;
myProcess.Start();
myProcess.BeginOutputReadLine();
myProcess.BeginErrorReadLine();
You are getting the error
"g95 is not recognized as an internal or external ..."
because you haven't added the path to g95.exe in your PATH environment variable. You will get similar result if you open up command prompt and type g95. Here is a link to G95 Windows FAQ page that explains it.
Please tell me how to hide the window to connect to the published application via RDP (remote app). Ie when I open rdp file through the function Process() my process run hidden, but unfortunately this process close immediately, and run new copy mststc.exe But it state is not hidden :(
private const string Arg = "C:\\RDP\\myapp.rdp";
private const string FileName = "mstsc";
private Process _myProcess = new Process();
...
myProcess.StartInfo.FileName = sFileName;
myProcess.StartInfo.Arguments = arg;
myProcess.StartInfo.UseShellExecute = false;
myProcess.StartInfo.CreateNoWindow = true;
myProcess.StartInfo.WindowStyle = ProcessWindowStyle.Hidden;
myProcess.Start();
Process mstsc = Process.GetProcessesByName(sFileName)[0];
mstsc.Id not equal myProcess.Id
I see that for first time run first copy mstsc.exe (i think processed/parse file), then for second time run ssecond copy mstsc.exe and first copy was killed
I am making a backup program in c# in which I use a .bat script to create a new Tasks that runs my program on an interval. I need to make sure the .bat is only run if the task does not exist. To do this I use the following piece of code, but it doesn't seem to detect whether or not it exists.
if (!File.Exists(#"C:\Windows\System32\Tasks\BackupUtil.*"))
{
Process proc = new Process();
proc.StartInfo.WindowStyle = ProcessWindowStyle.Hidden;
proc.StartInfo.UseShellExecute = true;
proc.StartInfo.FileName = #"C:\Users\Sebastian Esp\documents\visual studio 2012\Projects\FileBackUp_Sorter\FileBackUp_Sorter\Task_Schedule.bat";
proc.Start();
proc.WaitForExit();
}
Use Directory.GetFiles instead
if (Directory.GetFiles(#"C:\Windows\System32\Tasks", "BackupUtil.*").Length == 0)
//....Your code
}
http://msdn.microsoft.com/en-us/library/wz42302f(v=vs.110).aspx