我需要帮助我如何使用命令行参数打开以下两个文本文件,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();
任何帮助表示赞赏!谢谢
答案 0 :(得分:1)
这将是
System.IO.StreamReader file = new System.IO.StreamReader(args[0]);
System.IO.StreamWriter fileWriter = new System.IO.StreamWriter(args[1]);
或者如果主要功能上没有string[] args
,那么
var args = Environment.GetCommandLineArgs();
假设您的程序名为prog.exe
并以C:\>prog.exe infile.txt outfile.txt
答案 1 :(得分:0)
在代码的这两行中尝试以下更改将是相同的。
//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]);
答案 2 :(得分:0)
编译代码(考虑它创建一个文件说demo.exe)。现在打开命令提示符并转到demo.exe存在的位置。放下以下的commad。
demo.exe "c:\AnswerFile.txt" "C:\outputFile.txt"
希望它能奏效。