我正在使用设置了以下所示代码的Visual Studio。输入两条路径后,将显示一条消息,说明现在将生成报告,其余脚本(以下未显示)将运行并生成工作簿。但是,如果输入的文件夹路径无效,Visual Studios会在“不存在”中进行同样的说明。我要发生的事情是,如果指定的路径存在,则弹出消息说它将像现在一样生成,并且作为控制台应用程序的一部分,因为它显示类似“路径无效,请再试一次”
Console.Write("Please enter the source path for the Checks Workbook, including the name of the file (Not including the file extension): ");
string checksPath;
checksPath = Console.ReadLine()+".xlsx";
Console.Write("Please enter the folder location you wish your report to go to (Not including the file extension): ");
string reportDest;
reportDest = Console.ReadLine()+".xlsx";
Console.WriteLine("Your report will now produce");
答案 0 :(得分:1)
您可以检查文件是否存在using System.IO.File.Exists("path")
答案 1 :(得分:0)
这似乎可行:
Console.Write("Please enter the folder location you wish your report to go to (Not including the file extension): ");
string reportDest;
reportDest = Console.ReadLine() + ".xlsx";
Console.WriteLine(File.Exists(reportDest) ? "Your report will now produce" : "Path does not exist - please try again");