I am developing a windows application.I am calling a command prompt,I need to call exe file that exe file take parameter.
I am able to open command prompt but not able to send the parametrs
string strCmdText = "create-keyfile.exe ..\\KatanaFirmware\\key-template.txt ..\\Keyfiles\\new-keyfile.txt " + cableTextBox.Text.Trim()+" " + startIdTextBox.Text.Trim();
System.Diagnostics.Process.Start("CMD.exe", strCmdText);
Can please help me out.
Thanks
Punith
System.Diagnostics.ProcessStartInfo startInfo = new ProcessStartInfo("create-keyfile.exe");
startInfo.Arguments = "..\\KatanaFirmware\\key-template.txt ..\\Keyfiles\\new-keyfile.txt " + cableTextBox.Text.Trim()+" " + startIdTextBox.Text.Trim();
System.Diagnostics.Process.Start(startInfo);
You can also see MSDN site on Process.Start, there are examples on how to execute .exe and pass arguments to it.
ProcessStartInfo process = new ProcessStartInfo();
process.FileName = "yourprogram.exe";
process.Arguments = strCmdText; // or put your arguments here rather than the string
Process.Start(process);
have you tried
System.Diagnostics.Process.Start("CMD.exe "+strCmdText);
Actually on further inspection I don't think you need to call CMD.EXE
You should be calling your exe file, unless of course you are using CMD to to display something
string strCmdText = "..\\KatanaFirmware\\key-template.txt ..\\Keyfiles\\new-keyfile.txt " + cableTextBox.Text.Trim()+" " + startIdTextBox.Text.Trim();
System.Diagnostics.Process.Start("create-keyfile.exe", strCmdText);
Have you tried cmd with /k or /c option
From the link
/c : Carries out the command specified by string and then stops.
/k : Carries out the command specified by string and continues.
string strCmdText = "/k create-keyfile.exe ..\\KatanaFirmware\\key-template.txt ..\\Keyfiles\\new-keyfile.txt " + cableTextBox.Text.Trim()+" " + startIdTextBox.Text.Trim();
System.Diagnostics.Process.Start("CMD.exe", strCmdText);
try this.
string strCmdText = "KatanaFirmware\\key-template.txt "+ "\\Keyfiles\\new-keyfile.txt " + cableTextBox.Text.Trim()+" " + startIdTextBox.Text.Trim();
Process mp = new Process();
mp.StartInfo.FileName = "E:\\create-keyfile.exe"; //path of the exe that you want to run
mp.StartInfo.UseShellExecute = false;
mp.StartInfo.CreateNoWindow = true;
mp.StartInfo.RedirectStandardInput = true;
mp.StartInfo.RedirectStandardOutput = true;
mp.StartInfo.Arguments = strCmdText;
mp.Start();
mp.WaitForExit();
Hope it helps.
Related
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'm using the following code to copy the exif data from one file to another using exiftool.
When directly run from command line,the tool is quick.But when the command is executed from a winform app,it takes much greater time.What is going wrong here? Please advice. Is there any alternative way to achieve this?
foreach(string cpath in filelist)
{
string path = "-overwrite_original -TagsFromFile " + "\"" + file + "\"" +" "+ "\"" + outdir + "\\" + Path.GetFileNameWithoutExtension(file) + ext + "\"";
runCmd(path);
}
public void runCmd(string command)
{
ProcessStartInfo cmdsi = new ProcessStartInfo("exiftool.exe");
cmdsi.WindowStyle = ProcessWindowStyle.Hidden;
cmdsi.Arguments = command;
Process cmd = Process.Start(cmdsi);
cmd.WaitForExit();
}
I am making a C# program that will make input file an .iso file.
For example if we write C:\Users\User\Desktpo\a.txt in the textbox that I created and give a destination path for example C:\ it has to create an iso file name a in C:\.
So I downloaded PowerISO and learn about piso.exe then I made some other research about using Process.Start(); in C# so I write these lines of code:
string str = " create -o " + TextBox1.Text + ".iso -add " + TextBox2.Text + "//" ;
Process process = new Process();
process.StartInfo.WindowStyle = ProcessWindowStyle.Hidden;
process.StartInfo.FileName = "\"C:\PowerISO\piso.exe\"";
process.StartInfo.Arguments = str;
process.Start();
But this doesn't work.
Why?
EDIT: I am making an winform using Visual Studio 2017.
Please Note that
C:\PowerISO\piso.exe\
is not a valid C# path, the "\" before the P and the p is an escape indicater.
Either Add verbatim operator "#" before or escape it properly
edit:
In addition, you are adding extra double quotes to the FileName property.
You can check if the path is correct by asserting the file path with File.Exists method
Console.WriteLine(File.Exists(#"C:\PowerISO\piso.exe") ? "File exists." : "File does not exist.");
e.g
string str = " create -o " + TextBox1.Text + ".iso -add " + TextBox2.Text + "//" ;
Process process = new Process();
process.StartInfo.WindowStyle = ProcessWindowStyle.Hidden;
process.StartInfo.FileName = #"C:\PowerISO\piso.exe";
process.StartInfo.Arguments = str;
process.Start();
edit:
Verbatim operator will work, but escaping is better in my opinion.
string str = " create -o " + TextBox1.Text + ".iso -add " + TextBox2.Text + "//" ;
Process process = new Process();
process.StartInfo.WindowStyle = ProcessWindowStyle.Hidden;
process.StartInfo.FileName = "C:\\PowerISO\\piso.exe";
process.StartInfo.Arguments = str;
process.Start();
I'm trying to run the following:
String command = #"Rscript C:\Users\someone\Documents\generate_files.R " + fname + " " + folder;
System.Diagnostics.Process.Start("CMD.exe", "/K PATH C:\\Program Files\\R\\R-3.1.1\\bin;%path%");
System.Diagnostics.Process.Start("CMD.exe", "/K " + command);
Nothing happens when I execute it, does anyone know why? If I try
System.Diagnostics.Process.Start("CMD.exe", "/K MD TEST");
That works fine :s
e: Some extra info, The first command is setting the PATH so that the Rscript can be called by just typing Rscript. Also, both of these commands work when I do them in a normal CMD interface.
Prepare a batch file and execute it
using(StreamWriter sw = new StreamWriter("runscript.cmd", false))
{
sw.WriteLine(#"PATH C:\Program Files\R\R-3.1.1\bin;%path%");
sw.WriteLine(#"Rscript C:\Users\someone\Documents\generate_files.R " + fname + " " + folder);
}
System.Diagnostics.Process.Start("CMD.exe", "/K runscript.cmd");
This assumes that you have read/write permissions on the current directory. You can change the location to a more suitable position using
string path = Environment.GetFolderPath(Environment.SpecialFolder.ApplicationData);
string fileCmd = Path.Combine(path, "runscript.cmd");
using(StreamWriter sw = new StreamWriter(fileCmd, false)
....
As realised by Steve, I was running two console processes. To resolve this I simply ran both commands in the same process.
cmd.Arguments = "/K \"" + fullFilePath;
*try double " " right before PATH