File.Create Method (String)

 
System_CAPS_noteNote

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

Creates or overwrites a file in the specified path.

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

Public Shared Function Create (
	path As String
) As FileStream

Parameters

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
UnauthorizedAccessException

The caller does not have the required permission.

-or-

path specified a file that is read-only.

ArgumentException

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

ArgumentNullException

path is null.

PathTooLongException

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.

DirectoryNotFoundException

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

IOException

An I/O error occurred while creating the file.

NotSupportedException

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.

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

FileIOPermission

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
Silverlight
Available since 2.0
Windows Phone Silverlight
Available since 7.0
Return to top
Show: