部分功能代码未执行
我有一个用 C# 构建的 WPF 程序,其部分功能是填充两个 ListBox 控件。在 Window_Loaded 事件中,程序调用 Update() 函数来填充 ListBox,但它只执行该函数的一部分。如果我在向文件追加内容后调用该函数,该函数甚至会崩溃。该文件的结构如下:
第一个列表框的内容 content_for_second_listbox
函数更新如下:
private void UpdateURL()
{
StreamReader rdr = new StreamReader("Links.db");
string line;
int dummy;
URL.Clear();
TAGS.Clear();
while ((line = rdr.ReadLine()) != null)
{
dummy = line.IndexOf(' ');
URL.Add(line.Substring(0, dummy));
TAGS.Add(line.Substring(dummy + 1));
}
rdr.Close();
LLinks.Items.Clear();
LTags.Items.Clear();
for (int a = 0; a < URL.Count; a++)
{
LLinks.Items.Add(new ListBoxItem().Content = URL[a]);
LTags.Items.Add(new ListBoxItem().Content = TAGS[a]);
}
}
它在第一个循环后停止执行,我通过调试发现了这一点。这是追加函数
private void LBookmarkBT_Click(object sender, RoutedEventArgs e)
{
StreamWriter wrt = new StreamWriter("Links.db", true);
wrt.Write("\n" + LURLTBox.Text + " " + LTagsTBox.Text);
wrt.Close();
UpdateURL();
}
有什么想法吗?
更新:问题是它正在读取所有行并正确解析它们,但随后它会突然读取换行符,行字符串变成“”,因此 dummy = -1 并且抛出长度异常。我设法通过将循环放入 try-catch 块并丢弃异常来克服这个问题,一切正常,但如果我可以这么说,它似乎有点“肮脏”。我应该怎么办 ?也许 if(dummy == -1) 不解析该行?
try
{
while ((line = rdr.ReadLine()) != null)
{
dummy = line.IndexOf(' ');
URL.Add(line.Substring(0, dummy));
TAGS.Add(line.Substring(dummy + 1));
}
}
catch (Exception e)
{ }
I have a WPF program built in C#, and part of it's function is populating two ListBox controls. At the Window_Loaded event the program calls the Update() function in order to populate the ListBoxes, but it only executes part of the function. And if I call the function after an append has been made to the file, the function will even crash. The file is structured as follows :
content_for_first_listbox
content_for_second_listbox
The function Update is as follows :
private void UpdateURL()
{
StreamReader rdr = new StreamReader("Links.db");
string line;
int dummy;
URL.Clear();
TAGS.Clear();
while ((line = rdr.ReadLine()) != null)
{
dummy = line.IndexOf(' ');
URL.Add(line.Substring(0, dummy));
TAGS.Add(line.Substring(dummy + 1));
}
rdr.Close();
LLinks.Items.Clear();
LTags.Items.Clear();
for (int a = 0; a < URL.Count; a++)
{
LLinks.Items.Add(new ListBoxItem().Content = URL[a]);
LTags.Items.Add(new ListBoxItem().Content = TAGS[a]);
}
}
It stops executing after the first loop, I found that out from debugging. And here is the append function
private void LBookmarkBT_Click(object sender, RoutedEventArgs e)
{
StreamWriter wrt = new StreamWriter("Links.db", true);
wrt.Write("\n" + LURLTBox.Text + " " + LTagsTBox.Text);
wrt.Close();
UpdateURL();
}
Any ideas ?
Update : The problem is that it was reading the all the lines and parsed them correctly but then it would read a newline out of nowhere, the line string became "" and therefore dummy = -1 and it threw a length exception. I managed to overcome this by putting the loop in a try-catch block and discarding the exception, and everything works fine, but it seems kind of, "dirty" if I may say so. What should I do ? Perhaps and if(dummy == -1) don't parse the line ?
try
{
while ((line = rdr.ReadLine()) != null)
{
dummy = line.IndexOf(' ');
URL.Add(line.Substring(0, dummy));
TAGS.Add(line.Substring(dummy + 1));
}
}
catch (Exception e)
{ }
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
尝试在“异常”对话框中启用“抛出异常时中断”运行(VS.Net / Debug\Exceptions... 菜单选项中的 Ctrl+Alt+E)。当抛出异常时,这应该会破坏调试器 - 这几乎肯定是您在这里得到的结果。
Try running with "Break when an exception is thrown" enabled in the Exceptions dialog (Ctrl+Alt+E in VS.Net / Debug\Exceptions... menu option). This should break the debugger when an exception is thrown - which is almost certainly what you're getting here.