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.

FileInfo.Create Method

Creates a file.

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

Return Value

A new file.

Remarks

By default, full read/write access to new files is granted to all users.

This method is a wrapper for the functionality provided by File.Create.

For an example of using this method, see the Example section below. The following table lists examples of other typical or related I/O tasks.

To do this... See the example in this topic...
Create a text file. Writing Text to a File
Write to a text file. Writing Text to a File
Read from a text file. Reading Text from a File
Append text to a file. Opening and Appending to a Log File

File.AppendText

FileInfo.AppendText

Copy a file. File.Copy

FileInfo.CopyTo

Rename or move a file. File.Move

FileInfo.MoveTo

Delete a file. File.Delete

FileInfo.Delete

Read from a binary file. Reading and Writing to a Newly Created Data File
Write to a binary file. Reading and Writing to a Newly Created Data File
Create a directory. CreateDirectory

Directory

Example

The following example creates a reference to a file, and then creates the file on disk using FileInfo.Create().

[Visual Basic] 
Imports System
Imports System.IO

Public Class DeleteTest
    Public Shared Sub Main()
        ' Create a reference to a file.
        Dim fi As New FileInfo("temp.txt")
        ' Actually create the file.
        Dim fs As FileStream = fi.Create()
        ' Modify the file as required, and then close the file.
        fs.Close()
        ' Delete the file.
        fi.Delete()
    End Sub 'Main
End Class 'DeleteTest

[C#] 
using System;
using System.IO;

public class DeleteTest 
{
    public static void Main() 
    {
        // Create a reference to a file.
        FileInfo fi = new FileInfo("temp.txt");
        // Actually create the file.
        FileStream fs = fi.Create();
        // Modify the file as required, and then close the file.
        fs.Close();
        // Delete the file.
        fi.Delete();
    }
}

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

using namespace System;
using namespace System::IO;

int main() {
    // Create a reference to a file.
    FileInfo* fi = new FileInfo(S"temp.txt");
    // Actually create the file.
    FileStream* fs = fi->Create();
    // Modify the file as required, and then close the file.
    fs->Close();
    // Delete the file.
    fi->Delete();
}

[JScript] 
import System;
import System.IO;

public class DeleteTest {
    public static function Main() : void {
        // Create a reference to a file.
        var fi : FileInfo = new FileInfo("temp.txt");
        // Actually create the file.
        var fs : FileStream = fi.Create();
        // Modify the file as required, and then close the file.
        fs.Close();
        // Delete the file.
        fi.Delete();
    }
}
DeleteTest.Main();

The following example creates a file, adds some text to it, and reads from the file.

[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"
        Dim fi As FileInfo = New FileInfo(path)

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

        'Create the file.
        Dim fs As FileStream = fi.Create()
        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 = fi.OpenText()

        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";
        FileInfo fi = new FileInfo(path);

        // Delete the file if it exists.
        if (fi.Exists) 
        {
            fi.Delete();
        }

        //Create the file.
        using (FileStream fs = fi.Create()) 
        {
            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 = fi.OpenText()) 
        {
            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";
    FileInfo* fi = new FileInfo(path);

    // Delete the file if it exists.
    if (fi->Exists) {
        fi->Delete();
    }

    //Create the file.
    FileStream* fs = fi->Create();
    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 = fi->OpenText();
    try {
        String* s = S"";
        while (s = sr->ReadLine()) {
            Console::WriteLine(s);
        }
    } __finally {
        if (sr) __try_cast<IDisposable*>(sr)->Dispose();
    }
}

Requirements

Platforms: Windows 98, Windows NT 4.0, Windows Millennium Edition, Windows 2000, Windows XP Home Edition, Windows XP Professional, Windows Server 2003 family, .NET Compact Framework

.NET Framework Security: 

See Also

FileInfo Class | FileInfo Members | System.IO Namespace | Working with I/O | Reading Text from a File | Writing Text to a File | Basic File I/O | Reading and Writing to a Newly Created Data File

Show:
© 2014 Microsoft