【发布时间】:2019-02-06 01:07:39
【问题描述】:
我有一个项目,我要求用户验证他们的机器上是否存在文件。他们输入路径,然后代码检查文件是否存在。
我想查看用户是否输入了特定的 txt 文件,如果是,则打开该文件。
我要查找的文件名是“SavingsAccount.txt”。 该文件可以在任何目录中。
Console.WriteLine("Please enter the file path location");
Console.WriteLine("e.g.C:\\Users\\acke9387\\Downloads\\file1.txt");
bool Exist = true;
while (Exist)
{
String file = Console.ReadLine();
bool DoesFileExist = System.IO.File.Exists(file);
if (DoesFileExist == true)
{
Console.WriteLine("File does exist");
if (file.Contains("SavingsAccount.txt"))
{
System.IO.File.ReadAllText(file);
}
}
if (DoesFileExist == false)
{
Console.WriteLine("File does not exist, Please enter a valid path.");
}
}
【问题讨论】:
-
你有什么问题?
标签: c# visual-studio file-exists