我正在创建一个小型的C#程序,该程序从用户选择的位置读取纺织品..我设法正确地读取了这些文件,但是我想如果错误地输入文件名/路径....或文件类型不正确,则向用户显示错误消息。 我已经尝试了有限的知识内的一切,现在我有点卡住了。任何帮助都将非常感激。由于检查用户是否输入了正确的文件系统路径
using System;
class ReadFromFile
{
static void Main()
{
Console.WriteLine ("Welcome to Decrypter (Press any key to begin)");
Console.ReadKey();
//User selects file they wish to decrypt
int counter = 0;
string line;
string path;
Console.WriteLine ("\nPlease type the path to your file");
path = Console.ReadLine();
// Read the file and display it line by line.
System.IO.StreamReader file =
new System.IO.StreamReader (path);
while ((line = file.ReadLine()) != null) {
Console.WriteLine (line);
counter++;
}
file.Close();
// Suspend the screen.
Console.ReadLine();
}
}
如果您只想查看路径是否有效以及文件是否存在,可以使用['File.Exists'](https://msdn.microsoft.com/zh-cn/library/system.io .file.exists(v = vs.110).aspx) – juharr