/
c# 调用cmd.exe 调用 x.bat
I've been trying all day to start process which would run the following code:
C:\bin\ant.bat -f=C:\build.xml -DinputFile=C:\Desktop\Book1.xml -DstartDate=2018-06-20 -DxslFile=ProcessingDate -DoutputFile=fff and it works completely fine in cmd. What I have tried: this is my last code in C# which I really hoped would work, but however it doesn't:
public void run() { string antFile = @"C:\ant.bat"; string build = @"C:\build.xml"; string inputFile = @"C:\Book1.xml"; string startDate = "2018-05-23"; string outputFile = "ff"; ProcessStartInfo procStartInfo = new ProcessStartInfo("cmd.exe", "/c" + @"C:bin\ant.bat -f=C:\build.xml -DinputFile=C:\Desktop\Book1.xml -DstartDate=2018-06-20 -DxslFile=ProcessingDate -DoutputFile=test0.xsl"); Process proc = new Process(); proc.StartInfo = procStartInfo; proc.Start(); ProcessStartInfo procStartInfo2 = new ProcessStartInfo("cmd.exe", "/c" + antFile + "-f=" + build + "-DinputFile=" + inputFile + "-DstartDate=" + startDate + "-DxslFile=" + startDate + "-DoutputFile=" + outputFile); Process proc2 = new Process(); proc2.StartInfo = procStartInfo2; proc2.Start(); }
2 solutions
1、Your assembled command line looks like this:
cmd.exe /cC:\ant.bat-f=C:\build.xml-DinputFile=C:\Book1.xml-DstartDate=2018-05-23-dXslFile=2018-05-23-DoutputFile=ff Just concatenating strings together does not automatically put spaces in the appropriate places for you. You have to account for that yourself. Also, it greatly helps debugging if you put the strings into variables instead of directly assembling them as an argument in the function call:
string arguments=$"/c {antFile} -f={build} -DinputFile={inputFile} -DstartDate={startDate} -DxslFile={startDate} -DoutputFile={outputFile}"; ProcessStartInfo procStartInfo2 = new ProcessStartInfo("cmd.exe", arguments);
2、
public static void RunCmd(string executeString) { ProcessStartInfo processStartInfo = new ProcessStartInfo(executeString); processStartInfo.RedirectStandardOutput = true; processStartInfo.RedirectStandardError = true; processStartInfo.UseShellExecute = false; Process process = new Process(); process.StartInfo = processStartInfo; process.Start(); process.WaitForExit(); if (process.ExitCode == -1) throw new Exception(process.StandardOutput.ReadToEnd()); }
///
Let’s imagine a simple app that will allow the user to change its resolution and window mode through the command line. For this example to work, we prepared a CommandLineController script that we attach to any object on the scene.
using System; using UnityEngine; public class CommandLineController : MonoBehaviour { #region MEMBERS private const string windowModeArg = "isWindowedMode="; private const string resolutionWidthArg = "width="; private const string resolutionHeightArg = "height="; [SerializeField] private int defaultWidth = 1920;
[SerializeField] private int defaultHeight = 1080; #endregion #region PROPERTIES public int DefaultHeight { get { return defaultHeight; } } public int DefaultWidth { get { return defaultWidth; } } #endregion #region FUNCTIONS void Start() { ParseCommandLineArguments(); } private void ParseCommandLineArguments() { string[] args = System.Environment.GetCommandLineArgs(); int screenWidth = DefaultWidth; int screenHeight = DefaultHeight; bool isWindowsMode = false; string argumentString;
for (int i = 0; i < args.Length; i++) { argumentString = ""; if (args[i].StartsWith(windowModeArg) == true) { argumentString = args[i].Replace(windowModeArg, ""); Boolean.TryParse(argumentString, out isWindowsMode) } else if (args[i].StartsWith(resolutionWidthArg) == true) { argumentString = args[i].Replace(resolutionWidthArg, ""); Int32.TryParse(argumentString, out screenWidth); } else if (args[i].StartsWith(resolutionHeightArg) == true) { argumentString = args[i].Replace(resolutionHeightArg, ""); Int32.TryParse(argumentString, out screenHeight); } } Screen.SetResolution(screenWidth, screenHeight, isWindowsMode == false); } #endregion #region CLASS_ENUMS #endregion }
Upon start, the script checks for any command-line arguments. If it finds any arguments fitting the width, height or is windowed mode parameters, it tries to parse them and then change the screen resolution and FullScreen mode accordingly.
Because Unity stores the last set resolution for the player in the registry, we declared two fields that store the default width and height for the script to use.
So, if our default resolution is 1920×1080 and the app is in FullScreen mode, launching it with the following parameters:
C:\Users\<user name>\Desktop\CommandLine\CommandLineExample.exe width=640 height=480 isWindowedMode=true
will cause the app to change the resolution to 640×480 and display in a window.
As we can see, command line parameters can be used to customize any aspect of the app upon launch. By removing the need for preparing multiple different builds for different parties like testers or clients, this very handy feature saves us time and makes it easier to manage build configurations.
From resolution changes and developer-mode options to more robust server configurations – accessing the parameters is very simple and can be easily integrated into an existing app.
/
bat路径中有空格
例如bat文件中写
C:/Program Files (x86)/Google/Chrome/Application/chrome.exe ./html/index.html
pause
会报错,'C:/Program' 不是内部或外部命令。问题出在路径中的空格,将整个路径打上双引号就行了。即
"C:/Program Files (x86)/Google/Chrome/Application/chrome.exe" ./html/index.html
pause
/
怎么在Bat 里 休眠
The correct way to sleep in a batch file is to use the timeout command, introduced in Windows 2000.
timeout /t 30
The timeout would get interrupted if the user hits any key; however, the command also accepts the optional switch /nobreak
, which effectively ignores anything the user may press, except an explicit CTRL-C
:
timeout /t 30 /nobreak
Additionally, if you don't want the command to print its countdown on the screen, you can redirect its output to NUL
:
timeout /t 30 /nobreak > NUL
//
//