I tried to call a .exe program I have calling cmd from a c# script that generates a command. When I run the code I get the output the process tried to write to a nonexistent pipe. But when I run said command using a cmd open from Windows it runs perfectly. What I am doing wrong? I am using Visual Studio 2022.
SilentMkdir(baseDir + "/00_CameraInit");
string binName =pre + binDir + "\\aliceVision_cameraInit.exe";
string dstDir =pre + baseDir + "/00_CameraInit\\";
string cmdLine = binName;
cmdLine = cmdLine + " --defaultFieldOfView 45.0 --verboseLevel info --sensorDatabase \"\" --allowSingleView 1";
cmdLine = cmdLine + " --imageFolder \"" + pre + srcImageDir + "\"";
cmdLine = cmdLine + " --output \"" + dstDir + "cameraInit.sfm\"";
Console.WriteLine(cmdLine);
var processInfo = new ProcessStartInfo("cmd.exe", cmdLine);
processInfo.UseShellExecute = false;
processInfo.RedirectStandardOutput = true;
processInfo.CreateNoWindow = false;
//processInfo.RedirectStandardOutput = !string.IsNullOrEmpty("output.txt");
int exitCode = -1;
string output = null;
try
{
var process = Process.Start(processInfo);
process.Start();
output = process.StandardOutput.ReadToEnd();
Console.WriteLine(output);
process.WaitForExit();
}
catch (Exception e)
{
Console.WriteLine("Run error" + e.ToString()); // or throw new Exception
}
I am tryin got execute a msdeploy.exe command using cmd from visual studio with c# as scripting language
string filename = #"C:\Deploy\Test\Test.zip";
string servername = #"PADEVSPTAPP";
string compname = #"IIS Web Application Name";
string appvalue = #"Test";
string strCmdText;
strCmdText = "msdeploy.exe -verb:sync -source:package=" + filename + " -dest=auto,computerName=" + servername + " -setParam=name=" + compname + ",value=" + appvalue + " -allowUntrusted";
//System.Diagnostics.Process.Start("CMD.exe", strCmdText);
try
{
System.Diagnostics.ProcessStartInfo procStartInfo =
new System.Diagnostics.ProcessStartInfo("cmd", "/c " + strCmdText);
procStartInfo.RedirectStandardOutput = true;
procStartInfo.UseShellExecute = false;
procStartInfo.CreateNoWindow = true;
System.Diagnostics.Process proc = new System.Diagnostics.Process();
proc.StartInfo = procStartInfo;
proc.Start();
string result = proc.StandardOutput.ReadToEnd();
proc.WaitForExit();
Console.WriteLine(result);
}
catch (Exception objException)
{
Console.WriteLine(objException.ToString());
}
the string outcome is
msdeploy.exe -verb:sync -source:package="C:\\Deploy\\Test\\Test.zip"
-dest=auto,computerName="PADEVSPTAPP" -setParam=name="IIS Web Application Name",value="Test" -allowUntrusted
but this does not work due to \\ in the command.
How should i execute this command.
I even tried with powershell script ,which also did not work
string PS_script = #"$msdeploy = ""C:\\Program Files\\IIS\\Microsoft Web Deploy V3\\msdeploy.exe""
$package = """;
PS_script = PS_script + Row.deployfile;
PS_script = PS_script + #"""
$compname = ""PADEVSPTAPP""
$appname = ""IIS Web Application Name""
$appvalue = """;
PS_script = PS_script + changetype[0];
PS_script = PS_script + #"""
$md = $(""`""{0}`"" -verb:sync -source:package=`""{1}`"" -dest=auto,ComputerName=`""{2}`"" -setParam=name=`""{3}`"",value=`""{4}`"" -allowUntrusted"" -f $msdeploy, $package, $compname, $appname, $appvalue)
cmd.exe /C ""`""$md`""""";
I have no clue where I am going wrong.
You are using an equals sign where it should be colon.
It's supposed to be -dest: and not -dest=
Same with setParam, it's supposed to be -setParam: not -setParam=
I suspect you don't actually have double backslashes \\ in your string it will just look like that if you inspect via the debugger - I suspect thats whats throwing you off.
Since you have spaces in your compname variable you need double quotes in your arguments string (probably around all your variables would be a good idea).
Also try running msdeploy.exe directly instead of via cmd.exe /c.
I assumed your msdeploy.exe is located in C:\Program Files (x86)\IIS\Microsoft Web Deploy V3
The string outcome is:
-verb:sync -source:package="C:\Deploy\Test\Test.zip" -dest:auto,computerName="PADEVSPTAPP" -setParam:name="IIS Web Application Name",value="Test" -allowUntrusted
Put it all together:
string filename = #"C:\Deploy\Test\Test.zip";
string servername = #"PADEVSPTAPP";
string compname = #"IIS Web Application Name";
string appvalue = #"Test";
string strCmdText;
strCmdText = "-verb:sync -source:package=\"" + filename + "\" -dest:auto,computerName=\"" + servername + "\" -setParam:name=\"" + compname + "\",value=\"" + appvalue + "\" -allowUntrusted";
//System.Diagnostics.Process.Start("CMD.exe", strCmdText);
try
{
System.Diagnostics.ProcessStartInfo procStartInfo =
new System.Diagnostics.ProcessStartInfo(#"C:\Program Files (x86)\IIS\Microsoft Web Deploy V3\msdeploy.exe");
procStartInfo.Arguments = strCmdText;
procStartInfo.RedirectStandardOutput = true;
procStartInfo.UseShellExecute = false;
procStartInfo.CreateNoWindow = true;
System.Diagnostics.Process proc = new System.Diagnostics.Process();
proc.StartInfo = procStartInfo;
proc.Start();
string result = proc.StandardOutput.ReadToEnd();
proc.WaitForExit();
Console.WriteLine(result);
}
catch (Exception objException)
{
Console.WriteLine(objException.ToString());
}
BONUS INFO
If you need a more bulletproof way of determining where msdeploy.exe is located maybe have a look at these links:
https://gist.github.com/SergeyAxenov/15cf008531e6d0741533
How to find out what version of webdeploy/msdeploy is currently installed?
I have a problem with executing the jar file in c#.
This jar file is epubcheck.jar
here is my code to run the file
public string IdpfValidateEpub(string epub)
{
try
{
string result = null;
string epubCheckPath = System.IO.Path.Combine(System.IO.Path.GetDirectoryName(Assembly.GetExecutingAssembly().Location), "epubcheck.jar");
string arguments = "java -jar" + " \"" + epubCheckPath + "\"" + " \"" + epub + "\"";
System.Diagnostics.Process pProcess = new System.Diagnostics.Process();
//strCommand is path and file name of command to run
pProcess.StartInfo.FileName = #"cmd.exe";
//strCommandParameters are parameters to pass to program
pProcess.StartInfo.Arguments = arguments;
Debug.WriteLine("arguments: " + pProcess.StartInfo.Arguments);
pProcess.StartInfo.UseShellExecute = false;
pProcess.StartInfo.CreateNoWindow = false;
//Set output of program to be written to process output stream
pProcess.StartInfo.RedirectStandardOutput = true;
//Start the process
pProcess.Start();
//Get program output
result = pProcess.StandardOutput.ReadToEnd();
//Wait for process to finish
pProcess.WaitForExit();
return result;
}
catch (Exception ex)
{
Debug.WriteLine(ex.Message);
Debug.WriteLine(ex.StackTrace);
throw ex;
}
}
But the program is not running at all. I also set the property to show the window. So i can see if its running or not. But it only shows the command prompt like this
I also printed the arguments i passed in the System.Diagnostics.Process in order to check if the arguments are correct.
After the program printed the arguments. i just copied it and paste in the command prompt. And the program works as expected. But why does it doesn't work in my c# code?
Thank you so much.
Java is a program in its own right, so you don't actually need cmd.exe to run it.
Change your arguments to the following so that you're passing the arguments for java:
string arguments = "-jar" + " \"" + epubCheckPath + "\"" + " \"" + epub + "\"";
And then simply start java instead of cmd:
pProcess.StartInfo.FileName = #"java";
I am developing a software that will list all the software install
in Computer
now i want to Uninstall it using my Program In C# by
calling the Uninstall Key of that software in
Registry Key
My Program Is
Like That But the Process Is Not Working
var UninstallDir = "MsiExec.exe /I{F98C2FAC-6DFB-43AB-8B99-8F6907589021}";
string _path = "";
string _args = "";
Process _Process = new Process();
if (UninstallDir != null && UninstallDir != "")
{
if (UninstallDir.StartsWith("rundll32.exe"))
{
_args = ConstructPath(UninstallDir);
_Process.StartInfo.FileName = Environment.SystemDirectory.ToString() + "\\explorer.exe";
_Process.StartInfo.Arguments = Environment.SystemDirectory.ToString() + "\\" + UninstallDir;
_Process.Start();
}
else if (UninstallDir.StartsWith("MsiExec.exe"))
{
_args = ConstructPath(UninstallDir);
_Process.StartInfo.FileName = Environment.SystemDirectory.ToString() + "\\cmd.exe";
_Process.StartInfo.Arguments = Environment.SystemDirectory.ToString() + "\\" + UninstallDir;
_Process.Start();
}
else
{
//string Path = ConstructPath(UninstallDir);
_path = ConstructPath(UninstallDir);
if (_path.Length > 0)
{
_Process.StartInfo.FileName = _path;
_Process.StartInfo.UseShellExecute = false;
_Process.Start();
}
}
Try this approach:
Process p = new Process();
p.StartInfo.FileName = "msiexec.exe";
p.StartInfo.Arguments = "/x {F98C2FAC-6DFB-43AB-8B99-8F6907589021}/qn";
p.Start();
Refer to this link: http://www.microsoft.com/resources/documentation/windows/xp/all/proddocs/en-us/msiexec.mspx?mfr=true
HTH.
The problem with your misexec.exe code is that running cmd.exe someprogram.exe doesn't start the program because cmd.exe doesn't execute arguments passed to it. But, you can tell it to by using the /C switch as seen here. In your case this should work:
_Process.StartInfo.FileName = Environment.SystemDirectory.ToString() + "\\cmd.exe";
_Process.StartInfo.Arguments = "/C " + Environment.SystemDirectory.ToString() + "\\" + UninstallDir;
Where all I did was add /C (with a space after) to the beginning of the arguments. I don't know how to get your rundll32.exe code to work, however.
Your solution looks good, but keep a space before \qn:
p.StartInfo.Arguments = "/x {F98C2FAC-6DFB-43AB-8B99-8F6907589021} /qn";
Otherwise it wont work in silent mode.
How to display list process, when i run sql*loader in c#. I mean when i run sql*loader from cmd windows, i get list process how many row has been inserted. But when i run SQL*Loader from C#, i can't get process SQL*Loader.
This is my code:
string strCmd, strSQLLoader;
string strLoaderFile = "XLLOAD.CTL";
string strLogFile = "XLLOAD_LOG.LOG";
string strCSVPath = #"E:\APT\WorkingFolder\WorkingFolder\sqlloader\sqlloader\bin\Debug\8testskrip_HTTP.csv";
string options = "OPTIONS (SKIP=1, DIRECT=TRUE, ROWS=1000000,BINDSIZE=512000)";
string append = "APPEND INTO TABLE XL_XDR FIELDS TERMINATED BY ','";
string table = "OPTIONALLY ENCLOSED BY '\"' TRAILING NULLCOLS (xdr_id,xdr_type,session_start_time,session_end_time,session_last_update_time,session_flag,version,connection_row_count,error_code,method,host_len,host,url_len,url,connection_start_time,connection_last_update_time,connection_flag,connection_id,total_event_count,tunnel_pair_id,responsiveness_type,client_port,payload_type,virtual_type,vid_client,vid_server,client_addr,server_addr,client_tunnel_addr,server_tunnel_addr,error_code_2,ipid,c2s_pkts,c2s_octets,s2c_pkts,s2c_octets,num_succ_trans,connect_time,total_resp,timeouts,retries,rai,tcp_syns,tcp_syn_acks,tcp_syn_resets,tcp_syn_fins,event_type,flags,time_stamp,event_id,event_code)";
strCmd = "sqlldr xl/secreat#o11g control=" + strLoaderFile + " LOG=" + strLogFile;
System.IO.DirectoryInfo di;
try
{
System.Diagnostics.ProcessStartInfo cmdProcessInfo = new System.Diagnostics.ProcessStartInfo("cmd.exe");
di = new DirectoryInfo(strCSVPath);
strSQLLoader = "";
strSQLLoader += "LOAD DATA INFILE '" + strCSVPath.ToString().Trim() + "' " + append + " " + table;
StreamWriter writer = new StreamWriter(strLoaderFile);
writer.WriteLine(strSQLLoader);
writer.Flush();
writer.Close();
// Redirect both streams so we can write/read them.
cmdProcessInfo.RedirectStandardInput = true;
cmdProcessInfo.RedirectStandardOutput = true;
cmdProcessInfo.UseShellExecute = false;
cmdProcessInfo.LoadUserProfile = true;
//System.Diagnostics.Process.Start("CMD.exe", strCmdLine);
// Start the procses.
System.Diagnostics.Process pro = System.Diagnostics.Process.Start(cmdProcessInfo);
// Issue the dir command.
pro.StandardInput.WriteLine(strCmd);
// Exit the application.
pro.StandardInput.WriteLine("exit");
//Process[] processlist = Process.GetProcesses();
//foreach(Process pro in processlist){
Console.WriteLine("Process: {0} ID: {1}", pro.ProcessName, pro.Id);
Console.WriteLine(pro.StandardOutput.ReadLine());
//}
// Read all the output generated from it.
string strOutput;
strOutput = pro.StandardOutput.ReadToEnd();
pro.Dispose();
}
catch (Exception ex)
{
return;
}
finally
{
}
Thanks
When you send
pro.StandardInput.WriteLine("exit");
to the process, this will close the console and exit the process.
So when you call
Console.WriteLine("Process: {0} ID: {1}", pro.ProcessName, pro.Id);
it raises a
"Process has exited, so the requested
information is not available."
exception. To fix this, move your exit call to after this line.
Never ignore exceptions!!! They tell you useful things that help you to fix your problems!!