I am not able to pass some parameters to my Dev cmd prompt for vs, I can do it with the classic cmd but not with this one. And I need it because I want to execute CodedUITests from an executable.
This is what my code looks like:
String Path = #"C:\ProgramData\Microsoft\Windows\Start Menu\Programs\Microsoft Visual Studio 2012\Visual Studio Tools\Developer Command Prompt for VS2012.lnk";
ProcessStartInfo proc = new ProcessStartInfo();
proc.FileName = Path;
proc.UseShellExecute = true;
proc.Arguments = #"/c MSTest/h";
Process.Start(proc);
It starts, but no args are inserted,what am I doing wrong ?
EDIT 1 - None of these are working
Process.Start(Path, #"/c "+"MSTest/h"); - err : invalid path - in dev cmd prompt
OR
Process.Start(Path, #"/c ""MSTest/h"); - err: invalid path - in dev cmd prompt
OR
Process.Start(Path, #"/c MSTest/h"); - nothing
OR
Process.Start(Path, "/c MSTest/h"); -nothing
OR
Process.Start(Path, "MSTest/h"); -nothing
EDIT 2 - This is how my final code looks like, partially working, dev cmd starting, but no way to parse args into it because any args I pass they go straight to cmd not to dev-cmd
// ProcessStartInfo procStartInfo = new System.Diagnostics.ProcessStartInfo("cmd", #"%comspec% /k ""C:\Program Files (x86)\Microsoft Visual Studio 11.0\Common7\Tools\VsDevCmd.bat""");
ProcessStartInfo procStartInfo = new System.Diagnostics.ProcessStartInfo("cmd", #"%comspec% /k ""C:\Users\butoiu.edward\Desktop\VsDevCmd1.bat");
procStartInfo.UseShellExecute = false;
// procStartInfo.Arguments = "/k MSTest";
Process proc = new Process();
proc.StartInfo = procStartInfo;
proc.Start();
proc.WaitForExit();
Did you try this way?
void OpenWithArguments()
{
Process.Start("IExplore.exe", "www.northwindtraders.com");
Process.Start("path to exe", "argument");
}
-- FMI MSDN LINK
Update:
I assume it will work this way... but not sure
Open sys default cmd prompt.. and give first param as batch file path (C:\Program Files (x86)\Microsoft Visual Studio 11.0\Common7\Tools\VsDevCmd.bat) and give a space and add next attribute.
Process.Start("Path to EXE", "arg1 arg2")
lnk" file it is actually a link of visual studio cmd prompt. Instead of this location you try original file located at "C:\Program Files (x86)\Microsoft Visual Studio 10.0\VC\vcvarsall.bat"
String Path = #"C:\Program Files (x86)\Microsoft Visual Studio 10.0\VC\vcvarsall.bat";
ProcessStartInfo proc = new ProcessStartInfo();
proc.FileName = Path;
proc.UseShellExecute = true;
proc.Arguments = #"/c MSTest/h";
Process.Start(proc);
I hope this will help you.
Related
The command line I need to execute is
"C:\Program Files (x86)\Microsoft Visual Studio\2019\Enterprise\Common7\IDE\devenv.exe" /Run "C:\unity\unity\MRTK Tutorial\Builds\MRTK Tutorial.sln"
This works from a windows command line without issues,
I formatted it into a string for visual studio
When running from C# this command never executes and the contents of result are ""
System.Diagnostics.ProcessStartInfo procStartInfo = new System.Diagnostics.ProcessStartInfo("C:\\Program Files(x86)\\Microsoft Visual Studio\\2019\\Enterprise\\Common7\\IDE\\devenv.exe", " /Run \"C:\\unity\\unity\\MRTK Tutorial\\Builds\\MRTK Tutorial.sln\"");
// The following commands are needed to redirect the standard output.
// This means that it will be redirected to the Process.StandardOutput StreamReader.
procStartInfo.RedirectStandardOutput = true;
procStartInfo.UseShellExecute = false;
// Do not create the black window.
procStartInfo.CreateNoWindow = true;
// Now we create a process, assign its ProcessStartInfo and start it
System.Diagnostics.Process proc = new System.Diagnostics.Process();
You can try the following code to use c# to execute devenv.exe.
var devEnvPath = #"C:\Program Files (x86)\Microsoft Visual Studio\2019\Enterprise\Common7\IDE\devenv.exe";
string SolutionFile = #"D:\Test\testconsole\testconsole.sln";
ProcessStartInfo startInfo = new ProcessStartInfo(devEnvPath);
startInfo.Arguments = "/Run " + SolutionFile;
Process process = new Process();
process.StartInfo = startInfo;
process.Start();
process.WaitForExit();
Console.ReadKey();
Based on my test, the above code will open vs2019 and open the startup project.
I have to run a .bat file from c#...
I use this method.
file = "C:\\Diego\\PublishCore\\Startup_service.bat";
ProcessStartInfo psi = new ProcessStartInfo();
psi.CreateNoWindow = true;
psi.FileName = file;
psi.UseShellExecute = true;
psi.Verb = "runas";
Process.Start(psi);
.BAT is executed... but the action I ask to perfom it does not execute...
If my .bat says MKDir MyDir... Its creates a Directory called MyDIr with no problems.
But when my bat says dotnet myApp.dll, a cmd Windows opens and closes, but it does not start myApp aplication....
If a doublé-click my .bat is runs fine.
What I am missing? Why the aplication does not start?
I solved it...
The problem was that, as my bat run the instruction dotnet myApp.dll.
I set the path file where the file was, but it was executed in the location where the my Solution is, instead of running in the same directory where I have .bat file.
I have to set WorkingDirectory and Arguments
C:\\Diego\\PublishCore\\Startup_InomCore.bat
ProcessStartInfo psi = new ProcessStartInfo();
psi.WorkingDirectory = "C:\\Diego\\PublishCore";
// psi.CreateNoWindow = true;
psi.FileName = #"cmd.exe";
psi.Arguments = "/c start /wait " + "C:\\Diego\\PublishCore\\Startup_InomCore.bat";
// psi.UseShellExecute = true;
psi.Verb = "runas";
var process = Process.Start(psi);
With the code below I can only start Visual Studio 2008 Command Prompt, but now I have to publish a web site to a target path on my local drive.
I need c# code which does something like the following command:
msbuild /target:Build /p:BuildingProject=true;OutDir=C:\Temp\build\
ccosapp.sln My first attempt (fails - doesn't do anything):
Below is the code I tried, which doesn't do anything:
ProcessStartInfo oInfo = new ProcessStartInfo(#"C:\Program Files (x86)\Microsoft Visual Studio 9.0\VC\vcvarsall.bat", #"msbuild C:\Users\Johan\Documents\Visual Studio 2008\Projects\PRJAPP\PRJAPP.sln");
oInfo.UseShellExecute = false;
oInfo.ErrorDialog = false;
oInfo.CreateNoWindow = true;
oInfo.RedirectStandardOutput = true;
System.Diagnostics.Process p = System.Diagnostics.Process.Start(oInfo);
System.IO.StreamReader oReader2 = p.StandardOutput;
string sRes = oReader2.ReadToEnd();
oReader2.Close();
Another attempt (also fails):
string strCmdText = "/k \"C:\\Program Files (x86)\\Microsoft Visual Studio 9.0\\VC\\vcvarsall.bat\" && msbuild /p:OutDir=C:\\Temp\\build \"C:\\Users\\Johan\\Documents\\Visual Studio 2008\\Projects\\CCOSApp\\ccosapp.sln\"";
System.Diagnostics.Process.Start("CMD.exe", strCmdText);
I'm getting the error in my CMD window:
'C:\Program' is not recognized as an internal or external command,
operable program or batch file.
The last attempt (fails as well - getting the same error as in my 2nd attempt):
/// <span class="code-SummaryComment"><summary></span>
/// Executes a shell command synchronously.
/// <span class="code-SummaryComment"></summary></span>
/// <span class="code-SummaryComment"><param name="command">string command</param></span>
/// <span class="code-SummaryComment"><returns>string, as output of the command.</returns></span>
public void ExecuteCommandSync(object command)
{
try
{
// create the ProcessStartInfo using "cmd" as the program to be run,
// and "/c " as the parameters.
// Incidentally, /c tells cmd that we want it to execute the command that follows,
// and then exit.
System.Diagnostics.ProcessStartInfo procStartInfo =
new System.Diagnostics.ProcessStartInfo("cmd", "/k " + command);
// The following commands are needed to redirect the standard output.
// This means that it will be redirected to the Process.StandardOutput StreamReader.
procStartInfo.RedirectStandardOutput = true;
procStartInfo.UseShellExecute = false;
// Do not create the black window.
procStartInfo.CreateNoWindow = false;
// Now we create a process, assign its ProcessStartInfo and start it
System.Diagnostics.Process proc = new System.Diagnostics.Process();
proc.StartInfo = procStartInfo;
proc.Start();
// Get the output into a string
string result = proc.StandardOutput.ReadToEnd();
// Display the command output.
Console.WriteLine(result);
}
catch (Exception objException)
{
// Log the exception
}
}
/// <span class="code-SummaryComment"><summary></span>
/// Execute the command Asynchronously.
/// <span class="code-SummaryComment"></summary></span>
/// <span class="code-SummaryComment"><param name="command">string command.</param></span>
public void ExecuteCommandAsync(string command)
{
try
{
//Asynchronously start the Thread to process the Execute command request.
System.Threading.Thread objThread = new System.Threading.Thread(new ParameterizedThreadStart(ExecuteCommandSync));
//Make the thread as background thread.
objThread.IsBackground = true;
//Set the Priority of the thread.
objThread.Priority = ThreadPriority.AboveNormal;
//Start the thread.
objThread.Start(command);
}
catch (ThreadStartException objException)
{
// Log the exception
}
catch (ThreadAbortException objException)
{
// Log the exception
}
catch (Exception objException)
{
// Log the exception
}
}
string cmdStr = #" ""C:\Program Files (x86)\Microsoft Visual Studio 9.0\VC\vcvarsall.bat""
cd ""C:\Users\Johan\Documents\Visual Studio 2008\Projects\CCOSApp""
msbuild /target:Build /p:BuildingProject=true;OutDir=C:\Temp\build
ccosapp.sln";
new VsFunctions().ExecuteCommandAsync(cmdStr);
I came with this workaround:
StreamWriter w = new StreamWriter(#"C:\temp\publish.bat");
w.WriteLine(#"call ""C:\Program Files (x86)\Microsoft Visual Studio 9.0\VC\vcvarsall.bat""");
w.WriteLine(#"call cd ""C:\Users\Johan\Documents\Visual Studio 2008\Projects\CCOSApp""");
w.WriteLine(#"call msbuild /target:Build /p:BuildingProject=true;OutDir=C:\Temp\build ccosapp.sln");
w.Close();
System.Diagnostics.Process proc = new System.Diagnostics.Process();
ProcessStartInfo psi = new ProcessStartInfo(#"publish.bat");
psi.WorkingDirectory = #"C:\temp\";
proc.StartInfo = psi;
proc.Start();
I create a .bat file.
I write the commands in it.
Finally I start the .bat file as a Process.
C:\Program' is not recognized as an internal or external command,
operable program or batch file.
you are getting this error because there is space in your path, between Program and File
try placing double quotation mark at the start and end of your path
Instead of passing the whole path of the executable, try to changing the directory to "C:\Program Files (x86)\Microsoft Visual Studio 9.0\VC\" via
oInfo.WorkingDirectory = #"C:\Program Files (x86)\Microsoft Visual Studio 9.0\VC\";
Then just call the vcvarsass.bat
Hopefully that helps
Does it work when you use the Arguments property of StartInfo
Process cmd = new Process();
cmd.StartInfo.CreateNoWindow = false;
cmd.StartInfo.UseShellExecute = false;
cmd.StartInfo.FileName = #"C:\Program Files (x86)\Microsoft Visual Studio 9.0\VC\vcvarsall.bat";
cmd.StartInfo.Arguments = #"msbuild C:\Users\Johan\Documents\Visual Studio 2008\Projects\PRJAPP\PRJAPP.sln";
cmd.Start();
cmd.WaitForExit();
ie like in this example: Using cmd.exe from C#
I'm trying to run JMeter through C# with cmd but it just opens cmd and doesn't run anything.
System.Diagnostics.Process process = new System.Diagnostics.Process();
System.Diagnostics.ProcessStartInfo startInfo = new System.Diagnostics.ProcessStartInfo();
//startInfo.WindowStyle = System.Diagnostics.ProcessWindowStyle.Hidden;
startInfo.WorkingDirectory = "D:";
startInfo.FileName = "cmd.exe";
startInfo.Arguments = "/k D:\\jmeter\\apache-jmeter-2.13\\bin\\ApacheJMeter.jar -n -t D:\\Delo\\dokument.jmx";
process.StartInfo = startInfo;
process.Start();
That code just opens the cmd and nothing happens. I've tried changing the working directory but it doesn't work. If I don't set the working directory, cmd just open at my debug directory. This does work if I start it directly from cmd (without C#).
Solved with this: a link
I'm not exactly sure what are you trying to achieve, and why you aren't using System.Diagnostics ? But I have a suggestion if I understood you right:
> System.Diagnostics.ProcessStartInfo startInfo = new System.Diagnostics.ProcessStartInfo();
> startInfo.WorkingDirectory = "D:";
> startInfo.FileName = #"D:\jmeter\apache-jmeter-2.13\bin\ApacheJMeter.jar";
> startInfo.Arguments = "";
> System.Diagnostics.Process.Start(startInfo);
>
> System.Diagnostics.ProcessStartInfo startInfo2 = new System.Diagnostics.ProcessStartInfo();
> startInfo2.WorkingDirectory = "D:";
> startInfo2.FileName = #"D:\Delo\dokument.jmx";
> startInfo2.Arguments = "";
> System.Diagnostics.Process.Start(startInfo2);
I don't think you will be able to run .jar file directly via cmd interpreter, go for the following alternatives:
use jmeter.bat wrapper script
call Java executable like: path\to\java.exe -jar D:\\jmeter\\apache-jmeter-2.13\\bin\\ApacheJMeter.jar ...
I would also suggest using -l command line argument so .jtl results file could be generated.
See How Do I Run JMeter in Non-GUI Mode? article for details. I also believe that Full list of command-line options will be helpful in your case.
Similar question was asked at least a dozen times on SO and it looks that now I have exhausted most of the proposed solutions but still unable to complete the task successfully.
So what I have is the following command that I want to run in cmd:
xcopy /q C:\fileName.txt \\VMNAME\C$\destFolder /Y /E
But I need it to be executed with certain credentials. So what I was doing manually is entering the below command first:
runas /user:<domainName>\<userName> cmd
That was opening a separate cmd window and I was running the first (xcopy) command in that window.
What I have at the moment:
string strCmdText = string.Format(#"xcopy /q {0} {1} /Y /E", source, destination);
ProcessStartInfo procStartInfo = new ProcessStartInfo();
procStartInfo.RedirectStandardError = true;
procStartInfo.RedirectStandardOutput = true;
procStartInfo.UseShellExecute = false;
procStartInfo.FileName = "runas";
procStartInfo.Arguments = String.Format(#"/user:<domainName>\<userName> cmd " + strCmdText);
Process.Start(procStartInfo);
Where the source and destination are of the below structure:
source = "C:\\somePath\\fileName.txt"
destination = "\\\\<VMName>\\C$\\somePath\\"
I have also tried defining procStartInfo with:
procStartInfo.Verb = "runas";
Instead of:
procStartInfo.FileName = "runas";
With similar results.
At the moment, when I run the above code, it does not return any error but doesn't do what's expected either. Am I missing something or this approach is wrong?