2017-06-16 73 views
2

我是使用System.IO的新手。向用户显示文件无法加载到程序的消息c#

我有一个应用程序从Web抓取Json文件,只抓取部分数据以显示在Windows应用程序窗体的控件上。 表单允许用户将数据保存为新文件并加载文件,只要它包含我在保存文件时添加的“指示器”,该文件告诉程序它已被我的程序保存。

一切正常。

只要不包含该指示器的文件被加载到程序中,它就不会显示任何内容,这是我希望它执行的操作,但我也希望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以再次尝试。

+0

你想要的文件包含一条线,你必须取值为Indicator'还是要跨越从文件中的所有行只是一个单一的'Indicator'价值? – Deadzone

+0

第一个。 当我打开已创建它的显示,如文本文件: (只是一个例子) 指标,内容:1,数据2:字, – BeginnerCoder

+0

这被认为是一个有效的文件: 线路1:“SomeText”则会 2号线:“Indicator” ... – Deadzone

回答

0

我这样做,而不是和它的工作对我的应用程序

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"); 
     } 
    } 
} 
+0

这就是我在你的问题下的评论中所要求的。猜猜我不明白你的意思,我得到的印象是这个代码不会工作,因为不是每一行都必须包含“Indicator”,这只需要一行就可以包含它:)。 – Deadzone

0

Contains区分大小写。试试这个为你的评价:

line.IndexOf("Indicator", StringComparison.InvariantCultureIgnoreCase) >= 0 
2

您可以包装foreach到,如果将使用一些LINQ代码语句,以确定是否所有行包含“指标”:

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."); 
} 
+0

是否在line =>行内声明了一个新变量?否则,我不明白它来自哪里。 – BeginnerCoder

+0

@BeginnerCoder这被称为lambda表达式,它在内部使用'foreach'循环操作,因此这基本上是这里的'line' foreach(行中的var行)'。我可以包含一个foreach例子来说明它是如何工作的。 – Deadzone

+0

我跟着你想做的事情,但没有使用lambda,并得到它的工作。感谢灯泡时刻! – BeginnerCoder