两个文本文件的命令行参数
本文关键字:文件 命令行 参数 文本 两个 | 更新日期: 2023-09-27 18:18:14
我需要帮助我如何打开以下两个文本文件,answerfile和outputfile,使用命令行参数:
int counter = 0;
string line;
// Read the file and display it line by line.
System.IO.StreamReader file = new System.IO.StreamReader(@"c:'AnswerFile.txt");
System.IO.StreamWriter fileWriter = new System.IO.StreamWriter(@"C:'outputFile.txt");
while ((line = file.ReadLine()) != null)
{
System.Console.WriteLine(line);
fileWriter.WriteLine(line);
counter++;
}
file.Close();
fileWriter.Close();
System.Console.WriteLine("There were {0} lines.", counter);
// Suspend the screen.
System.Console.ReadLine();
任何帮助都是感激的!谢谢你
就像
System.IO.StreamReader file = new System.IO.StreamReader(args[0]);
System.IO.StreamWriter fileWriter = new System.IO.StreamWriter(args[1]);
或者如果Main函数中没有string[] args
,则
var args = Environment.GetCommandLineArgs();
假设您的程序名为prog.exe
,启动为C:'>prog.exe infile.txt outfile.txt
尝试以下更改,这两行代码将保持不变。
//System.IO.StreamReader file = new System.IO.StreamReader(@"c:'AnswerFile.txt");
//System.IO.StreamWriter fileWriter = new System.IO.StreamWriter(@"C:'outputFile.txt");
if (args.Length < 2)
{
throw new Exception("File(s) not provided.");
}
System.IO.StreamReader file = new System.IO.StreamReader(args[0]);
System.IO.StreamWriter fileWriter = new System.IO.StreamWriter(args[1]);
编译代码(假设它创建了一个文件,例如demo.exe)。现在打开命令提示符,转到demo.exe所在的位置。输入以下命令
demo.exe "c:'AnswerFile.txt" "C:'outputFile.txt"