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);
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'm using the below code to log output of a cmd call to a file however it's not working at times.
System.Diagnostics.Process process = new System.Diagnostics.Process();
System.Diagnostics.ProcessStartInfo startInfo = new System.Diagnostics.ProcessStartInfo();
startInfo.WindowStyle = System.Diagnostics.ProcessWindowStyle.Hidden;
startInfo.FileName = "cmd.exe";
//startInfo.Arguments = "/C dir C:\\ >c:\\temp\\dir.txt";
startInfo.Arguments = "/C \"C:\\Program Files\\Geth\\geth.exe\" --exec \"web3.eth.getBalance(web3.eth.accounts[0]);\" attach >c:\\temp\\out.txt";
process.StartInfo = startInfo;
process.Start();
The simple dir works fine.
Using the Ethereum geth.exe without --exec works fine.
However once I include the --exec argument the output is blank. Both commands work fine and produce output if manually called in cmd.exe.
"C:\Program Files\Geth\geth.exe" attach >c:\temp\out.txt
"C:\Program Files\Geth\geth.exe" --exec "web3.eth.getBalance(web3.eth.accounts[0]);" attach >c:\temp\out.txt
I see you found a workaround, but for others:
process.Start();
process.WaitForExit();
You have to wait for the process to exit.
So I've looked at alot of different SO posts, I've been on codeproject and dreamincode aswell but I cant for the life of me find out how to CORRECTLY start a process on Windows 8.1 with admin rights.
This is my code.
I'm trying to create a silent install of FireFox but everytime I give it the filepath it still promts me with the UAC, and I thought that running as admin would work. What am I doing wrong here?
Console.WriteLine("Please enter the path to the application: ");
string path = Console.ReadLine();
ProcessStartInfo psi = new ProcessStartInfo();
psi.UseShellExecute = true;
psi.Verb = "runas";
psi.Arguments = "/s /v /qn /min";
psi.CreateNoWindow = true;
psi.WindowStyle = ProcessWindowStyle.Hidden;
psi.FileName = path;
Process.Start(psi);
Using Unity3D and from editor script trying to run a script in the terminal on osx.
When running test.sh from terminal the GDCL application does its thing and then outputs the arguments. But if I run the script from Unity3D editor I only get the arguments in the output. GDCL doesn't run.
How can I get Unity3D to run terminal scripts?
C# script that runs test.sh (gives only output)
ProcessStartInfo psi = new ProcessStartInfo();
psi.FileName = Application.dataPath+"/test.sh";
psi.UseShellExecute = false;
psi.RedirectStandardOutput = true;
psi.Arguments = "arg1 arg2 arg3";
//psi.Arguments = "test";
Process p = Process.Start(psi);
string strOutput = p.StandardOutput.ReadToEnd();
p.WaitForExit();
UnityEngine.Debug.Log(strOutput);
The test.sh script has chmod 777 (GDCL works only from terminal)
#!/bin/sh
GDCL ~/Documents/Unity/testproject/Assets/Font\ Normal.GlyphProject ~/Documents/Unity/testproject/Assets/Textures/fontNormal/font -fo PlainText-txt
for arg in $*
do
echo $arg
done
Try setting UseShellExecute to true or try running your shell directly and passing the script as the first argument.
psi.UseShellExecute = true;
Or
ProcessStartInfo psi = new ProcessStartInfo();
psi.FileName = "/bin/sh";
psi.UseShellExecute = false;
psi.RedirectStandardOutput = true;
psi.Arguments = Application.dataPath + "/test.sh" + " arg1 arg2 arg3";
Don't forget to import:
using System.Diagnostics;
try which GDLC in terminal, get the full path and instead of GDLC in test.sh use full path then it will work
To dump database from command line, all I need to do is:
mysqldump -uroot --password= myDb --routines> "C:\s.sql"
So all I would try programmatically is this, which is the direct interpretation of it I suppose:
ProcessStartInfo psi = new ProcessStartInfo();
psi.FileName = dumpUtilityPath;
psi.RedirectStandardInput = false;
psi.RedirectStandardOutput = true;
psi.UseShellExecute = false;
psi.CreateNoWindow = true;
psi.Arguments = "-uroot --password= myDb --routines> \"C:\\s.sql\"";
Process process = Process.Start(psi);
process.WaitForExit();
process.Close();
Which doesn't work at all. Instead I have to go for this which one can find all over the net, which works too.
ProcessStartInfo psi = new ProcessStartInfo();
psi.FileName = dumpUtilityPath;
psi.RedirectStandardInput = false;
psi.RedirectStandardOutput = true;
psi.UseShellExecute = false;
psi.CreateNoWindow = true;
psi.Arguments = string.Format("-R -u{0} --password={1} -h{2} {3} --routines", "root", "", "localhost", "myDb");
Process process = Process.Start(psi);
string output = process.StandardOutput.ReadToEnd();
process.WaitForExit();
process.Close();
using (StreamWriter writer = new StreamWriter("C:\\s.sql"))
{
writer.WriteLine(output);
writer.Close();
}
Why is that I need to use stream writer to get the database in an sql file which I can do otherwise directly from commands in command prompt?
What is the role of -R in the second block?
You can not redirect stdout using ">" in arguments because that is a feature of the command prompt.
-R includes the stored procedures and functions in the dump. See http://dev.mysql.com/doc/refman/5.1/en/mysqldump.html#option_mysqldump_routines for more information.
What you are doing in the command line version is using the shell to pipe the standard output to a file (the > command, followed by a file name, is a shorthand way of saying "take all of the standard output of this program and write it to this file"). To do the same thing from C#, you need to hand the standard output yourself and write it to a file.
The -R in the second example seems duplicative. According to this page, it is the same as --routines. Have you tried it without?
I thought I would include what the Arguments could look like programmatically, in our case we also wanted to dump the events of the DB to a file.
psi.Arguments = string.Format(#"-u{0} -p{1} -h{2} {3}", "someUser", "xxxxxx", "localhost", dbName, "--routines","--events");