[英]c# read each line containing CMD.EXE comand from text file and process it correctly
我在这里只需要一个特定的答案:我能够创建一个文件,其中包含我需要的命令和选项,例如每行所需的特定格式:
@"C:\mydosprog\mydosprog.exe" -o=option1 -option2
@"C:\mydosprog\mydosprog.exe" -o=option1 -option2
@"C:\mydosprog\mydosprog.exe" -o=option1 -option2
... and more lines
这是我正在使用的代码:
var launchmyfile = File.ReadAllLines(@"c:\foo\mycommands.txt");
for (int i = 0; i < inputLines.Length; i++)
System.Diagnostics.Process.Start(???????);
//this is where i'm battling and at the ??'s :-)
有一种简单有效的方法吗? (类似于dos批处理文件,但在c#中)如果是这样,怎么办?
我将不胜感激任何提示,技巧和答案
谢谢
您迭代inputLines
而不是launchmyfile
。
但是,您需要:
从文件中删除@符号,当它在字符串中并且在Process.Start的路径中无效时,它用作逐字修饰符没有意义。
保留引号,这些引号可用于区分命令行的路径,并且您需要在程序中将其分开
// test file
File.WriteAllLines(@"C:\TEMP\TEST.TXT", new string[] {
@"""c:\windows\system32\ping.exe"" -n 3 google.com",
@"""c:\windows\system32\ping.exe"" -n 3 google.com",
@"""c:\windows\system32\ping.exe"" -n 3 google.com",
});
var launchmyfile = File.ReadAllLines(@"C:\TEMP\TEST.TXT");
for (int i = 0; i < launchmyfile.Length; i++)
{
// 2nd " breaks the path from the command line
int commandLinePos = launchmyfile[i].IndexOf("\"", 1);
// get path
string executable = launchmyfile[i].Substring(1, commandLinePos - 1);
// get command line
string commandLine = launchmyfile[i].Substring(commandLinePos + 2);
// execute
System.Diagnostics.Process.Start(executable, commandLine);
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.