【发布时间】:2019-03-07 22:29:21
【问题描述】:
我正在尝试制作一个从另一个应用程序加载配置文件的程序。 如果文件存在,它会加载它并显示一条消息,但如果配置文件无效,它会显示一条错误消息,然后打开一个对话框以加载正确的文件。但是如果用户重新加载了错误的文件,同样的对话框应该会再次出现,但那是我的代码失败的时候。
同样,如果文件从一开始就不存在,它会显示一个对话框来加载文件,但是如果给出取消对话框或再次选择错误的文件,我的代码就会失败。
我知道解决方案是使用循环,但我不确定如何构建它。 pd: searchfile() 是我打开对话框的函数,readconfig() 是我读取另一个应用程序配置文件的函数。
strfilenamepath = @"C:\Users\test\dogs.exe.config";
if (File.Exists(strfilenamepath))
{
onlyFilename = System.IO.Path.GetFileName(strfilenamepath);
textBox1.Text = onlyFilename;
try
{
string[] valores = readConfig(strfilenamepath);
MessageBox.Show(valores[0] + valores[1] + valores[2]);
}
catch (Exception ex)
{
MessageBox.Show("Error loading config file." + ex.Message);
searchFile();
onlyFilename = System.IO.Path.GetFileName(strfilenamepath);
textBox1.Text = onlyFilename;
string[] valores = readConfig(strfilenamepath);
MessageBox.Show(valores[0] + valores[1] + valores[2]);
}
}
else
{
searchFile();
onlyFilename = System.IO.Path.GetFileName(strfilenamepath);
textBox1.Text = onlyFilename;
try
{
readConfig(strfilenamepath);
string[] valores = readConfig(strfilenamepath);
MessageBox.Show(valores[0] + valores[1] + valores[2]);
}
catch (Exception ex)
{
MessageBox.Show("Error loading config file." + ex.Message);
searchFile();
onlyFilename = System.IO.Path.GetFileName(strfilenamepath);
textBox1.Text = onlyFilename;
string[] valores = readConfig(strfilenamepath);
MessageBox.Show(valores[0] + valores[1] + valores[2]);
}
}
【问题讨论】:
-
循环在哪里?
searchFile()是什么? -
我没有使用循环,因为我不知道如何在我的代码中构造它。 Searchfile() 只是一个简单的函数,它打开一个对话框并将路径保存在一个变量中。
-
你不想要一个循环。只需向用户发布无效的消息并让用户单击按钮重试即可。
标签: c# loops while-loop dialog