when i save a file, if the parent directory doesn't exist i want my save to crete what's missing in my path

what to add, knowing that the todayDate folder doesnt existe.
i need to save the file in a brand new folder which name is for instance 2007_11_14.


string targetFile = PathToExcelRepository + VarSession.Community + "/containers/"  +todayDate+ "/" + FileNameToUpload;

      if(!File.Exists(targetFile))
      {
            fileUpload.PostedFile.SaveAs(targetFile);


received error:
Could not find a part of the path "C:\Documents and Settings\Erwin\My Documents\Visual Studio Projects\e-plan\E-pla.....
toshi_Asked:
Who is Participating?

Improve company productivity with a Business Account.Sign Up

x
 
doraiswamyConnect With a Mentor Commented:
 String DirName = Path.GetDirectory(targetfile);
  if(!Directory.Exists (DirName)
    {
    Directory.Create(DirName);
    }
  if(!File.Exists(targetfile))
   {
  //  ....
   }
0
 
cmhuntyCommented:
You'll have to get the full path to the file and then test the path, so

Dim strPathToFile As String = PathToExcelRepository + VarSession.Community + "/containers/"  +todayDate+ "/"
Dim di As New DirectoryInfo(strPathToFile)
If di.Exists = False
   di.Create()
End If

Then you can continue with your code
0
 
doraiswamyCommented:
cmhunty, this is the C# topic area.
0
 
bungHocConnect With a Mentor Commented:
System.IO.Directory.CreateDirectory(@"C:\Something\SomethingElse\AnotherThing");

This creates all sub directories too (if needed)
0
 
toshi_Author Commented:
you're all kings guys !!!!!
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.