I've been trying all day to start process which would run the following code:
C:\bin\ant.bat -f=C:\build.xml -DinputFile=C:\Desktop\Book1.xml -DstartDate=2018-06-20 -DxslFile=ProcessingDate -DoutputFile=fff
and it works completely fine in cmd.
this is my last code in C# which I really hoped would work, but however it doesn't:
public void run() {
string antFile = #"C:\ant.bat";
string build = #"C:\build.xml";
string inputFile = #"C:\Book1.xml";
string startDate = "2018-05-23";
string outputFile = "ff";
ProcessStartInfo procStartInfo = new ProcessStartInfo("cmd.exe", "/c" + #"C:bin\ant.bat -f=C:\build.xml -DinputFile=C:\Desktop\Book1.xml -DstartDate=2018-06-20 -DxslFile=ProcessingDate -DoutputFile=test0.xsl");
Process proc = new Process();
proc.StartInfo = procStartInfo;
proc.Start();
ProcessStartInfo procStartInfo2 = new ProcessStartInfo("cmd.exe", "/c" + antFile + "-f=" + build + "-DinputFile=" + inputFile + "-DstartDate=" + startDate + "-DxslFile=" + startDate + "-DoutputFile=" + outputFile);
Process proc2 = new Process();
proc2.StartInfo = procStartInfo2;
proc2.Start();
}
Firstly, I've tried to just put everything from cmd to the process but it didn't work, after I tried to do what I actually have to: put all the string values as arguments but it didn't work either.
Instead I am getting bunch of exceptions
I'm literally out of options as I've sat all day doing this. Does anyone have idea what problem it could be?
UPDATE:
I've managed to run startInfo3 process. But startInfo4 still doesn't work. I've checked both lines seem to produce the same so what's wrong with it if they're are the same. Do I pass them incorrectly?
ProcessStartInfo startInfo3 = new ProcessStartInfo();
startInfo3.FileName = "cmd.exe";
startInfo3.Arguments = "/c" + #"C:\ant.bat -f=C:\build.xml -DinputFile=C:\Book1.xml -DstartDate=2018-06-20 -DxslFile=ProcessingDate -DoutputFile=fff";
Process.Start(startInfo3);
ProcessStartInfo startInfo4 = new ProcessStartInfo();
startInfo4.FileName = "cmd.exe";
startInfo4.Arguments = "/c" + antFile + "-f=" + build + "-DinputFile=" + inputFile + "-DstartDate=" + startDate + "-DxslFile=" + startDate + "-DoutputFile=" + outputFile;
Process.Start(startInfo4);
After digging a bit more I figured out an answer:
ProcessStartInfo procStartInfo5 = new ProcessStartInfo();
procStartInfo5.FileName = "cmd.exe";
procStartInfo5.Arguments = $"/c{antFile} -f={build} -DinputFile={inputFile} -DstartDate={startDate} -DxslFile=ProcessingDate -DoutputFile={outputFile}";
Process.Start(procStartInfo5);
Hope it helps someone!
Related
I'm trying to uninstall some redistributables from a C# program, so I look through the Program ID values stored in my app.config and then try to run msiexec to uninstall them. If I store the parameters in a ProcessStartInfo object the call doesn't work, but if I call Process.Start("stuff") it works fine. Why is that? I want to use ProcessStartInfo so that I have more control over the window that pops up.
This doesn't work:
Process process = new Process();
ProcessStartInfo startInfo = new ProcessStartInfo();
startInfo.WindowStyle = ProcessWindowStyle.Hidden;
int numberOfKeys = ConfigurationManager.AppSettings.Count;
for (int i = 0; i < numberOfKeys; i++)
{
string[] guid = ConfigurationManager.AppSettings.GetValues(i);
startInfo.Arguments = "/X " + guid[0] + " /l*vx log" + i.ToString() + ".txt";
startInfo.CreateNoWindow = true;
startInfo.FileName = "msiexec.exe";
process.StartInfo = startInfo;
var result = process.Start();
}
But this does:
int numberOfKeys = ConfigurationManager.AppSettings.Count;
for (int i = 0; i < numberOfKeys; i++)
{
string[] guid = ConfigurationManager.AppSettings.GetValues(i);
var result = Process.Start("msiexec.exe", "/X " + guid[0] + " /l*vx log" + i.ToString() + ".txt");
}
Can anyone explain why?
I got it to work by putting a Sleep(10000) after the call. Thanks for the suggestions!
I'm working on a project where you "send" a command to the cmd.exe and receive the output. For this command you need a file path -k and an url.
I have the following code (names and values changed):
string path = "C:\Users\program.exe"
string pathcustom = "\"" + path + "\""; //the path needs to be in quotation marks
Process p = new Process();
p.StartInfo.FileName = "cmd.exe";
p.StartInfo.UseShellExecute = false;
string Address = "1.2.3"
string command = pathcustom + " " + "-k" + " " + "https://username:passwort#serveradress" + Address; //Serveradress is the URL
p.StartInfo.Arguments = "/C " + command;
p.StartInfo.RedirectStandardOutput = true;
p.Start();
string ReturnValue = p.StandardOutput.ReadToEnd();
This Code is working fine like I want it to be, but I need another methode thats exactly similar except that the Address looks different. In the code above it would look something like 1.2.3 but int the following method the Address has to look like this (including the backslashes and quotation marks) \"ab:cd:de\" so let's pretend this is
string path = "C:\Users\program.exe"
string pathcustom = "\"" + path + "\"";
Process p = new Process();
p.StartInfo.FileName = "cmd.exe";
p.StartInfo.UseShellExecute = false;
string Address = #"\""ab:cd:de\""";
string command = pathcustom + " " + "-k" + " " + "https://username:passwort#serveradress" + Address;
p.StartInfo.Arguments = "/C " + command;
p.StartInfo.RedirectStandardOutput = true;
p.Start();
string ReturnValue = p.StandardOutput.ReadToEnd();
When I rewrite the code so that the cmd stays open, with the first method I get the ouput I want/expect. But with the second, not working, method, it sends the command to the cmd and executes it, but it writes as "message" that the command was either written wrong or it couldn't be found. But when i take exactly the same code (via streamwriter I write the command for the cmd into a textfile) and copy it into the cmd, it executes it like it should. So basically, it just doesn't work if I execute the command via c#. Please help
You have to wait for the application to exit
Use something like p.WaitForExit(milliseconds)
Or check p.HasExited
According to this MSDN Post, in order for an argument in StartInfo.Arguments to keep the quotes, you need to "triple escape it" , like this:
string Address = "\\\"\"\"ab:cd:de\\\"\"\";
string command = pathcustom + " " + "-k" + "https://username:passwort#serveradress" + Address;
I need to run batch file, which has path (can contains spaces) as argument.
Batch file is really simple:
echo off
echo %1 > echotest.txt
Csharp code I am using to run this batch file:
ProcessStartInfo info = new ProcessStartInfo();
info.UserName = KIM_USER;
info.Password = ConvertToSecureString(KIM_USER_PASSWORD);
info.FileName = theTask.Path;
info.Arguments = "\"" + TranslateParameter(theTask.Parameter) + "\"";
info.Domain = Environment.MachineName;
info.WorkingDirectory = Path.GetDirectoryName(theTask.Path);
info.UseShellExecute = false;
info.CreateNoWindow = true;
Process batProcess = Process.Start(info);
batProcess.WaitForExit();
Basically in parameter comes e.g {Test_Path} and this is in TranslateParameter converted to real path, e.g: D:\Test Path\ (contains spaces)
This does not work for me, it returns me exit code 1 everytime.
If i remove \" from info.Arguments, it works, but in output file is just D:\Test
Any suggestions?
Regards
Can you try this if that works:
ProcessStartInfo info = new ProcessStartInfo();
info.UserName = KIM_USER;
info.Password = ConvertToSecureString(KIM_USER_PASSWORD);
info.FileName = theTask.Path + " \"" + TranslateParameter(theTask.Parameter) + "\"";
//info.Arguments = "\"" + TranslateParameter(theTask.Parameter) + "\"";
info.Domain = Environment.MachineName;
info.WorkingDirectory = Path.GetDirectoryName(theTask.Path);
info.UseShellExecute = false;
info.CreateNoWindow = true;
Process batProcess = Process.Start(info);
batProcess.WaitForExit();
UPDATED...
I want to call kdiff from Console application. So I'm building two files and want to compare they at the end of executing my program:
string diffCmd = string.Format("{0} {1}", Logging.FileNames[0], Logging.FileNames[1]);
// diffCmd = D:\vdenisenko\DbHelper\DbHelper\bin\Debug\Reports\16_Nov 06_30_46_DiscussionThreads_ORIGIN.txt D:\vdenisenko\DbHelper\DbHelper\bin\Debug\Reports\16_Nov 06_30_46_DiscussionThreads_ORIGIN.txt
System.Diagnostics.Process.Start(#"C:\Program Files (x86)\KDiff3\kdiff3.exe", diffCmd);
//specification is here http://kdiff3.sourceforge.net/doc/documentation.html
It runs kdiff3 tool, but something wrong with filenames or command... Could you please look on screenshot and say what is wrong?
You need to use Process.Start():
string kdiffPath = #"c:\Program Files\Kdiff3.exe"; // here is full path to kdiff utility
string fileName = #"d:\file1.txt";
string fileName2 = #"d:\file2.txt";
Process.Start(kdiffPath,String.Format("\"{0}\" \"{1}\"",fileName,fileName2));
Arguments as described in the docs: kdiff3 file1 file2
var args = String.Format("{0} {1}", fileName, fileName2);
Process.Start(kdiffPath, args);
string kdiffPath = #"c:\Program Files\Kdiff3.exe"; // here is full path to kdiff utility
string fileName = #"d:\file1.txt";
string fileName2 = #"d:\file2.txt";
ProcessStartInfo psi = new ProcessStartInfo(kdiffPath);
psi.RedirectStandardOutput = true;
psi.WindowStyle = ProcessWindowStyle.Hidden;
psi.UseShellExecute = false;
psi.Arguments = fileName + " " + fileName2;
Process app = Process.Start(psi);
StreamReader reader = app.StandardOutput;
//get reponse from console app in your app
do
{
string line = reader.ReadLine();
}
while(!reader.EndOfStream);
app.WaitForExit();
This will run the program from your console app
Process p = new Process();
p.StartInfo.FileName = kdiffPath;
p.StartInfo.Arguments = "\"" + fileName + "\" \"" + fileName2 + "\"";
p.Start();
Unless you are trying to do something else, in which case you need to provide more details.
I try to call the esriRegAsm.exe with arguments from a C# program.
The purpose is to register a Dll. Therefore I usually call the esriRegAsm.exe with the Dll as argument plus some additional parameters (/p:Desktop /s). This works fine if I type it into cmd.exe. Somehow I think that the process sends only the first string to the cmd and not the whole argument list, but I need the "" for the space character in the paths.
For debugging I added a message box and the strings seems to be okay.
Backslash or double backslash seems to be unimportant.
string targetDir = this.Context.Parameters["targ"];
string programFilesFolder = this.Context.Parameters["proFiles"];
System.Diagnostics.Process process = new System.Diagnostics.Process();
System.Diagnostics.ProcessStartInfo startInfo = new System.Diagnostics.ProcessStartInfo();
startInfo.WindowStyle = System.Diagnostics.ProcessWindowStyle.Hidden;
startInfo.FileName = "cmd.exe";
startInfo.Arguments = "/C \"" + programFilesFolder + "Common Files\\\\ArcGIS\\\\bin\\\\esriRegAsm.exe\" " + "\"" + targetDir + "RArcGISTest.dll\" /p:Desktop /s";
MessageBox.Show("/C \"" + programFilesFolder + "Common Files\\\\ArcGIS\\\\bin\\\\esriRegAsm.exe\" " + "\"" + targetDir + "RArcGISTest.dll\" /p:Desktop /s");
process.StartInfo = startInfo;
process.Start();
As I cannot attache a picture of the message box... the output is:
/C "C:\Program Files (x86)\Common Files\ArcGIS\bin\esriRegAsm.exe" "C:\install\RArcGISTest.dll" /p:Desktop /s"
Why are you double-escaping things, and why are you routing it through cmd.exe? Just execute the process directly:
string targetDir = this.Context.Parameters["targ"];
string programFilesFolder = this.Context.Parameters["proFiles"];
Process process = new Process();
ProcessStartInfo startInfo = new ProcessStartInfo();
startInfo.WindowStyle = System.Diagnostics.ProcessWindowStyle.Hidden;
startInfo.FileName = Path.Combine(programFilesFolder, #"Common Files\ArcGIS\bin\esriRegAsm.exe");
startInfo.Arguments = "\"" + Path.Combine(targetDir, "RArcGISTest.dll") + "\" /p:Desktop /s";
process.StartInfo = startInfo;
process.Start();