【问题标题】:C# Application not opening due to StreamReader由于 StreamReader,C# 应用程序未打开
【发布时间】:2015-04-10 09:48:12
【问题描述】:

我有一个项目,我必须将各种约会加载到日历应用程序中。当我实现这个加载方法时,我应该能够以以下格式将我的数据写入text file

10/04/2015 '\t' 10:40 '\t' 30 '\t' test '\t' test.

'\t' = tab

解决方案构建成功,但是当我在没有调试的情况下启动时,应用程序在后台打开并且不显示在屏幕上。然后我必须去任务管理器并结束该过程。

申请表未显示。我的应用程序在不使用此加载方法的情况下运行良好,但是我需要能够预先填充我的日历以及手动输入约会。

   StreamReader fileReader;
   string line;
   DateTime nStart;
   DateTime nDate;
   int nLength;
   string nSubject;
   string nLocation;
   bool result;



public bool Load()
        {
            fileReader = new StreamReader("D:\\All Downloads\\CalendarApp\\Apps.txt");
        line = fileReader.ReadLine();
        while (line != null)
        {
            string s = line;
            string[] split = s.Split('\t');                
            nDate = Convert.ToDateTime(split[0]);
            nStart = Convert.ToDateTime(split[1]);
            nLength = Convert.ToInt32(split[2]);
            nSubject = split[3];
            nLocation = split[4];
            IAppointment temp = new Appointment(nSubject, nLocation, nStart, nLength, nDate);
            listAppointments.Add(temp);
        }

        fileReader.Close();


        if (listAppointments == null)
        {
            result = false;
        }
        else
        {
            result = true;
        }
        return result;

【问题讨论】:

  • 您的fileReader.ReadLine() 调用必须包含在while 子句中。使用 using 语句来结束 StreamReader 构造是一个很好的做法。而且,你应该在你的方法中声明你的变量什么时候才会被使用。
  • 学习调试。点击 Visual Studio 中的暂停按钮,看看它在哪里。然后单步执行你的代码,看看为什么它会停留在那个循环中。

标签: c# winforms streamreader


【解决方案1】:

在调试时启动会发生什么?

你有一个无限循环。

尝试在while 循环的底部添加另一个对line = fileReader.ReadLine(); 的调用。

或者把你的循环改成这样:

while ((line = fileReader.ReadLine()) != null)
{
    ...
}

另外,最后 9 行:

if (listAppointments == null)
{
    result = false;
}
else
{
    result = true;
}
return result;

可以这样写:

return listAppointments != null;

但是,您没有显示listAppointments 的声明或初始化位置。如果确实是null,那么在循环中调用listAppointments.Add 时会出错。

你可能想要这个:

return listAppointments.Count > 0;

刚刚再次查看了您的代码。如果您只期望一行文本,那么您根本不需要 while 循环。如果有不止一行,那么你只会从最后一行中获取值,如果最后有一个空行,那肯定会在你的代码中引发错误。

整个方法应该看起来更像这样(带有变量的方法中定义的任何变量未在其他地方列出):

public bool Load()
{
    using (var fileReader = new StreamReader(@"D:\All Downloads\CalendarApp\Apps.txt"))
    {
        var line = fileReader.ReadLine();
        if (line != null)
        {
            string s = line;
            string[] split = s.Split('\t');                
            nDate = Convert.ToDateTime(split[0]);
            nStart = Convert.ToDateTime(split[1]);
            nLength = Convert.ToInt32(split[2]);
            nSubject = split[3];
            nLocation = split[4];
            listAppointments.Add(new Appointment(nSubject, nLocation, nStart, nLength, nDate));
            return true;
        }
    }
    return false;
}

如果在尝试转换为DateTime 时仍然出错,则搜索解析DateTime 值的有效方法。

【讨论】:

  • 嗨,Dennis,非常感谢它完美运行,我的应用程序现在启动了。我移动了'line = fileReader.ReadLine();'进入我的while循环,现在它打开约会,没有预先填充的数据,当我去创建一个新约会时,它会抛出一个异常,说“字符串未被识别为有效的日期时间。
  • IList listAppointments = new List();是这样声明的
  • 感谢您的回复。我尝试了您的 Load 方法版本,并将我的成员变量移到了该方法中。现在我的应用程序打开到主屏幕,它最初不显示约会,直到我点击今天的日期到另一个日期,然后回到今天的日期。所以我认为这可能与我的 Clear 方法有关。 “字符串 oStart = comboBox2.SelectedItem.ToString(); start = DateTime.ParseExact(oStart, "dd MM yyyy HH:mm", System.Globalization.CultureInfo.InvariantCulture);"是我想使用的格式,但是当
  • 再次感谢丹尼斯,你帮了大忙。
  • 很高兴我能帮上忙。随意发布一个关于您的日期时间解析问题的新问题。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2016-09-15
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多