Export (0) Print
Expand All

How to: Create a File or Folder (C# Programming Guide)

This example creates a folder and a subfolder on a computer, and then creates a new file in the subfolder and writes some data to the file.


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();
    }
}


If the folder already exists, CreateDirectory does nothing, and no exception is thrown. However, File.Create does overwrite any existing file. To avoid overwriting an existing file, you can use the OpenWrite method and specify the FileMode.OpenOrCreate option that will cause the file to be appended to rather than overwritten.

The following conditions may cause an exception:

An instance of SecurityException class may be thrown in partial-trust situations.

If the user does not have permission to create the folder, the example throws an instance of the UnauthorizedAccessException class.

Community Additions

ADD
Show:
© 2014 Microsoft