how to pass file path and parameters to shell.shellExecute command.
For example I am trying following
:shell.ShellExecute("L:\\test\\test.exe",["/abc /pqr /xyz"]);
Here, abc,pqr,xyz are the parameters required to open the file test.exe
You can try using the below command where you can send the required arguments using
process.StartInfo.Arguments
Below is the Sample code .
using System.Diagnostics;
...
Process process = new Process();
// Configure the process using the StartInfo properties.
process.StartInfo.FileName = "process.exe";
process.StartInfo.Arguments = "-n";
process.StartInfo.WindowStyle = ProcessWindowStyle.Maximized;
process.Start();
Instead of directly using shell you can use process to call your exe with parameter. Below is the sample code
private static void RunExeWithParameter(string exePath, string parameter1, string parameter2)
{
string error = "";
using (Process process = new Process())
{
process.StartInfo.UseShellExecute = false;
process.StartInfo.RedirectStandardError = true;
process.StartInfo.CreateNoWindow = true;
process.StartInfo.FileName = "\"" + exePath + "\"";
process.StartInfo.Arguments = "\"" + parameter1 + " \"" + parameter2 + "\"";
process.StartInfo.WindowStyle = ProcessWindowStyle.Hidden;
process.Start();
process.WaitForExit();
error = process.StandardError.ReadToEnd();
process.Close();
}
if (error.Trim().Trim("\r\n".ToCharArray()).Trim() != "")
{
throw new Exception(error);
}
}
Related
I want to run visual studios command programmatically.I have tried the above code but no help.All I am getting is a command prompt with my project`s directory open.
I have used Execute("VS140COMNTOOLS") as input.
private void Execute(string vsEnvVar) {
var vsInstallPath = Environment.GetEnvironmentVariable(vsEnvVar);
if (Directory.Exists(vsInstallPath)) {
var filePath = vsInstallPath + "vsvars32.bat";
if (File.Exists(filePath)) {
//start vs command process
Process proc = new Process();
var command = Environment.GetEnvironmentVariable("ComSpec");
command = #"" + command + #"";
//var batfile = #"E:\Test\vstest.bat";
var args = string.Format("/S/K \" \"{0}\" \"", filePath);
proc.StartInfo.FileName = command;
proc.StartInfo.Arguments = args;
//proc.StartInfo.RedirectStandardInput = true;
//proc.StartInfo.RedirectStandardOutput = true;
proc.StartInfo.CreateNoWindow = false;
proc.StartInfo.UseShellExecute = false;
proc.Start();
} else {
Console.WriteLine("File Does not exists " + filePath);
}
}
}
Try this:
private Process Execute(string vsEnvVar)
{
Process process = new Process();
ProcessStartInfo psi = new ProcessStartInfo("cmd.exe");//assume location is in path. Otherwise use ComSpec env variable
psi.CreateNoWindow = true;
psi.UseShellExecute = false;
psi.RedirectStandardError = true;
psi.RedirectStandardInput = true;
psi.RedirectStandardOutput = true;
psi.WindowStyle = ProcessWindowStyle.Hidden;
process.StartInfo = psi;
// attach output events
process.ErrorDataReceived += new DataReceivedEventHandler(process_ErrorDataReceived);
process.OutputDataReceived += new DataReceivedEventHandler(process_OutputDataReceived);
process.StartInfo = psi;
process.Start();
process.BeginErrorReadLine();
process.BeginOutputReadLine();
process.StandardInput.WriteLine(string.Format("call \"%{0}%vsvars32.bat\""), vsEnvVar);
process.StandardInput.Flush();
return process;
}
Now you can execute any commands by writing to process.StandardInput
process.StandardInput.WriteLine(#"msbuild c:\MySolution.sln /t:Clean");
I want to import a csv file to mongodb by using mongoimport in C#. So I implement this method
public bool importCSV(string filepath, string db, string collectionName){
string result="";
try
{
ProcessStartInfo procStart = new ProcessStartInfo("cmd", "C:/MongoDB/Server/3.0/bin/mongoimport -d " + db + " -c " + collectionName + " --type csv --file " + filepath );
procStart.RedirectStandardOutput = true;
procStart.CreateNoWindow = false;
Process proc = new Process();
proc.StartInfo = procStart;
proc.Start();
result += proc.StandardOutput.ReadToEnd();
}
catch(Exception e){
Console.WriteLine(e.ToString());
}
if (!result.Equals("")){
return true;
}
return false;
}
When I run command itself, I can import file to MongoDB. But by using C#, method returns false.
Can anyone help me to solve this problem?
SOLUTION!!!
public bool importCsv(string filepath, string collectionName){
string result ="";
try
{
ProcessStartInfo startInfo = new ProcessStartInfo();
startInfo.FileName = #"C:/MongoDB/Server/3.0/bin/mongoimport.exe";
startInfo.Arguments = #" -d test -c " + collectionName + " --type csv --file " + filepath + " --headerline";
Process proc = new Process();
proc.StartInfo = startInfo;
proc.Start();
result += "ddd";
}
catch(Exception e)
{
Console.WriteLine(e.ToString());
}
if (!result.Equals(""))
{
return true;
}
return false;
}
try something like this:
process.StartInfo.UseShellExecute = false;
process.StartInfo.RedirectStandardOutput = true;
process.OutputDataReceived += new DataReceivedEventHandler((sender, e) =>
{
if (!String.IsNullOrEmpty(e.Data))
{
result+=e.Data;
}
});
process.Start();
// Asynchronously read the standard output of the spawned process.
// This raises OutputDataReceived events for each line of output.
process.BeginOutputReadLine();
process.WaitForExit();
process.Close();
ProcessStartInfo startInfo = new ProcessStartInfo();
//startInfo.WindowStyle = ProcessWindowStyle.Hidden;
startInfo.FileName = "cmd.exe";
startInfo.Arguments = "tsvc -a -st rifs -h "+textBox1+" -sn "+textBox2+" -status";
Process process = new Process();
process.StartInfo = startInfo;
process.Start();
richTextBox1.Text = process.StandardOutput.ReadToEnd();
I need to run a command in cmd that will take 2 parameters that will be inserted to textBox1 and textBox2 and then sending the output to the richTextBox1.
When running it this way I get :
first chance exception of type 'System.InvalidOperationException' occurred in System.dll
Additional information: StandardOut has not been redirected or the process hasn't started yet
I tried to exclude the Output line , and when i do it does run CMD but does not type in any command (It just opens a CMD window and does nothing ) .
Process.Start() asynchronously starts a new process. When you get to the process.StandardOutput.ReadToEnd() the process is not finished yet, thus the exception. You should use eventing to hook into the OutputDataRecieved event. You want to do something like this:
System.Diagnostics.Process process = new System.Diagnostics.Process();
System.Diagnostics.ProcessStartInfo startInfo = new System.Diagnostics.ProcessStartInfo();
startInfo.RedirectStandardOutput = true;
startInfo.RedirectStandardError = true;
startInfo.UseShellExecute = false;
startInfo.CreateNoWindow = true;
startInfo.FileName = "cmd.exe";
startInfo.Arguments = "/c tsvc -a -st rifs -h " + textBox1 + " -sn " + textBox2 + " -status";
process.StartInfo = startInfo;
process.OutputDataReceived += ProcessOnOutputDataReceived;
process.ErrorDataReceived += ProcessOnOutputDataReceived;
process.Start();
and then add an event handler to the Output data recieved like so:
private void ProcessOnOutputDataReceived(object sender, DataReceivedEventArgs dataReceivedEventArgs)
{
richTextBox1.Text += dataReceivedEventArgs.Data;
}
Also, I am not certain but i think you need to call text on your textboxes:
startInfo.Arguments = "/c tsvc -a -st rifs -h " + textBox1.Text + " -sn " + textBox2.Text + " -status";
Managed to do it in the End with .
richTextBox1.Text = "";
int counter = 0 ,totalMemory=0;
string line;
string command = " /c ro -a -h " + textBox1.Text + " -sn HC* $$info Server Total = $servermemory Service Memory = $servicememory > c:\\noc\\ntb\\logs\\output.txt";
ProcessStartInfo procStartInfo = new ProcessStartInfo("CMD", command);
Process proc = new Process();
procStartInfo.CreateNoWindow = true;
procStartInfo.RedirectStandardOutput = true;
procStartInfo.RedirectStandardError = true;
procStartInfo.UseShellExecute = false;
proc.StartInfo = procStartInfo;
proc.Start();
proc.WaitForExit();
I have an app.exe application that asks to enter input path string, once i enter, it asks output path string... now when i enter, app.exe perform some operation
i need to pass these paths from my Window Form Application
i saw a lot of questions like this but could not implement what i require because i never worked with processes and Stream Reader or Writer
any help please... examples will be thanked.. thank you..
string input = #"C:\Documents and Settings\pankaj\Desktop\My File\greetingsfreinds.ppt";
string output = #"C:\Documents and Settings\pankaj\Desktop\test";
Process process = new Process();
process.StartInfo.FileName = #"C:\Program Files\Wondershare\MyApp\app.exe";
process.StartInfo.UseShellExecute = false;
process.StartInfo.CreateNoWindow = false;
process.StartInfo.RedirectStandardInput = true;
process.Start();
process.WaitForExit(3000);
process.Close();
ok i tried that
but its giving some exception
StandardOut has not been redirected or the process hasn't started yet...
my code was
string input = #"C:\Documents and Settings\pankaj\Desktop\My File\greetingsfreinds.ppt";
string output = #"C:\Documents and Settings\pankaj\Desktop\test";
Process process = new Process();
process.StartInfo.FileName = #"C:\Program Files\Wondershare\MyApp\app.exe";
process.StartInfo.UseShellExecute = false;
process.StartInfo.CreateNoWindow = false;
process.StartInfo.RedirectStandardInput = true;
process.StartInfo.Arguments = input + ";" + output;
process.Start();
string Strout = process.StandardOutput.ReadToEnd();
process.WaitForExit();
process.Close();
You can use ProcessStartInfo.Arguments for this.
Process process = new Process()
process.StartInfo.FileName = #"C:\Program Files\Wondershare\MyApp\app.exe";
process.StartInfo.UseShellExecute = false;
....
process.Arguments = input + " " + output;
I'm using this code run in windows command prompt..
But I need this done programmatically using C# code
C:\Windows\Microsoft.NET\Framework\v4.0.30319>aspnet_regiis.exe -pdf "connection
Strings" "C:\Users\XXX\Desktop\connection string\DNN"
try this
ExecuteCommand("Your command here");
call it using process
public void ExecuteCommand(string Command)
{
ProcessStartInfo ProcessInfo;
Process Process;
ProcessInfo = new ProcessStartInfo("cmd.exe", "/K " + Command);
ProcessInfo.CreateNoWindow = true;
ProcessInfo.UseShellExecute = true;
Process = Process.Start(ProcessInfo);
}
You may use the Process.Start method:
Process.Start(
#"C:\Windows\Microsoft.NET\Framework\v4.0.30319\aspnet_regiis.exe",
#"-pdf ""connection Strings"" ""C:\Users\XXX\Desktop\connection string\DNN"""
);
or if you want more control over the shell and be able to capture for example the standard output and error you could use the overload taking a ProcessStartInfo:
var psi = new ProcessStartInfo(#"C:\Windows\Microsoft.NET\Framework\v4.0.30319\aspnet_regiis.exe")
{
Arguments = #"-pdf ""connection Strings"" ""C:\Users\XXX\Desktop\connection string\DNN""",
UseShellExecute = false,
CreateNoWindow = true
};
Process.Start(psi);
You should be able to do that using a process
var proc = new Process();
proc.StartInfo.FileName = #"C:\Windows\Microsoft.NET\Framework\v4.0.30319\aspnet_regiis.exe ";
proc.StartInfo.Arguments = string.Format(#"{0} ""{1}""" ""{2}""","-pdf","connection Strings" ,"C:\Users\XXX\Desktop\connection string\DNN");
proc.StartInfo.UseShellExecute = false;
proc.StartInfo.RedirectStandardOutput = true;
proc.Start();
string outPut = proc.StandardOutput.ReadToEnd();
proc.WaitForExit();
var exitCode = proc.ExitCode;
proc.Close();