Multiple arguments with cmd.exe via a C# Process - c#

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();

Related

Create a forms app to restart a remote PC

I just need to create a tool to restart a specific windows device in our city offices. which will always in 192.168.cityID.33 I have found a way to detect the citycode portion of the IP and to add 33 to it in GetIPMethod.
thee mentioned device have different set of username and password im having issues passing the restart command to the cmd
PS: I'm not a full time developer i'm just a network admin who tries to reduce my daily work load :)
Thanks in advance
public void Command1()
{
String IP = GetIPAddress().ToString();
string NewIP = IP.Substring(0, IP.LastIndexOf("."));
string TOPIP = NewIP + ".33";
Process process = new Process();
process.StartInfo.FileName = "cmd.exe";
process.StartInfo.CreateNoWindow = true;
process.StartInfo.RedirectStandardInput = true;
process.StartInfo.RedirectStandardOutput = true;
process.StartInfo.UseShellExecute = false;
process.StandardInput.WriteLine("NET USE \\" + TOPIP + "\receiver /USER:GenusDS G3nu5DS");
process.StandardInput.WriteLine("shutdown /m \\" + TOPIP + " /r /f -t 00");
process.Start();
process.StandardInput.Flush();
process.Close();
process.WaitForExit();
Console.WriteLine(process.StandardOutput.ReadToEnd());
Console.ReadKey();
string strCmdText;
strCmdText = "NET USE \\" + TOPIP + "\receiver /USER:GDS G3nS";
System.Diagnostics.Process.Start("CMD.exe",strCmdText);
}
public static IPAddress GetIPAddress()
{
IPAddress ip = Dns.GetHostAddresses(Dns.GetHostName()).Where(address =>
address.AddressFamily == AddressFamily.InterNetwork).First();
return ip;
}
I normally do something similar to below:
Process process = new Process();
ProcessStartInfo startInfo = new ProcessStartInfo();
startInfo.WindowStyle = ProcessWindowStyle.Hidden;
startInfo.FileName = "cmd.exe";
startInfo.Arguments = "/C net use \\[My IP] [My Password] /USER:[My Username]";
process.StartInfo = startInfo;
Process.Start(startInfo);
And this question shows how to run multiple commands in one process.
Hope that helps.
var proc1 = new ProcessStartInfo();
string Command;
proc1.UseShellExecute = true;
Command = "net use " + slash + TOPIP + "\\receiver /user:GenusDS G3nu5DS&shutdown /m " + slash + TOPIP + " /r /f -t 00";
proc1.WorkingDirectory = #"C:\Windows\System32";
proc1.FileName = #"C:\Windows\System32\cmd.exe";
/// as admin = proc1.Verb = "runas";
proc1.Arguments = "/c " + Command;
proc1.WindowStyle = ProcessWindowStyle.Maximized;
Process.Start(proc1);

Run .bat file from cmd.exe in C# with various arguments

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!

double quote error when using ProcessStartInfo in C#

I'm trying to open run a few arguments using cmd.exe from ProcessStartInfo in C#
but my folder navigation needs to include double quotes eg. "C:\this is\my\folder site"
as you see the reason for using double quotes is because the folders have space on their name.
this is my code
var ddd = "\"" + projectPath + "\"";
var strCmdTxt = "/c cd " + ddd + " && code .";
System.Diagnostics.Process process = new System.Diagnostics.Process();
System.Diagnostics.ProcessStartInfo startInfo = new System.Diagnostics.ProcessStartInfo
{
WindowStyle = System.Diagnostics.ProcessWindowStyle.Hidden,
FileName = "cmd.exe",
Arguments = strCmdTxt, UseShellExecute = true, CreateNoWindow= true
};
process.StartInfo = startInfo;
process.Start();
BUT, what it runs is something like this
cd\ "C:\this is\my\folder site\"
which, just returns me to C drive
The command should be cd "C:\this is\my\folder site"
Looks like what you're trying to achieve is start VS Code in the specified folder. Consider using the working directory of the process you're starting, instead of trying to navigate to that directory and starting VS Code in there. Here is a method to help with that:
private static void StartVSCodeInFolder(string projectPath)
{
using (System.Diagnostics.Process process = new System.Diagnostics.Process())
{
System.Diagnostics.ProcessStartInfo startInfo = new System.Diagnostics.ProcessStartInfo
{
WindowStyle = System.Diagnostics.ProcessWindowStyle.Hidden,
FileName = "C:/Program Files/Microsoft VS Code/Code.exe",
Arguments = ".",
UseShellExecute = false,
CreateNoWindow = true,
WorkingDirectory = projectPath
};
process.StartInfo = startInfo;
process.Start();
}
}
Hope this helps.
Could you not change the working directory using the Environment class and simply using "code.exe".
It seems like it would be a cleaner approach.
Environment.CurrentDirectory = #"C:\Program Files\Microsoft VS Code";

run command promt application from code C#

This is my code:
var startupPath = Directory.GetParent(Directory.GetCurrentDirectory()).Parent.Parent.FullName; // +\\Common
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 " + startupPath + #"\Common\sound 1.wav result.wav -tempo=35";
process.StartInfo = startInfo;
process.Start();
Pathes are correct. A wanna to start application and set this parameters sound 1.wav result.wav -tempo=35. What am I doing wrong?
content of startInfo.Arguments:
"/c C:\\SOUNDS\\New folder\\TrunscribeHelper\\Common\\sound 1.wav result.wav -tempo=35"
I've tryed to start it directly:
ProcessStartInfo startInfo = new ProcessStartInfo(string.Concat(startupPath + #"\Common", "\\", "sound.exe"));
startInfo.Arguments = "1.wav result.wav -tempo=35";
startInfo.UseShellExecute = false;
System.Diagnostics.Process.Start(startInfo);
But Am I doing wrong now?
Your have to set your path in the args under double quotes:
"/c \"C:\\SOUNDS\\New folder\\TrunscribeHelper\\Common\\sound 1.wav\" result.wav -tempo=35"
Can you try changing this line:
startInfo.Arguments ="/c " + startupPath + #"\Common\sound 1.wav result.wav -tempo=35";
to
startInfo.Arguments ="/c \"" + startupPath + #"\Common\sound"" 1.wav result.wav -tempo=35";

C# run batch file with argument with spaces

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();

Categories

Resources