當文件存在時,我需要向用戶顯示一些消息,顯示消息「文件存在...是否要覆蓋它?」用戶選項是/否
if (File.Exists(binaryFilePath))
{
Program.DisplayMessage("The file: " + binaryFileName + " exist. You want to overwrite it? Y/N");
string overwrite = Console.ReadLine();
while (overwrite != null)
{
if (overwrite.ToUpper() == "Y")
{
WriteBinaryFile(frameCodes, binaryFilePath);
} if (overwrite.ToUpper() == "N")
{
throw new IOException();
overwrite = null;
} if (overwrite.ToUpper() != "Y" && overwrite.ToUpper() != "N")
{
Program.DisplayMessage("!!Please Select a Valid Option!!");
overwrite = Console.ReadLine();
}
}
}
如果用戶寫入「Y」的過程開始並完成正常......問題是怎麼可以停止? 我試着用這個,但不行...
我該怎麼做?
請不要恢復人們的編輯。他們正在努力改進代碼的格式,以使問題更具可讀性並因此可以回答。 – ChrisF 2011-04-25 22:32:40
爲什麼你使用while循環呢?如果用戶回答否,然後將覆蓋設置爲空,爲什麼會拋出異常? – esrange 2011-04-25 22:32:44
@esrange在輸入有效的選項之前,它似乎是指ReadLine(),然後通過將覆蓋設置爲null來從循環中斷開。 @ale break;是擺脫循環的方式。 – adorablepuppy 2011-04-25 22:35:26