File.Create Method (String)


The .NET API Reference documentation has a new home. Visit the .NET API Browser on to see the new experience.

Creates or overwrites a file in the specified path.

Namespace:   System.IO
Assembly:  mscorlib (in mscorlib.dll)

public static FileStream Create(
	string path


Type: System.String

The path and name of the file to create.

Return Value

Type: System.IO.FileStream

A FileStream that provides read/write access to the file specified in path.

Exception Condition

The caller does not have the required permission.


path specified a file that is read-only.


path is a zero-length string, contains only white space, or contains one or more invalid characters as defined by InvalidPathChars.


path is null.


The specified path, file name, or both exceed the system-defined maximum length. For example, on Windows-based platforms, paths must be less than 248 characters, and file names must be less than 260 characters.


The specified path is invalid (for example, it is on an unmapped drive).


An I/O error occurred while creating the file.


path is in an invalid format.

The FileStream object created by this method has a default FileShare value of None; no other process or code can access the created file until the original file handle is closed.

This method is equivalent to the Create(String, Int32) method overload using the default buffer size.

The path parameter is permitted to specify relative or absolute path information. Relative path information is interpreted as relative to the current working directory. To obtain the current working directory, see GetCurrentDirectory.

If the specified file does not exist, it is created; if it does exist and it is not read-only, the contents are overwritten.

By default, full read/write access to new files is granted to all users. The file is opened with read/write access and must be closed before it can be opened by another application.

For a list of common I/O tasks, see Common I-O Tasks.

The following example creates a file in the specified path, writes some information to the file, and reads from the file.

using System;
using System.IO;
using System.Text;

class Test
    public static void Main()
        string path = @"c:\temp\MyTest.txt";


            // Delete the file if it exists.
            if (File.Exists(path))
                // Note that no lock is put on the
                // file and the possibility exists
                // that another process could do
                // something with it between
                // the calls to Exists and Delete.

            // Create the file.
            using (FileStream fs = File.Create(path))
                Byte[] info = new UTF8Encoding(true).GetBytes("This is some text in the file.");
                // Add some information to the file.
                fs.Write(info, 0, info.Length);

            // Open the stream and read it back.
            using (StreamReader sr = File.OpenText(path))
                string s = "";
                while ((s = sr.ReadLine()) != null)

        catch (Exception ex)


for permission to read and write to the file described by the path parameter. Security action: Demand . Associated enumerations: FileIOPermissionAccess.Read, FileIOPermissionAccess.Write

Universal Windows Platform
Available since 10
.NET Framework
Available since 1.1
Available since 2.0
Windows Phone Silverlight
Available since 7.0
Return to top