File.Create
Method
Definition
Overloads
| Create(String) |
Creates or overwrites a file in the specified path. |
| Create(String, Int32) |
Creates or overwrites the specified file. |
| Create(String, Int32, FileOptions) |
Creates or overwrites the specified file, specifying a buffer size and a FileOptions value that describes how to create or overwrite the file. |
| Create(String, Int32, FileOptions, FileSecurity) |
Creates or overwrites the specified file with the specified buffer size, file options, and file security. |
Create(String)
Creates or overwrites a file in the specified path.
public static System.IO.FileStream Create (string path);
- path
- String
The path and name of the file to create.
A FileStream that provides read/write access to the file specified in path.
The caller does not have the required permission.
-or-
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.
Examples
The following example creates a file in the specified path, writes some information to the file, and reads from the file.
using namespace System;
using namespace System::IO;
using namespace System::Text;
int main()
{
String^ path = "c:\\temp\\MyTest.txt";
// Delete the file if it exists.
if ( File::Exists( path ) )
{
File::Delete( path );
}
// Create the file.
FileStream^ fs = File::Create( path );
try
{
array<Byte>^info = (gcnew UTF8Encoding( true ))->GetBytes( "This is some text in the file." );
// Add some information to the file.
fs->Write( info, 0, info->Length );
}
finally
{
if ( fs )
delete (IDisposable^)fs;
}
// Open the stream and read it back.
StreamReader^ sr = File::OpenText( path );
try
{
String^ s = "";
while ( s = sr->ReadLine() )
{
Console::WriteLine( s );
}
}
finally
{
if ( sr )
delete (IDisposable^)sr;
}
}
using System;
using System.IO;
using System.Text;
class Test
{
public static void Main()
{
string path = @"c:\temp\MyTest.txt";
try
{
// 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.
File.Delete(path);
}
// 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)
{
Console.WriteLine(s);
}
}
}
catch (Exception ex)
{
Console.WriteLine(ex.ToString());
}
}
}
Imports System
Imports System.IO
Imports System.Text
Public Class Test
Public Shared Sub Main()
Dim path As String = "c:\temp\MyTest.txt"
Try
' Delete the file if it exists.
If File.Exists(path) Then
' 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.
File.Delete(path)
End If
' Create the file.
Using fs As FileStream = File.Create(path)
Dim info As Byte() = New UTF8Encoding(True).GetBytes("This is some text in the file.")
' Add some information to the file.
fs.Write(info, 0, info.Length)
End Using
' Open the stream and read it back.
Using sr As StreamReader = File.OpenText(path)
Do While sr.Peek() >= 0
Console.WriteLine(sr.ReadLine())
Loop
End Using
Catch ex As Exception
Console.WriteLine(ex.ToString())
End Try
End Sub
End Class
Remarks
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.
Create(String, Int32)
Creates or overwrites the specified file.
public static System.IO.FileStream Create (string path, int bufferSize);
- path
- String
The name of the file.
- bufferSize
- Int32
The number of bytes buffered for reads and writes to the file.
A FileStream with the specified buffer size that provides read/write access to the file specified in path.
The caller does not have the required permission.
-or-
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.
Examples
The following example creates a file with the specified buffer size.
using namespace System;
using namespace System::IO;
using namespace System::Text;
int main()
{
String^ path = "c:\\temp\\MyTest.txt";
// Delete the file if it exists.
if ( File::Exists( path ) )
{
File::Delete( path );
}
// Create the file.
FileStream^ fs = File::Create( path, 1024 );
try
{
array<Byte>^info = (gcnew UTF8Encoding( true ))->GetBytes( "This is some text in the file." );
// Add some information to the file.
fs->Write( info, 0, info->Length );
}
finally
{
if ( fs )
delete (IDisposable^)fs;
}
// Open the stream and read it back.
StreamReader^ sr = File::OpenText( path );
try
{
String^ s = "";
while ( s = sr->ReadLine() )
{
Console::WriteLine( s );
}
}
finally
{
if ( sr )
delete (IDisposable^)sr;
}
}
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))
{
File.Delete(path);
}
// Create the file.
using (FileStream fs = File.Create(path, 1024))
{
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)
{
Console.WriteLine(s);
}
}
}
}
Imports System
Imports System.IO
Imports System.Text
Public Class Test
Public Shared Sub Main()
Dim path As String = "c:\temp\MyTest.txt"
Try
' Delete the file if it exists.
If File.Exists(path) Then
' 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.
File.Delete(path)
End If
' Create the file.
Using fs As FileStream = File.Create(path, 1024)
Dim info As Byte() = New UTF8Encoding(True).GetBytes("This is some text in the file.")
' Add some information to the file.
fs.Write(info, 0, info.Length)
End Using
' Open the stream and read it back.
Using sr As StreamReader = File.OpenText(path)
Do While sr.Peek() >= 0
Console.WriteLine(sr.ReadLine())
Loop
End Using
Catch ex As Exception
Console.WriteLine(ex.ToString())
End Try
End Sub
End Class
Remarks
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.
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.
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.
Create(String, Int32, FileOptions)
Creates or overwrites the specified file, specifying a buffer size and a FileOptions value that describes how to create or overwrite the file.
public static System.IO.FileStream Create (string path, int bufferSize, System.IO.FileOptions options);
- path
- String
The name of the file.
- bufferSize
- Int32
The number of bytes buffered for reads and writes to the file.
- options
- FileOptions
One of the FileOptions values that describes how to create or overwrite the file.
A new file with the specified buffer size.
The caller does not have the required permission.
-or-
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.
Remarks
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.
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.
Create(String, Int32, FileOptions, FileSecurity)
Creates or overwrites the specified file with the specified buffer size, file options, and file security.
public static System.IO.FileStream Create (string path, int bufferSize, System.IO.FileOptions options, System.Security.AccessControl.FileSecurity fileSecurity);
- path
- String
The name of the file.
- bufferSize
- Int32
The number of bytes buffered for reads and writes to the file.
- options
- FileOptions
One of the FileOptions values that describes how to create or overwrite the file.
- fileSecurity
- FileSecurity
One of the FileSecurity values that determines the access control and audit security for the file.
A new file with the specified buffer size, file options, and file security.
The caller does not have the required permission.
-or-
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.
Remarks
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.
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.