当前位置:网站首页>Bat 使用细节2
Bat 使用细节2
2022-06-30 06:27:00 【警醒与鞭策】
/
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.
/
/
边栏推荐
- Who doesn't want a blog site of their own - build a blog site WordPress
- Zibll sub theme v6.4.1wordpress open source download_ Crack the original / use it directly / no tutorial required
- Installing googleplay environment on Huawei mobile phones
- Win10 /11 开热点无法上网问题
- Base64详解:玩转图片Base64编码
- Redux source code implementation
- ES6 deconstruction assignment
- A complete performance test process
- 【微信小程序:单选、多选样式,背景色,圆角】
- Dynamic routing job
猜你喜欢
随机推荐
Is it safe to open an account online? Can you open an account to speculate on the Internet?
Subnet division and subnet summary
ini解析學習文檔
Cocos studio3.1 installation package win
Picture.....
ES6 array
图像处理7-图像增强
Traitement d'images 7 - amélioration d'images
How does Altium designer hide some temporarily unnecessary classes, such as GND
接口中方法详解
旋转标注工具roLabelImg
DOM (document object model) document XML file object model
Wechat applet mall project
Installation and initialization of MariaDB database
Connect to remote server
Ten years' miscellaneous thoughts
C language final experiment report (student achievement management system) source code
1.5 - 逻辑运算
1.9 - 存储器的分类
Unclear about glide loading picture








![[wechat applet: single or multiple styles, background color, rounded corners]](/img/01/0381915591b0d3c13afbba4d50952d.png)
