maliks
maliks

Reputation: 1112

How to check existence of directory using path C#

Using

Directory _dir = Directory.CreateDirectory(path);

So how to store the object of directory in a variable so then I have to check whether the directory at specified path exists or not as?

if(!_dir.exists())
{
    _dir.CreateDirectory(path);
}

Is it allowed in C#?

Upvotes: 0

Views: 141

Answers (2)

VIJAY LAKHANI
VIJAY LAKHANI

Reputation: 54

Try This...

        string path=@"C:\Users\v\Desktop\DESKTOP";
        if(!Directory.Exists(path))
        {
            Directory.CreateDirectory(path);
        }

Upvotes: 2

mohsen
mohsen

Reputation: 1806

DirectoryInfo dir = Directory.CreateDirectory(path);
if(!dir.Exists)
{
    dir.CreateDirectory(path);
}

Or

DirectoryInfo dir = Directory.CreateDirectory(path);
if(!Directory.Exists(path))
{
    dir.CreateDirectory(path);
}

Upvotes: 0

Related Questions