Executing a process with Process.Start() using the path of the process - c#

I'm doing this:
public static void ExecProcess(String path, string filename)
{
Process proc = new Process();
proc.StartInfo.FileName = path + "nst.exe";
proc.StartInfo.Arguments = filename;
proc.StartInfo.UseShellExecute = false;
proc.StartInfo.RedirectStandardError = true;
proc.StartInfo.RedirectStandardOutput = true;
proc.Start();
proc.WaitForExit();
var stringa = proc.StandardOutput.ReadToEnd();
proc.Close();
}
The problem is my process is using the path of my C# application and not its path. So nst.exe is in C:\Desktop but when I call it with the code above the execution path became C:\\Documents\VisualStudio\MyProject\Debug\.
How can I execute the process in his path?
[EDIT]
This is how I call the method:
public void EseguiOttimizzatore()
{
OttimizzatoreService.ExecProcess(#"C:\Users\Developer\Desktop\", _idPlanning.ToString() + ".dat");
}

Set the WorkingDirectory property of StartInfo:
proc.StartInfo.WorkingDirectory = #"C:\Users\Developer\Desktop\";

Related

How to pass filepath and parameters to shell.shellExecute command

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

Unable to open Visual Studio Command Prompt pragmatically using VS 2015

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

Run WinSat Command C#

I am trying to run cmd winsat -drive c programmatically
The code below is only returning "Windows System Assessment Tool" the first line of the output but is not letting the winsat run and return the rest of the output
I am looking for the entire output to be returned shown here
The code I am using is this
public string RunAndOutput(object command)
{
var procStartInfo =
new ProcessStartInfo("winsat", "/c " + "-drive c");
procStartInfo.RedirectStandardOutput = true;
procStartInfo.UseShellExecute = false;
procStartInfo.CreateNoWindow = true;
var proc = new Process();
proc.StartInfo = procStartInfo;
proc.Start();
string result = proc.StandardOutput.ReadToEnd();
proc.WaitForExit(); // Wait for everything to finish
return result;
}
You will need to wait for the process to exit using WaitForExit(), like this:
public string RunAndOutput(object command)
{
var procStartInfo =
new ProcessStartInfo("cmd", "/c " + command);
procStartInfo.RedirectStandardOutput = true;
procStartInfo.UseShellExecute = false;
procStartInfo.CreateNoWindow = true;
var proc = new Process();
proc.StartInfo = procStartInfo;
proc.Start();
string result = proc.StandardOutput.ReadToEnd();
proc.WaitForExit(); // Wait for everything to finish
return result;
}

How to use wevtutil.exe command lines in a Process?

This is two functions im using the wevtutil in both functions this arguments worked in a bat file but not working here i cant find any of the text files created in the contentDirectory.
Something is wrong with the Arguments i guess.
private void SystemEvents()
{
Process proc = new Process();
proc.EnableRaisingEvents = true;
proc.StartInfo.UseShellExecute = false;
proc.StartInfo.FileName = "cmd.exe";
proc.StartInfo.CreateNoWindow = true;
proc.StartInfo.WorkingDirectory = contentDirectory;
proc.StartInfo.Arguments = "wevtutil qe system /rd:true /f:text> eventsys.txt";
proc.Start();
proc.WaitForExit();
proc.Close();
}
private void AppEvents()
{
Process proc = new Process();
proc.EnableRaisingEvents = true;
proc.StartInfo.UseShellExecute = false;
proc.StartInfo.FileName = "cmd.exe";
proc.StartInfo.CreateNoWindow = true;
proc.StartInfo.WorkingDirectory = contentDirectory;
proc.StartInfo.Arguments = "wevtutil qe application /rd:true /f:text> eventapp.txt";
proc.Start();
proc.WaitForExit();
proc.Close();
}
What is wrong with the arguments ?
your cmd.exe is missing a /c agument. type cmd.exe /? in a termainal for more info.
you could just change the Arg line too:
proc.StartInfo.Arguments = "/c wevtutil qe application /rd:true /f:text";
but really I guess you want to do something like this instead.
Process proc = new Process();
proc.EnableRaisingEvents = true;
proc.StartInfo.UseShellExecute = false;
proc.StartInfo.FileName = "wevtutil";
proc.StartInfo.CreateNoWindow = true;
proc.StartInfo.Arguments = "qe application /rd:true /f:text";
Then redirect stdout/stderr to allow you to process the output in C#.

Run command line code programmatically using C#

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

Categories

Resources