File.Create Method (String)
Creates or overwrites a file in the specified path.
Assembly: mscorlib (in mscorlib.dll)
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 Nothing.
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.
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 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. Dim 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) fs.Close() ' Open the stream and read it back. Dim sr As StreamReader = File.OpenText(path) Do While sr.Peek() >= 0 Console.WriteLine(sr.ReadLine()) Loop sr.Close() Catch ex As Exception Console.WriteLine(ex) End Try End Sub End Class
Windows Phone 8.1, Windows Phone 8, Windows 8.1, Windows Server 2012 R2, Windows 8, Windows Server 2012, Windows 7, Windows Vista SP2, Windows Server 2008 (Server Core Role not supported), Windows Server 2008 R2 (Server Core Role supported with SP1 or later; Itanium not supported)
The .NET Framework does not support all versions of every platform. For a list of the supported versions, see .NET Framework System Requirements.