Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8654793
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 12, 20262026-06-12T14:52:23+00:00 2026-06-12T14:52:23+00:00

private void button6_Click(object sender, EventArgs e) { string[] entries = File.ReadAllLines(@D:\Keywords.txt); foreach (string entry

  • 0
private void button6_Click(object sender, EventArgs e)
        {
          string[] entries = File.ReadAllLines(@"D:\Keywords.txt");
            foreach (string entry in entries)
            {
                string[] values = entry.Split(',');
                LocalyKeyWords[values[0]].Clear();
                for (int i = 1; i < values.Length; i++)
                    LocalyKeyWords[values[0]].Add(values[i]);
            }
            using (var w = new StreamWriter(@"D:\Keywords.txt"))
            {
                crawlLocaly1 = new CrawlLocaly();
                crawlLocaly1.StartPosition = FormStartPosition.CenterParent;
                DialogResult dr = crawlLocaly1.ShowDialog(this);
                if (dr == DialogResult.OK)
                {
                    if (LocalyKeyWords.ContainsKey(mainUrl))
                    {
                        LocalyKeyWords[mainUrl].Clear();
                        //probably you could skip this part and create new List everytime
                        LocalyKeyWords[mainUrl].Add(crawlLocaly1.getText());
                    }
                    else
                    {
                        LocalyKeyWords[mainUrl] = new List<string>();
                        LocalyKeyWords[mainUrl].Add(crawlLocaly1.getText());
                    }
                    foreach (KeyValuePair<string, List<string>> kvp in LocalyKeyWords)
                    {
                        w.WriteLine(kvp.Key + "," + string.Join(",", kvp.Value));
                    }
                }
            } 
        }

The error is on the line:

LocalyKeyWords[values[0]].Clear();

The error is: KeyNotFoundException: The given key was not present in the dictionary

LocalyKeyWords is a List:

Dictionary<string, List<string>> LocalyKeyWords = new Dictionary<string, List<string>>();

And the file Keywords.txt is not empty now :

http://www.cnet.com,changing for test
http://www.wlala.co.co,adding

And yet when reading the file I’m getting the exception.
I see that on the error line that LocalyKeyWords is empty 0 but values contain two indexes :

[0] http://www.cnet.com and [1] changing for test

Index 0 is the url and index 1 is the url key!

I tried to add before this line and change the code there a bit to:

if (LocalyKeyWords.Count == 0)
   {                         
    LocalyKeyWords[values[0]] = new List<string>();
   }
   else
   {
    LocalyKeyWords[values[0]].Clear();
   } 

But it didn’t work and it didn’t solve the problem. I’m still getting the error on the line.
How can I fix it?

The List is built this way: url,key for exmaple: http://www.google.com“,”google
also the text file format is like that:

http://www.cnet.com,changing for test
http://www.wlala.co.co,adding

changing for test is the key of http://www.cnet.com
and addin is the key for http://www.wlala.co.co

So I want to do two things when reading back the url,key from the text file:

  1. To add the url and its belonging key to a ListBox so I did it in the constructor. I want to do it in the button click event each time the user change a key for a url or add a new url and its key so the user will see in real time the changes in ListBox. In the constructor I’m adding the url and keys and show it in the ListBox like this:

    sr = new StreamReader(@”d:\Keywords.txt”);
    while (null != (line = sr.ReadLine()))
    {

                    index = line.IndexOf(",");
                    key_of_each_line = line.Substring(0, index);
                    value_of_each_key = line.Substring(index + 1);
                    listBox1.Items.Add("Url: " + key_of_each_line + " --- " + "Key: " + value_of_each_key );
                }
                sr.Close();
    

sr is a StreamReader.
And it’s working good in the constructor. The same format in the constructor. I want to show it in real time in the button click event when the user add new/change a key/s or url/s

  1. To fix the reading part as it is now in the button click event with exception error.

The new reading part code:

if (File.Exists(@"d:\Keywords.txt"))
            {
                string[] entries = File.ReadAllLines(@"D:\Keywords.txt");
                foreach (string entry in entries)
                {
                    string[] values = entry.Split(',');
                    if (LocalyKeyWords.ContainsKey(values[0]))
                        LocalyKeyWords[values[0]].Clear();
                    else
                        LocalyKeyWords[values[0]] = new List<string>();
                    for (int i = 1; i < values.Length; i++)
                        LocalyKeyWords[values[0]].Add(values[i]);
                }
            }

Added also a check if the file exist in case the user deleted it. But if the file exist I don’t want that each time the user click the button it will create a new empty text file.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-12T14:52:25+00:00Added an answer on June 12, 2026 at 2:52 pm

    Does doing this sort out the issue:

    foreach (string entry in entries)
    {
        string[] values = entry.Split(',');
        LocalyKeyWords[values.First()] = values.Skip(1).ToList();
    }
    

    Alternatively, if you can reassign LocalyKeyWords you could do this:

    var LocalyKeyWords =
        File
            .ReadAllLines(@"D:\Keywords.txt")
            .Select(entry =>
            {
                var values = entry.Split(',');
                return new
                {
                    key = values.First(),
                    values = values.Skip(1).ToList(),
                };
            })
            .ToDictionary(kv => kv.key, kv => kv.values);
    

    The latter method is preferable as it is fully rebuilding the dictionary and therefore there is no way that there could be existing keys.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

private void button6_Click(object sender, EventArgs e) { string[] filePaths = Directory.GetFiles(@c:\MyDir\); } And I
I have this function: private void button6_Click(object sender, EventArgs e) { crawlLocaly1 = new
private void button1_Click(object sender, EventArgs e) { string name; name = textBox5.Text; SqlConnection con10
private void button1_Click(object sender, EventArgs e) { string costring = connection(); string MyQuery =
private void showCategory_Load(object sender, EventArgs e) { string categoryId = 100 } private void
I have this button click event: private void button6_Click(object sender, EventArgs e) { if
private void button1_Click(object sender, EventArgs e) { for (int i = 0; i <
When I run the following code: private void button1_Click(object sender, EventArgs e) { Bitmap
I tried to login to gmail with the code: private void button1_Click(object sender, EventArgs
I have this button click code wich is working now: private void button6_Click(object sender,

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.