I have an exe that I would like to use by executing it with command line parameters.
var query = Path.Combine(path, calculator.ExeName + ".exe");
var p = new Process();
p.StartInfo.FileName = query;
//the command line parameter that causes the exe to start in an invisible mode
p.StartInfo.Arguments = "episrc"
p.Start();
This code works and it starts the exe but there's one problem : the exe is supposed to be writing on a file in its directory but that doesn't happen. The process exits successfully (Exitcode0). What could be the cause of this problem?
I have a Delphi code that executes successfully the exe and the exe writes to the file but it's using the ExecProcess from win32 API thus the exe is valid and working.
Also if I try to execute it from the command prompt like so : kowwinnt.exe episrc it writes to the file successfully.
You should set the Working Directory.
Your code would look like this:
var query = Path.Combine(path, calculator.ExeName + ".exe");
var p = new Process();
p.StartInfo.FileName = query;
p.StartInfo.WorkingDirectory = path;
//the command line parameter that causes the exe to start in an invisible mode
p.StartInfo.Arguments = "episrc"
p.Start();
Related
The following code executes a .exe file on the local machine and generates a new file .obj based on an .ifc file in the same directory. .obj (new one) and .ifc should have the same name (no problem).
if (clicked) {
Process p = new Process();
p.StartInfo.FileName = "cmd.exe";
p.StartInfo.WorkingDirectory = "C:\...";
string arg1;
arg1 = "#/c command" // run .exe appliaction
p.StartInfo.Arguments = arg1;
p.Start();
p.StartInfo.CreateNoWindow = true;
p.StartInfo.WindowStyle = ProcessWindowStyle.Hidden;
p.WaitForExit();
}
If the user clicked the button one more time (the code gets run again), a new cmd appears and tells him that the file already exists. Does he want to override:
(y/n) would be the argument in this case.
Now I want to capture that new cmd and replace it with a "yes/no" window, so that the user wont see it. How is this possible?
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.
I'm trying to execute TFS via Process.Start but am having some difficulty and I can't understand why. Here's my code snippet:
/// <summary>
/// Get the entire history for a project
/// </summary>
public void GetHistory(String project)
{
ProcessStartInfo info = new ProcessStartInfo();
String fileName = Path.GetTempFileName();
info.Arguments = String.Format("history \"{0}\" /recursive /format:Detailed /noprompt > {1}", "c:\\source\\ " + project, fileName);
info.FileName = "\"C:\\Program Files (x86)\\Microsoft Visual Studio 11.0\\Common7\\IDE\\tf.exe\"";
info.RedirectStandardError = true;
info.UseShellExecute = false;
Process process = new Process();
process.StartInfo = info;
process.Start();
process.WaitForExit();
Console.WriteLine(process.StandardError.ReadToEnd());
Console.WriteLine("History written to " + fileName);
Console.ReadKey();
}
This is resulting in a set of arguments like so (I've just removed the full project name):
I then get the following error:
The history command takes exactly one item.
If I piece the string together and execute in a normal command line however then it works:
Can anyone tell me what I'm missing?
You can't redirect output to a file in the Process.Start arguments. File redirection is a function of the shell.
If you want to put the history into a file, you will need to File.Open the file yourself, read the output of the tf history command and write it to the file.
Or you could use a command script or PowerShell script.
I have tried to redirect the command prompt output to a file using Asp.Net C#.
System.Diagnostics.Process si = new System.Diagnostics.Process();
si.StartInfo.WorkingDirectory = "c:\\";
si.StartInfo.UseShellExecute = false;
si.StartInfo.FileName = "cmd.exe";
si.StartInfo.Arguments = #"/c dir" +">" + #"Myval.txt";
si.StartInfo.CreateNoWindow = true;
si.StartInfo.RedirectStandardInput = true;
si.StartInfo.RedirectStandardOutput = true;
si.StartInfo.RedirectStandardError = true;
si.Start();
string output = si.StandardOutput.ReadToEnd();
Response.Write(output);
si.Close();
The file is getting created successfully but no content present in it.
Even the variable Output returns nothing.
Help me to resolve this issue.
EDIT after being corrected:
I just tested on my machine and the code works perfectly. I apologize for not reading and testing carefully myself. Myval.txt is created and the DIR output is written into it.
The output variable is empty because you are rerouting any output by the DIR command into the txt file, so that's by design.
Please see if there are any locks on the txt file preventing it from being overwritten. Further than that, I can only guess that there is a security issue preventing the DIR command from running.
IIS7 - I tested this various ways including using a Batch file but the application isn't available on desktop. I can see the worker process and the exe running under my user name but with session id value of zero.
The following has worked for me through command prompt:
// Start the child process.
Process p = new Process();
// Redirect the output stream of the child process.
p.StartInfo.UseShellExecute = false;
p.StartInfo.RedirectStandardOutput = true;
p.StartInfo.FileName = "YOURBATCHFILE.bat";
p.Start();
// Do not wait for the child process to exit before
// reading to the end of its redirected stream.
// p.WaitForExit();
// Read the output stream first and then wait.
string output = p.StandardOutput.ReadToEnd();
p.WaitForExit();
My website runs a local .exe file (generates some data), when a user clicks a certain link.
I would like to know how to do the following
what command to use to run the .exe?
where should I store the .exe and still maintain security?
I use .net 4 c#
Not sure if this works in MVC, but give it a shot:
// Process class resides in System.Diagnostics namespace
Process myProcess = Process.Start("...");
myProcess.WaitForExit();
// todo : process your data
Here's something that I use in one of my applications:
var p = new Process();
p.StartInfo.UseShellExecute = false;
p.StartInfo.RedirectStandardOutput = true;
p.StartInfo.RedirectStandardError = true;
p.StartInfo.FileName = HttpContext.Current.Server.MapFfmpegPath();
p.StartInfo.Arguments = "arguments go here :)";
p.Start();
p.WaitForExit();
As for the executable itself, I created a directory in my project and put the exe in that directory. The MapFfmpegPath method looks something like this.
public static string MapFfmpegPath(this HttpServerUtility server)
{
return "\"" + server.MapPath("/CoolPathHere/ffmpeg.exe") + "\"";
}