File.Create File.Create File.Create File.Create Method

Definition

Creates a file in the specified path.

Overloads

Create(String) Create(String) Create(String) Create(String)

Creates or overwrites a file in the specified path.

Create(String, Int32) Create(String, Int32) Create(String, Int32) Create(String, Int32)

Creates or overwrites the specified file.

Create(String, Int32, FileOptions) Create(String, Int32, FileOptions) Create(String, Int32, FileOptions) 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) Create(String, Int32, FileOptions, FileSecurity) Create(String, Int32, FileOptions, FileSecurity)

Creates or overwrites the specified file with the specified buffer size, file options, and file security.

Create(String) Create(String) Create(String) Create(String)

Creates or overwrites a file in the specified path.

public:
 static System::IO::FileStream ^ Create(System::String ^ path);
public static System.IO.FileStream Create (string path);
static member Create : string -> System.IO.FileStream
Public Shared Function Create (path As String) As FileStream

Parameters

path
String String String String

The path and name of the file to create.

Returns

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

Exceptions

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.

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.

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.

Security

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

See Also

Create(String, Int32) Create(String, Int32) Create(String, Int32) Create(String, Int32)

Creates or overwrites the specified file.

public:
 static System::IO::FileStream ^ Create(System::String ^ path, int bufferSize);
public static System.IO.FileStream Create (string path, int bufferSize);
static member Create : string * int -> System.IO.FileStream
Public Shared Function Create (path As String, bufferSize As Integer) As FileStream

Parameters

path
String String String String

The name of the file.

bufferSize
Int32 Int32 Int32 Int32

The number of bytes buffered for reads and writes to the file.

Returns

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

Exceptions

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.

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.

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.

Security

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

See Also

Create(String, Int32, FileOptions) Create(String, Int32, FileOptions) Create(String, Int32, FileOptions) 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(System::String ^ path, int bufferSize, System::IO::FileOptions options);
public static System.IO.FileStream Create (string path, int bufferSize, System.IO.FileOptions options);
static member Create : string * int * System.IO.FileOptions -> System.IO.FileStream
Public Shared Function Create (path As String, bufferSize As Integer, options As FileOptions) As FileStream

Parameters

path
String String String String

The name of the file.

bufferSize
Int32 Int32 Int32 Int32

The number of bytes buffered for reads and writes to the file.

options
FileOptions FileOptions FileOptions FileOptions

One of the FileOptions values that describes how to create or overwrite the file.

Returns

A new file with the specified buffer size.

Exceptions

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.

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.

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.

Security

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

Create(String, Int32, FileOptions, FileSecurity) Create(String, Int32, FileOptions, FileSecurity) 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(System::String ^ path, int bufferSize, System::IO::FileOptions options, System::Security::AccessControl::FileSecurity ^ fileSecurity);
public static System.IO.FileStream Create (string path, int bufferSize, System.IO.FileOptions options, System.Security.AccessControl.FileSecurity fileSecurity);
static member Create : string * int * System.IO.FileOptions * System.Security.AccessControl.FileSecurity -> System.IO.FileStream

Parameters

path
String String String String

The name of the file.

bufferSize
Int32 Int32 Int32 Int32

The number of bytes buffered for reads and writes to the file.

options
FileOptions FileOptions FileOptions FileOptions

One of the FileOptions values that describes how to create or overwrite the file.

fileSecurity
FileSecurity FileSecurity FileSecurity FileSecurity

One of the FileSecurity values that determines the access control and audit security for the file.

Returns

A new file with the specified buffer size, file options, and file security.

Exceptions

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.

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.

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.

Security

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

Applies to