向用户显示无法将文件加载到程序 c# 的消息
Display message to user that a file cannot be loaded to program c#
我刚开始使用 System.IO。
我有一个应用程序可以从 Web 抓取一个 Json 文件,并且只抓取部分数据显示在 Windows 申请表的控件上。
该表单允许用户将数据保存为新文件并加载文件,前提是它仅包含我在保存文件时添加的 "indicator",告诉程序它是由我的程序保存的。
一切正常。
每当将不包含该指标的文件加载到程序时,它什么都不显示,这正是我想要它做的,但我也希望弹出 Messagebox.Show()并让用户知道为什么值是空的以及为什么什么都没有发生。
if(openFile.ShowDialog() == DialogResult.OK)
{
string dataLine = string.Empty;
using (StreamReader read = new StreamReader(File.OpenRead(openFile.FileName)))
{
dataLine = read.ReadToEnd();
string[] beginningOfData = dataLine.Split(',');
string temporary = string.Empty;
foreach (string line in beginningOfData)
{
//Indicator
if(line.Contains("Indicator")
{
temporary = line.substring(9);
//Goes through the rest of the file
//Converts data to control value and adds it
}
else
{
//Where I tried to display the message box
}
}
}
}
这是我试过的,但它也没有像我想要的那样工作
else
{
MessageBox.Show("Can't load data.");
}
它仍然会显示 MessageBox,即使它读取指示器在那里并显示相应控件内的数据。此外,每当我尝试关闭 MessageBox 时,它都会再次显示它。
所以我决定改为:
else if(!line.contains("Indicator"))
{
MessageBox.Show("Can't load data.");
break;
}
也是这样:
else
{
if(!line.contains("Indicator"))
{
MessageBox.Show("Can't load data.");
break;
}
}
我还尝试通过
使其更具体
if(line.contains("Indicator") == false)
{
//Code here
}
但即使该文件是由程序创建的,它仍然会显示它。
休息;确实阻止了消息框再次出现,但我只希望 MessageBox 在它是不正确的文本文件(不包含指示器)时显示,并允许我关闭 MessageBox 再试一次。
Contains
区分大小写。试试这个进行评估:
line.IndexOf("Indicator", StringComparison.InvariantCultureIgnoreCase) >= 0
您可以将 foreach
包装到一个 if 语句中,该语句将使用一些 LINQ 代码来确定是否所有行都包含 "indicator":
if (beginningOfData.All(line => line.ToLower().Contains("indicator")))
{
string temporary = string.Empty;
foreach (string line in beginningOfData)
{
temporary = line.Substring(9);
//Goes through the rest of the file
//Converts data to control value and adds it
}
}
else
{
System.Windows.Forms.MessageBox.Show("Can't load data.");
}
我这样做了,它适用于我的应用程序
if(openFile.ShowDialog() == DialogResult.OK)
{
string dataLine = string.Empty;
using (StreamReader read = new StreamReader(File.OpenRead(openFile.FileName)))
{
//Changed happened here
dataLine = read.ReadToEnd();
string[] beginningOfData = dataLine.Split(',');
string temporary = string.Empty;
if(beginningOfData.Contains("Indicator"))
{
temporary = dataLine.Substring(9);
foreach(string realData in beginningOfData)
{
//Goes through file
}
}
else
{
MessageBox.Show("Can't load data");
}
}
}
我刚开始使用 System.IO。
我有一个应用程序可以从 Web 抓取一个 Json 文件,并且只抓取部分数据显示在 Windows 申请表的控件上。 该表单允许用户将数据保存为新文件并加载文件,前提是它仅包含我在保存文件时添加的 "indicator",告诉程序它是由我的程序保存的。
一切正常。
每当将不包含该指标的文件加载到程序时,它什么都不显示,这正是我想要它做的,但我也希望弹出 Messagebox.Show()并让用户知道为什么值是空的以及为什么什么都没有发生。
if(openFile.ShowDialog() == DialogResult.OK)
{
string dataLine = string.Empty;
using (StreamReader read = new StreamReader(File.OpenRead(openFile.FileName)))
{
dataLine = read.ReadToEnd();
string[] beginningOfData = dataLine.Split(',');
string temporary = string.Empty;
foreach (string line in beginningOfData)
{
//Indicator
if(line.Contains("Indicator")
{
temporary = line.substring(9);
//Goes through the rest of the file
//Converts data to control value and adds it
}
else
{
//Where I tried to display the message box
}
}
}
}
这是我试过的,但它也没有像我想要的那样工作
else
{
MessageBox.Show("Can't load data.");
}
它仍然会显示 MessageBox,即使它读取指示器在那里并显示相应控件内的数据。此外,每当我尝试关闭 MessageBox 时,它都会再次显示它。
所以我决定改为:
else if(!line.contains("Indicator"))
{
MessageBox.Show("Can't load data.");
break;
}
也是这样:
else
{
if(!line.contains("Indicator"))
{
MessageBox.Show("Can't load data.");
break;
}
}
我还尝试通过
使其更具体if(line.contains("Indicator") == false)
{
//Code here
}
但即使该文件是由程序创建的,它仍然会显示它。
休息;确实阻止了消息框再次出现,但我只希望 MessageBox 在它是不正确的文本文件(不包含指示器)时显示,并允许我关闭 MessageBox 再试一次。
Contains
区分大小写。试试这个进行评估:
line.IndexOf("Indicator", StringComparison.InvariantCultureIgnoreCase) >= 0
您可以将 foreach
包装到一个 if 语句中,该语句将使用一些 LINQ 代码来确定是否所有行都包含 "indicator":
if (beginningOfData.All(line => line.ToLower().Contains("indicator")))
{
string temporary = string.Empty;
foreach (string line in beginningOfData)
{
temporary = line.Substring(9);
//Goes through the rest of the file
//Converts data to control value and adds it
}
}
else
{
System.Windows.Forms.MessageBox.Show("Can't load data.");
}
我这样做了,它适用于我的应用程序
if(openFile.ShowDialog() == DialogResult.OK)
{
string dataLine = string.Empty;
using (StreamReader read = new StreamReader(File.OpenRead(openFile.FileName)))
{
//Changed happened here
dataLine = read.ReadToEnd();
string[] beginningOfData = dataLine.Split(',');
string temporary = string.Empty;
if(beginningOfData.Contains("Indicator"))
{
temporary = dataLine.Substring(9);
foreach(string realData in beginningOfData)
{
//Goes through file
}
}
else
{
MessageBox.Show("Can't load data");
}
}
}