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.
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