C# 使用c捕获文件异常#
下面是我今天使用Visual Studio控制台应用程序所做的努力 我想要发生的是,当控制台应用程序打开时,我输入第一个“checksPath”,如果结果不存在,我希望它说路径错误,或者让用户重试,或者关闭应用程序。如果路径有效,则它将移动到下一个“reportDest”,并且同样适用。如果它是一个无效路径,我希望有一条消息这样说,并选择再试一次,或关闭应用程序。如果输入的两条路径(最终)都有效,我希望有一条消息告诉您,报告现在将生成。生成报告的脚本的其余部分非常好,只是我在下面所说的那一点很麻烦C# 使用c捕获文件异常#,c#,visual-studio,exception-handling,C#,Visual Studio,Exception Handling,下面是我今天使用Visual Studio控制台应用程序所做的努力 我想要发生的是,当控制台应用程序打开时,我输入第一个“checksPath”,如果结果不存在,我希望它说路径错误,或者让用户重试,或者关闭应用程序。如果路径有效,则它将移动到下一个“reportDest”,并且同样适用。如果它是一个无效路径,我希望有一条消息这样说,并选择再试一次,或关闭应用程序。如果输入的两条路径(最终)都有效,我希望有一条消息告诉您,报告现在将生成。生成报告的脚本的其余部分非常好,只是我在下面所说的那一点很麻
string checksPath;
Console.Write("Please enter the source path for the Checks Workbook, including the name of the file (Not including the file extension): ");
checksPath = Console.ReadLine() + ".xlsx";
try
{
if (File.Exists("checksPath"))
throw new FileNotFoundException();
}
catch (FileNotFoundException e)
{
Console.WriteLine("Invalid path - Please close the app and try again!");
Console.ReadLine();
}
string reportDest;
Console.Write("Please enter the folder location and file you wish your report to go to (Not including the file extension): ");
reportDest = Console.ReadLine() + ".xlsx";
try
{
if (File.Exists("reportDest"))
throw new FileNotFoundException();
}
catch (FileNotFoundException e)
{
Console.WriteLine("Invalid path - Please close the app and try again!");
Console.ReadLine();
}
Console.WriteLine("Your report will now produce");
因为你需要不断地问一个问题,直到用户答对为止,所以你需要一个循环。接下来,在该循环中,您需要检查路径是否存在
bool run = true;
while (run)
{
Console.Clear();
Console.WriteLine("Enter Path:");
string answer = Console.ReadLine();
if (Directory.Exists(answer)) run = false;
else
{
Console.WriteLine("Path Does not exists. Try again. Press enter to continue...");
Console.ReadLine();
}
}
因为你需要不断地问一个问题,直到用户答对为止,所以你需要一个循环。接下来,在该循环中,您需要检查路径是否存在
bool run = true;
while (run)
{
Console.Clear();
Console.WriteLine("Enter Path:");
string answer = Console.ReadLine();
if (Directory.Exists(answer)) run = false;
else
{
Console.WriteLine("Path Does not exists. Try again. Press enter to continue...");
Console.ReadLine();
}
}
异常处理适用于异常情况。在这种情况下,您应该使用if
/else
来处理预期的条件。如果(!File.Exists(“checksPath”)异常处理适用于异常情况,那么如何处理。在这种情况下,您应该使用if
/else
来处理预期条件。如果(!File.Exists(“checksPath”),