Click here to Skip to main content
15,891,951 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
I get an argument exception(error image) when I run this piece of code
C#
DirectoryInfo directoryInfo = new DirectoryInfo(path);

This is the code that gets path
C#
private void openFolderToolStripButton_Click(object sender, EventArgs e)
{
    FolderBrowserDialog obd = new FolderBrowserDialog();
    if (obd.ShowDialog() == DialogResult.OK)
    {
        path = obd.SelectedPath;
        Console.WriteLine("path is found");
        Console.WriteLine(path.ToString());

    }
}


What I have tried:

It works fine with a predefined path e.g.
C#
DirectoryInfo directoryInfo = new DirectoryInfo(@"C:\Users\rohit\Desktop\all\DS4Windows");

What should I do, I am a bit confused why this is a problem
Posted
Updated 21-Feb-17 1:12am
v2
Comments
Karthik_Mahalingam 21-Feb-17 7:06am    
is the path null/empty on load?
Ehsan Sajjad 21-Feb-17 7:48am    
what is value in the path variable at that time when exception is coming ?

Since the path is empty/null on page load, it will result in argument exception, ( based on info from screenshot)

FolderBrowserDialog obd = new FolderBrowserDialog();
            if (obd.ShowDialog() == DialogResult.OK)
            {
               string path = obd.SelectedPath; 
                DirectoryInfo directoryInfo = new DirectoryInfo(path);
                // cut the code from page load and paste it over here.

            }
 
Share this answer
 
v2
Comments
Rohit Pai (rodude123) 21-Feb-17 8:33am    
This works thanks
Karthik_Mahalingam 21-Feb-17 8:35am    
Welcome RP
Rohit Pai (rodude123) 21-Feb-17 8:37am    
I didn't think of that. Is there a way to make a tree view have buttons, I was wondering because I am making a file browser for code editor
Karthik_Mahalingam 21-Feb-17 8:42am    
Hmm sometimes happens
Rohit Pai (rodude123) 21-Feb-17 8:43am    
yea
Hi,

from the provided screenshot it seems you're trying to do this in Form1_Load. Are you sure you have a valid path at this point? If it's your only form and you didn't send the path from some other form, this will probably be the issue. Debug your code to see what really is in the path variable in the moment you creating DirectoryInfo.

Hope this helps.
 
Share this answer
 
Comments
Rohit Pai (rodude123) 21-Feb-17 8:30am    
The path at the beginning is always null because the path is not chosen yet from the folder browser dialog, I am trying to create a folder browser for my code editor

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900