-1

我有这个功能:

private void button6_Click(object sender, EventArgs e)
{
    crawlLocaly1 = new CrawlLocaly();
    crawlLocaly1.StartPosition = FormStartPosition.CenterParent;
    DialogResult dr = crawlLocaly1.ShowDialog(this);
    if (dr == DialogResult.Cancel)
    {
        crawlLocaly1.Close();
    }
    else if (dr == DialogResult.OK)
    {
        //LocalyKeyWords.Add(crawlLocaly1.getText() + "," + mainUrl);
        if(LocalyKeyWords.ContainsKey(mainUrl)) 
        {
            LocalyKeyWords[mainUrl].Add(crawlLocaly1.getText());


        }
        else
        {
            LocalyKeyWords.Add(mainUrl, new List<string>(new string[]
            {
                crawlLocaly1.getText()
            }
            ));
        } 
        crawlLocaly1.Close();
    }
}

当用户在 textBox 中键入内容并单击“确定”时,LocalKeyWords 中的结果即 Dictionary> 如下所示:

在索引 0 中会有例如:http://www.google.com ","google 其中 google 是属于http://www.google.com的键

现在,如果用户在 url 中输入一次关键字,它将转到第二部分,如果用户更改相同 url 的密钥,它将转到第一部分,否则如果用户更改 url,它将转到第二部分部分。

第一次为 url 放置密钥,第二次为相同的 url 更新密钥或为新 url 放置新密钥。

所以列表应该是这样的:

index 0 http://www.google.com,google
index 1 http://www.microsoft.com,com

等等......这个功能正常工作我只需要将列表添加到硬盘上的文本文件中。所以在 Form1 级别我有 streamwriter w = new streamwriter(@"d:\test.txt");

事实上,当我在 List 上使用断点时,我在左侧看到了 url then ,然后我看到了一些不是关键字的东西:

 [0] = {[http://www.google.co.il, System.Collections.Generic.List`1[System.String]]}

它不是错误或其他东西,它只是在断点上,这就是我的看法。相反 System.Collections.Generic.List`1[System.String] 我想看到关键字,例如:google

无论如何,我如何将列表值写入文本文件,以便文本文件格式如下所示:

0 http://www.google.com,google
1 http://www.microsoft.com,hello
2 http://www.cnet.com,Daniel

0 1 2 表示索引,url 表示 mainUrl,右 sie 表示 Keys(用户输入的关键字)。

然后如何将文本文件中的值读回 lixtBox ?因此,当我在构造函数中运行程序时,它会读取文本文件并在 listBox 中显示这种格式:

url: http://www.google.com --- keword: google
url: http://www.microsoft.com --- keword: hello
url: http://www.cnet.com --- keword: Daniel

因此,在 listBox 中,用户会比在文本文件中看到更好。

4

2 回答 2

0

您可以尝试使用此简单代码将您的值写入文本文件:

        int count = 0;
        foreach(KeyValuePair<string, string> kvp in LocalyKeyWords)
        {
            w.WriteLine(count.ToString() + " " + kvp.Key + " " + kvp.Value);
            count++;
        }
        w.Close();

您也可以使用以下代码读取该文本文件,我必须警告您,它不会检查您是否在文本文件中使用空白字符分隔所有三个字符串。此外,如果您存储的文本包含空白字符,您应该使用一些特殊字符分隔文件中的字符串。

        TextReader r = new StreamReader(@"d:\test.txt");
        string line = string.Empty;
        while ((line = r.ReadLine()) != null)
        {
            string[] data = line.Split(new char[] { ' ' },  StringSplitOptions.RemoveEmptyEntries);
            ListViewItem lvi = new ListViewItem("url: " + data[1] + " " + data[2]);
            listView1.Items.Add(lvi);
        }
于 2012-10-09T19:01:29.277 回答
0

What the debugger is telling you is accurate. You're adding a List of String to your collection and not a string, that's why the debugger shows you a list instead of a string. It's a newbie mistake to think the tools are wrong.

LocalyKeyWords.Add(mainUrl, new List<string>(new string[] 

By the way, that statement isn't even syntactically correct - your closing bracket is missing.

于 2012-10-09T19:29:23.753 回答