Alternative to shell command ?
Hello all,
I want to know what is the alternative to shell command in C# ?
Below is the shell command.
Code:
shell("C:\Compute.exe" + " Parse" + " C:\source.txt")
I have a 3rd party console application named "Compute.exe", which accepts a text file as input.
so Compute.exe is the application i call from my application.
Here "Parse" is one of the commands i pass depending upon the need.
And Source.txt is the name of the input file.
at present i am calling this program using Process.Start method. But this doest open/run the compute.exe as it should run when i double click on it.
also i cannot pass the parameters to this file.
Need serious help in this regards.
thanks
MMH
Re: Alternative to shell command ?
Does it work manually? You could try to write a batch file, and call that.
You could even write a batch file on the fly
Re: Alternative to shell command ?
Quote:
Originally Posted by dglienna
Does it work manually? You could try to write a batch file, and call that.
You could even write a batch file on the fly
Can you explain how to achieve this using batch file ?
The problem i am facing is,
The behavior of compute.exe is different when opened with process.start. and different when opened manually (with double click).
Let me explain you this behavior:
When i run this exe manually (with double click) or through command prompt, i could see the compute.exe to ask for the input source file.
But when i run compute.exe using process.start, it just opens the console window. and does not ask for the source file (i can only see a blank console window)
what is the problem with this ? do i need to call/ run compute.exe with some extra code ?
Code:
private void bkgWorker_DoWork(object sender, System.ComponentModel.DoWorkEventArgs e)
{
Process startCompiler = new Process();
startCompiler.StartInfo.FileName = string.Concat(Application.StartupPath, "\\Compute.EXE");
startCompiler.Start();
startCompiler.WaitForExit();
}
private void bkgWorker_RunWorkerCompleted(object sender, System.ComponentModel.RunWorkerCompletedEventArgs e)
{
MessageBox.Show("Command executed.");
}
I cannot figure out how to pass parameters to the exe.
Re: Alternative to shell command ?
StartInfo member of Process has Arguments property. Look it up on MSDN.
Re: Alternative to shell command ?
Quote:
Originally Posted by dglienna
Does it work manually? You could try to write a batch file, and call that.
You could even write a batch file on the fly
Thanks for the suggestion... the batch file system works.... now i have to create a batch file on runtime first..... n call it from my application....
thanks alot once agian...
Re: Alternative to shell command ?
Quote:
Originally Posted by MMH
at present i am calling this program using Process.Start method. But this doest open/run the compute.exe as it should run when i double click on it.
Not sure what you mean. What do you mean it doesn't run as if you double clicked on it?
Quote:
Originally Posted by MMH
also i cannot pass the parameters to this file.
Sure you can. The second parameter to the Process.Start() method are the parameters.
So if you're used to doing this:
shell("C:\Compute.exe" + " Parse" + " C:\source.txt")
You would now do this:
Process.Start("C:\\Computer.exe", "C:\\source.txt");
Since you're using StartInfo, you would add the parameters to the Arguements property as STLDude suggested.
Quote:
Originally Posted by MMH
Thanks for the suggestion... the batch file system works.... now i have to create a batch file on runtime first..... n call it from my application....
thanks alot once agian...
Why would you want to constantly create batch files? That's additional overhead your application doesn't need.
Re: Alternative to shell command ?
Quote:
Originally Posted by kasracer
Not sure what you mean. What do you mean it doesn't run as if you double clicked on it?
When i double click on the file, it gets open in the correct way. because it askes for the parameters(i.e. the source file name).
But when i try to open the same file using process.start, it opens the file, but i can only see a blank "black" window. the file does not ask me for the input parameter.
Basically it looks like it is waiting for something....
here i am looking for the alternative to the shell command in C#.
Quote:
Originally Posted by kasracer
You would now do this:
Process.Start("C:\\Computer.exe", "C:\\source.txt");
i am not sure if the above code works in C#...
Quote:
Originally Posted by kasracer
Since you're using StartInfo, you would add the parameters to the Arguements property as STLDude suggested.
the console application does not accept parameters this way.
Quote:
Originally Posted by kasracer
Why would you want to constantly create batch files? That's additional overhead your application doesn't need.[/
Well creating batch file is just an alternative solution that i can think of...