Joe Pearson
Joe Pearson

Reputation: 149

An unhandled exception of type 'System.IO.DirectoryNotFoundException' occurred in mscorlib.dll

I get this error when I run my code. Essentially it should be moving data from whichever string matches mapping to the string in textbox1.

The entire error is as follows:

An unhandled exception of type 'System.IO.DirectoryNotFoundException' occurred in mscorlib.dll

Additional information: Could not find a part of the path 'C:\Users\jpearson\Documents\Visual Studio 2013\Projects\WindowsFormsApplication2\WindowsFormsApplication2\bin\Debug\0110'.

I have validated that all of the file paths in the code are valid, so I am not entirely sure what I am missing.

  private void button1_Click(object sender, EventArgs e)
        {
            folderBrowserDialog1.ShowDialog();
            textBox1.Text = folderBrowserDialog1.SelectedPath;
        }


        private Dictionary<string, string> mapping = new Dictionary<string, string>
         {
             { "0110", @"C:/Example" },
             { "1000", @"C:/Example2" },
             { "1100", @"C:/Example3" },
         };

private void button2_Click(object sender, EventArgs e)
{

    string destination = textBox1.Text;
    string source = textBox2.Text;

    if (mapping.ContainsKey(source))
    {
        foreach (var f in Directory.GetFiles(source))
        {
            File.Copy(f, Path.Combine(destination, Path.GetFileName(f)));
            GC.Collect();
            GC.WaitForPendingFinalizers();
        }
    }
    else
    {
        MessageBox.Show ("Oh No! Something went wrong, try again!");
    }
}

Upvotes: 1

Views: 18806

Answers (1)

Ivan Vasiljevic
Ivan Vasiljevic

Reputation: 5708

From code and example that you supplied I would say that key is value that you supplied through GUI and that key is mapped to folder that is value in dictionary. I would say that you forgot to get value for given key and that you are searching for folder with your key name '0110' for example. So I would suggest you to change code something like this:

private void button2_Click(object sender, EventArgs e)
  {
    string destination = textBox1.Text;
    string source = textBox2.Text;

    if (mapping.ContainsKey(source))
    {
        string directoryName = mapping[source];
        foreach (var f in Directory.GetFiles(directoryName))
        {
            File.Copy(f, Path.Combine(destination, Path.GetFileName(f)));
            GC.Collect();
            GC.WaitForPendingFinalizers();
        }
    }
    else
    {
        MessageBox.Show ("Oh No! Something went wrong, try again!");
    }
}

This is my assumption.

Hope that I helped you :)

Upvotes: 2

Related Questions