C# .NET - how to create folder automatally - Asked By Bhanuprakash T.R on 29-Jul-11 06:54 AM

HelloSir,
     i try this code
using ADOX;
  ADOX.CatalogClass cat = new ADOX.CatalogClass();
        cat.Create("Provider=Microsoft.Jet.OLEDB.4.0;" +
        "Data Source=C:/writemdb27jul/StockManagement.mdb;" +
        "Jet OLEDB:Engine Type=5");
        MessageBox.Show("database created successfully");
        cat = null;
the above code StockManagement.mdb is created automatically ... i want folder also created automatically.... while compaile writemdb27jul is automatically created and inside that StockManagement.mdb also created... please send the coding for this
Ravi S replied to Bhanuprakash T.R on 29-Jul-11 06:57 AM
HI

try this

public class CreateFileOrFolder
{
    static void Main()
    {
        // Specify a "currently active folder"
        string activeDir = @"c:\testdir2";

        //Create a new subfolder under the current active folder
        string newPath = System.IO.Path.Combine(activeDir, "mySubDir");

        // Create the subfolder
        System.IO.Directory.CreateDirectory(newPath);

        // Create a new file name. This example generates
        // a random string.
        string newFileName = System.IO.Path.GetRandomFileName();

        // Combine the new file name with the path
        newPath = System.IO.Path.Combine(newPath, newFileName);

        // Create the file and write to it.
        // DANGER: System.IO.File.Create will overwrite the file
        // if it already exists. This can occur even with
        // random file names.
        if (!System.IO.File.Exists(newPath))
        {
            using (System.IO.FileStream fs = System.IO.File.Create(newPath))
            {
                for (byte i = 0; i < 100; i++)
                {
                    fs.WriteByte(i);
                }
            }
        }

        // Read data back from the file to prove
        // that the previous code worked.
        try
        {
            byte[] readBuffer = System.IO.File.ReadAllBytes(newPath);
            foreach (byte b in readBuffer)
            {
                Console.WriteLine(b);
            }
        }
        catch (System.IO.IOException e)
        {
            Console.WriteLine(e.Message);
        }

        // Keep the console window open in debug mode.
        System.Console.WriteLine("Press any key to exit.");
        System.Console.ReadKey();
    }
}


Sreekumar P replied to Bhanuprakash T.R on 29-Jul-11 07:25 AM
Hi,

Here u just have to check that folder exists -- if not exits create one there...


         
      if (!System.IO.Directory.Exists("C:/writemdb27jul"))
      {
        System.IO.Directory.CreateDirectory("C:/writemdb27jul");
      }
      ADOX.CatalogClass cat = new ADOX.CatalogClass();
      cat.Create("Provider=Microsoft.Jet.OLEDB.4.0;" +
      "Data Source=C:/writemdb27jul/StockManagement.mdb;" +
      "Jet OLEDB:Engine Type=5");
      MessageBox.Show("database created successfully");
      cat = null;
Reena Jain replied to Bhanuprakash T.R on 29-Jul-11 08:46 AM
hi,

try this

public static void CreateDirectory(DirectoryInfo directory)
{
  if (!directory.Parent.Exists)
  CreateDirectory(directory.Parent);
  directory.Create();
}
 
//Use it in this way:
 
// path is your file path
string directory = Path.GetDirectoryName(path);
CreateDirectory(new DirectoryInfo(directory));

or like this
using System.IO;
  
class Program
{
  static void Main()
  {
  //
  // Create new folder in C:\ volume.
  Directory.CreateDirectory("C:\\newfolder");
  //
  // Create another directory with different string literal syntax.
  Directory.CreateDirectory(@"C:\newfolder2");
  //
  // Create an already-existing directory (does nothing).
   Directory.CreateDirectory(@"C:\newfolder2");
  }
}

Result of program
    There are 2 folders on your C:\ drive:
    1. newfolder
    2. newfolder2

Directory.CreateDirectory can be used to create the final directory and will automatically create all folders in the path if they don't exist.

Hope this will help you