部分功能代码未执行

发布于 2024-09-20 00:09:59 字数 1637 浏览 5 评论 0原文

我有一个用 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

開玄 2024-09-27 00:09:59

尝试在“异常”对话框中启用“抛出异常时中断”运行(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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文