Information
The topic you requested is included in another documentation set. For convenience, it's displayed below. Choose Switch to see the topic in its original location.

File.Create Method

Creates a file in the specified path.

Overload List

Creates a file in the specified path.

Supported by the .NET Compact Framework.

[Visual Basic] Overloads Public Shared Function Create(String) As FileStream
[C#] public static FileStream Create(string);
[C++] public: static FileStream* Create(String*);
[JScript] public static function Create(String) : FileStream;

Creates or overwrites the specified file.

Supported by the .NET Compact Framework.

[Visual Basic] Overloads Public Shared Function Create(String, Integer) As FileStream
[C#] public static FileStream Create(string, int);
[C++] public: static FileStream* Create(String*, int);
[JScript] public static function Create(String, int) : FileStream;

Example

[Visual Basic, C#, C++] The following example creates a file with the specified buffer size.

[Visual Basic, C#, C++] Note   This example shows how to use one of the overloaded versions of Create. For other examples that might be available, see the individual overload topics.
[Visual Basic] 
Imports System
Imports System.IO
Imports System.Text

Public Class Test
    Public Shared Sub Main()
        Dim path As String = "c:\temp\MyTest.txt"

        ' Delete the file if it exists.
        If File.Exists(path) Then
            File.Delete(path)
        End If

        ' Create the file.
        Dim 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)
        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()
    End Sub
End Class

[C#] 
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);
            }
        }
    }
}

[C++] 
#using <mscorlib.dll>

using namespace System;
using namespace System::IO;
using namespace System::Text;

int main() {
    String* path = S"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 {
        Byte info[] = (new UTF8Encoding(true))->GetBytes(S"This is some text in the file.");
        // Add some information to the file.
        fs->Write(info, 0, info->Length);
    } __finally {
        if (fs) __try_cast<IDisposable*>(fs)->Dispose();
    }

    // Open the stream and read it back.
    StreamReader* sr = File::OpenText(path);
    try {
        String* s = S"";
        while (s = sr->ReadLine()) {
            Console::WriteLine(s);
        }
    } __finally {
        if (sr) __try_cast<IDisposable*>(sr)->Dispose();
    }
}

[JScript] No example is available for JScript. To view a Visual Basic, C#, or C++ example, click the Language Filter button Language Filter in the upper-left corner of the page.

See Also

File Class | File Members | System.IO Namespace

Show:
© 2014 Microsoft