如何获取Hashtable键/值的内容将它们写入文本文件并将其读回并更新Hashtable变量?

时间:2014-02-18 20:20:29

标签: c# winforms

这就是我现在所做的:

private Hashtable alreadyPost = new Hashtable();
        private void PostMessage()
        {
            for (int i = 0; i < ScrollLabel._lines.Length; i++)
            {
                for (int x = 0; x < WordsList.words.Length; x++)
                {
                    if (ScrollLabel._lines[i].Contains(WordsList.words[x]) && !alreadyPost.ContainsKey(ScrollLabel._lines[i]))
                    {
                        lineToPost = ScrollLabel._lines[i];
                        string testline = lineToPost + Environment.NewLine + ScrollLabel._lines[i + 1];
                        //PostFacebookWall(AccessPageToken, testline + Environment.NewLine + Environment.NewLine + "נשלח באופן אוטומטי כניסיון דרך תוכנה");
                        alreadyPost.Add(lineToPost, true);
                        numberofposts += 1;
                        label7.Text = numberofposts.ToString();
                    }
                }
            }
            for (int i = 0; i < alreadyPost.Count; i++)
            {
                WritePostedAlready.WriteLine(alreadyPost[i]);
            }
            WritePostedAlready.Close();
        }

我现在补充道:

for (int i = 0; i < alreadyPost.Count; i++)
{
    WritePostedAlready.WriteLine(alreadyPost[i]);
}
WritePostedAlready.Close();

WritePostedAlreadyStreamWriter

我想要做的是从alreadyPost读取键和值,并将它们写入文本文件格式:

Key = Value
Key = Value
.
.
.

然后我想从文本文件中读取每个键和值,并将它们放回alreadyPost变量。

我每10秒钟在一个计时器中调用PostMessage。 所以我想检查每次重新运行程序时已发布的内容。

  1. 在运行程序时,在构造函数中读取一次键和值,如果存在任何键和值,则更新从文本文件中读取的alreadyPost变量并将其添加到{{1 }}

  2. 在方法alreadyPost中做同样的事情。

1 个答案:

答案 0 :(得分:0)

好的让它起作用:

foreach (DictionaryEntry entry in alreadyPost)
            {
                WritePostedAlready.WriteLine(entry.Key + " = " + entry.Value);
            }
            WritePostedAlready.Close();

            alreadyPost = new Hashtable();
            List<string> readLines = File.ReadAllLines(@"c:\Temp\WritePostedAlready.txt").ToList();
            foreach (string line in readLines)
            {
                string key = line.Split('=')[0];
                string val = line.Split('=')[1];
                if (!alreadyPost.ContainsKey(key))
                {
                    alreadyPost.Add(key, val);
                }
            }

感谢。