tags:

views:

147

answers:

3

I just can't get around this. I am able to create a file with File.Create ... File..Text and so on, only if the path exists. If it does not the file will not we write and return an error. How do I create a path?

+6  A: 

Try

Directory.CreateDirectory("C:\MyApp\MySubDir\Data")

http://www.devx.com/vb2themax/Tip/18678

Sklivvz
As this post mostlikely solves your problem here the additional MSDN information: http://msdn.microsoft.com/en-us/library/as2f1fez.aspx
Bdiem
And how do we check if the path exists? or we just call this function.
Jonathan Shepherd
READ! MSDN Says: If the folder already exists, CreateDirectory does nothing.
Bdiem
+3  A: 

You will need to create the Directory first. It will create all of the subdirectories that don't exist within the path you send it. It's quite a powerful piece of functionality.

Directory.CreateDirectory(filePath);

If you don't know whether the directory exists or not you can use Directory.Exists. But not for this case as it would be pointless. MSDN states that CreateDirectory does nothing if the directory currently exists. But if you wanted to check existance of the directory for another reason you can use:

  if(Directory.Exists(folder) == false)
    {
    //do stuff  
    }
ThePower
+1  A: 

Directory.CreateDirectory("path");

x2