由于 StreamReader,C# 应用程序未打开
C# Application not opening due to StreamReader
我有一个项目,我必须在其中将各种约会加载到日历应用程序中。当我实现这个加载方法时,我应该能够按照以下格式在 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;
调试时启动会发生什么?
你有一个无限循环。
尝试在 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
值的有效方法。
我有一个项目,我必须在其中将各种约会加载到日历应用程序中。当我实现这个加载方法时,我应该能够按照以下格式在 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;
调试时启动会发生什么?
你有一个无限循环。
尝试在 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
值的有效方法。