C# 如何获取哈希表键/值的内容并将其写入文本文件,然后读回并更新哈希表变量?

C# 如何获取哈希表键/值的内容并将其写入文本文件,然后读回并更新哈希表变量?,c#,winforms,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

这就是我现在所做的:

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();
        }
然后我想从文本文件中读回每个键和值,并将它们放回
alreadyPost
变量中

我每隔10秒用定时器调用
PostMessage
。 所以我想在每次运行程序时检查已经发布的内容

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

  • 在方法
    PostMessage
    中执行相同的操作

  • 好的,开始工作了:

    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);
                    }
                }
    
    foreach(alreadyPost中的DictionaryEntry条目)
    {
    WritePostalReady.WriteLine(entry.Key+“=”+entry.Value);
    }
    writePostalReady.Close();
    alreadyPost=新哈希表();
    List readLines=File.ReadAllLines(@“c:\Temp\WritePostedAlready.txt”).ToList();
    foreach(读取行中的字符串行)
    {
    string key=line.Split('=')[0];
    字符串val=line.Split('=')[1];
    如果(!alreadyPost.ContainsKey(键))
    {
    添加(键,val);
    }
    }
    

    谢谢。

    那么您对哪一块有问题?
    Key = Value
    Key = Value
    .
    .
    .
    
    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);
                    }
                }