Skip to main content This browser is no longer supported. Show
Upgrade to Microsoft Edge to take advantage of the latest features, security updates, and technical support. File.Create Method
DefinitionCreates or overwrites a file in the specified path. In this articleOverloads
Create(String, Int32, FileOptions, FileSecurity)Creates or overwrites a file in the specified path, specifying a buffer size, options that describe how to create or overwrite the file, and a value that determines the access control and audit security for the file.
Parameterspath String The path and name of the file to create. bufferSize Int32 The number of bytes buffered for reads and writes to the file. ReturnsFileStreamA new file with the specified buffer size, file options, and file security. ExceptionsThe caller does not have the required permission. -or-
-or-
.NET Framework and .NET Core versions older than 2.1: The specified path, file name, or both exceed the system-defined maximum length. The specified path is invalid (for example, it is on an unmapped drive). An I/O error occurred while creating the file.
RemarksThe This method is equivalent to the FileStream(String, FileMode, FileAccess, FileShare, Int32) constructor overload. 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. Applies toCreate(String, Int32, FileOptions)Creates or overwrites a file in the specified path, specifying a buffer size and options that describe how to create or overwrite the file.
Parameterspath String The path and name of the file to create. bufferSize Int32 The number of bytes buffered for reads and writes to the file. ReturnsFileStreamA new file with the specified buffer size. ExceptionsThe caller does not have the required permission. -or-
-or-
.NET Framework and .NET Core versions older than 2.1: The specified path, file name, or both exceed the system-defined maximum length. The specified path is invalid (for example, it is on an unmapped drive. An I/O error occurred while creating the file.
RemarksThe This method is equivalent to the FileStream(String, FileMode, FileAccess, FileShare, Int32) constructor overload. 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. Applies toCreate(String)Creates or overwrites a file in the specified path.
Parameterspath String The path and name of the file to create. ReturnsFileStreamA
FileStream that provides read/write access to the file specified in ExceptionsThe caller does not have the required permission. -or-
-or-
.NET Framework and .NET Core versions older than 2.1: The specified path, file name, or both exceed the system-defined maximum length. The specified path is invalid (for example, it is on an unmapped drive). An I/O error occurred while creating the file.
ExamplesThe following example creates a file in the specified path, writes some information to the file, and reads from the file.
RemarksThe 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 of 4,096 bytes. The 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. See also
Applies toCreate(String, Int32)Creates or overwrites a file in the specified path, specifying a buffer size.
Parameterspath String The path and name of the file to create. bufferSize Int32 The number of bytes buffered for reads and writes to the file. ReturnsFileStreamA
FileStream with the specified buffer size that provides read/write access to the file specified in ExceptionsThe caller does not have the required permission. -or-
-or-
.NET Framework and .NET Core
versions older than 2.1: The specified path, file name, or both exceed the system-defined maximum length. The specified path is invalid (for example, it is on an unmapped drive). An I/O error occurred while creating the file.
ExamplesThe following example creates a file with the specified buffer size.
RemarksThe 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. The This method is equivalent to the FileStream(String, FileMode, FileAccess, FileShare, Int32) constructor overload. 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. See also
Applies toWhen a file that already exists is opened in append mode the file's existing contents are not erased?If an existing file is opened in append mode, what happens to the file's existing contents? It will not be erased and new data will be written at the end of the file's current contents.
When you open a file that file already exists on the disk using the W mode?Terms in this set (9) When working with a sequential access file, you can jump directly to any piece of data in the file without reading the data that comes before it. When you open a file that file already exists on the disk using the "w" mode, the contents of the existing file will be erased.
What happens if you try to open a file for input read mode but that file does not exist?When you open a file for reading, if the file does not exist, the program will open an empty file.
What will happen when a program opens a file in write mode if the file doesn't exist?If you open a file for reading and the file doesn't exist, then an exception is thrown. If you open a file for writing and the file doesn't exist, then the file is created with 0 length.
|