C# 如何在visual studio中编写命令应用程序以读取批处理文件
我正在VisualStudio中创建我的第一个命令应用程序 我想知道从批处理文件中读取行的命令是什么。我需要能够读取批处理文件的第一行,然后使用从第一行获得的参数调用一些方法,之后我需要能够读取批处理文件的第二行并调用相同的方法,依此类推,直到文件结束C# 如何在visual studio中编写命令应用程序以读取批处理文件,c#,batch-file,command,C#,Batch File,Command,我正在VisualStudio中创建我的第一个命令应用程序 我想知道从批处理文件中读取行的命令是什么。我需要能够读取批处理文件的第一行,然后使用从第一行获得的参数调用一些方法,之后我需要能够读取批处理文件的第二行并调用相同的方法,依此类推,直到文件结束 我已经知道如何调用方法。我只需要知道如何读取批处理文件。批处理文件是文本文件,因此您可以执行以下操作: string[] lines = File.ReadAllLines(filename); 或者,如果您想懒洋洋地阅读(在.net 4中提供
我已经知道如何调用方法。我只需要知道如何读取批处理文件。批处理文件是文本文件,因此您可以执行以下操作:
string[] lines = File.ReadAllLines(filename);
或者,如果您想懒洋洋地阅读(在.net 4中提供):
IEnumerable lines=File.ReadLines(文件名);
但由于批处理文件通常相当小,我最有可能使用ReadAllLines
如果要将命令行参数传递给应用程序,请使用
Environment.GetCommandLineArgs
获取它们。是否确实要读取批处理文件?或者,您希望参数从调用它的批处理文件传递到程序中吗?
using(StreamReader batchReader = new StreamReader("path to batch file"))
{
string batchCommand;
while(!batchReader.EndOfStream)
{
batchCommand = batchReader.ReadLine();
// do your processing with batch command
}
}
using(StreamReader batchReader = new StreamReader("path to batch file"))
{
string batchCommand;
while(!batchReader.EndOfStream)
{
batchCommand = batchReader.ReadLine();
// do your processing with batch command
}
}