Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
150 views
in Technique[技术] by (71.8m points)

c# - How can I write the values in a List to a text file?

I have this function:

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();
    }
}

When the user type something to textBox and click ok so the result in the LocalKeyWords which is Dictionary> is like:

In index 0 there will be for example: http://www.google.com","google Where google is the key that belong to the http://www.google.com

Now if the user enter a keyword once to the url it will go to the second part and if the user change the key for the same url it will go to the first part else if the user changed the url so it will go to the second part.

First time it's putting a key for the url second its updating the key for the same url or put a new key for a new url.

So the List should be like this:

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

And so on... And this function is working correctly I just need to add the List to a text file on the hard disk. So in the Form1 level I have streamwriter w = new streamwriter(@"d: est.txt");

In fact when I'm using a breakpoint on the List I see on the left the url then , and then I see something else not the keyword:

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

Its not a bug or something it's just on a breakpoint that's how I see it. Instead System.Collections.Generic.List`1[System.String] I was suppose to see the keyword for example: google

Any way how do I write the List values to the text file so the text file format will be like this:

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

0 1 2 present the indexs and url's present the mainUrl and the right sie present the Keys(keywords the user entered).

And then how do I read back the values from the text file to the lixtBox ? So when I'm running the program in the constructor it will read the text file and show me in a listBox this format:

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

So in the listBox the user will see it nicer then in the text file.

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

You can try this simple code to write to textfile your values:

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

Also you can read that textfile using the following code, I must warn you that it doesn't check if you have all three strings separated with blank character in textfile. Also, if your text that you are storing has blank characters, you should separate strings in file with some special character.

        TextReader r = new StreamReader(@"d:	est.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);
        }

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...