FileInfo.CreateText 方法
创建写入新文本文件的 StreamWriter。
**命名空间:**System.IO
**程序集:**mscorlib(在 mscorlib.dll 中)
语法
声明
Public Function CreateText As StreamWriter
用法
Dim instance As FileInfo
Dim returnValue As StreamWriter
returnValue = instance.CreateText
public StreamWriter CreateText ()
public:
StreamWriter^ CreateText ()
public StreamWriter CreateText ()
public function CreateText () : StreamWriter
返回值
新的 StreamWriter。
异常
异常类型 | 条件 |
---|---|
文件名为目录。 |
|
磁盘为只读。 |
|
调用方没有所要求的权限。 |
备注
默认情况下,将向所有用户授予对新文件的完全读/写访问权限。
下表列出了其他典型或相关的 I/O 任务的示例。
若要执行此操作... |
请参见本主题中的示例... |
---|---|
写入文本文件。 |
|
读取文本文件。 |
|
向文件中追加文本。 |
|
复制文件。 |
|
重命名或移动文件。 |
|
删除文件。 |
|
读取二进制文件。 |
|
写入二进制文件。 |
|
创建目录。 |
示例
下面的示例说明 CreateText 方法。
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)
If fi.Exists = False Then
'Create a file to write to.
Dim sw As StreamWriter = fi.CreateText()
sw.WriteLine("Hello")
sw.WriteLine("And")
sw.WriteLine("Welcome")
sw.Flush()
sw.Close()
End If
'Open the file to read from.
Dim sr As StreamReader = fi.OpenText()
Do While sr.Peek() >= 0
Console.WriteLine(sr.ReadLine())
Loop
sr.Close()
End Sub
End Class
using System;
using System.IO;
class Test
{
public static void Main()
{
string path = @"c:\temp\MyTest.txt";
FileInfo fi = new FileInfo(path);
if (!fi.Exists)
{
//Create a file to write to.
using (StreamWriter sw = fi.CreateText())
{
sw.WriteLine("Hello");
sw.WriteLine("And");
sw.WriteLine("Welcome");
}
}
//Open the file to read from.
using (StreamReader sr = fi.OpenText())
{
string s = "";
while ((s = sr.ReadLine()) != null)
{
Console.WriteLine(s);
}
}
}
}
using namespace System;
using namespace System::IO;
int main()
{
String^ path = "c:\\temp\\MyTest.txt";
FileInfo^ fi = gcnew FileInfo( path );
if ( !fi->Exists )
{
//Create a file to write to.
StreamWriter^ sw = fi->CreateText();
try
{
sw->WriteLine( "Hello" );
sw->WriteLine( "And" );
sw->WriteLine( "Welcome" );
}
finally
{
if ( sw )
delete (IDisposable^)sw;
}
}
//Open the file to read from.
StreamReader^ sr = fi->OpenText();
try
{
String^ s = "";
while ( s = sr->ReadLine() )
{
Console::WriteLine( s );
}
}
finally
{
if ( sr )
delete (IDisposable^)sr;
}
}
import System.*;
import System.IO.*;
class Test
{
public static void main(String[] args)
{
String path = "c:\\temp\\MyTest.txt";
FileInfo fi = new FileInfo(path);
if (!(fi.get_Exists())) {
//Create a file to write to.
StreamWriter sw = fi.CreateText();
try {
sw.WriteLine("Hello");
sw.WriteLine("And");
sw.WriteLine("Welcome");
}
finally {
sw.Dispose();
}
}
//Open the file to read from.
StreamReader sr = fi.OpenText();
try {
String s = "";
while ((s = sr.ReadLine()) != null) {
Console.WriteLine(s);
}
}
finally {
sr.Dispose();
}
} //main
} //Test
.NET Framework 安全性
- FileIOPermission 用于读取和写入文件。关联的枚举:FileIOPermissionAccess.Read、FileIOPermissionAccess.Write
平台
Windows 98、Windows 2000 SP4、Windows CE、Windows Millennium Edition、Windows Mobile for Pocket PC、Windows Mobile for Smartphone、Windows Server 2003、Windows XP Media Center Edition、Windows XP Professional x64 Edition、Windows XP SP2、Windows XP Starter Edition
.NET Framework 并不是对每个平台的所有版本都提供支持。有关受支持版本的列表,请参见系统要求。
版本信息
.NET Framework
受以下版本支持:2.0、1.1、1.0
.NET Compact Framework
受以下版本支持:2.0、1.0
请参见
参考
FileInfo 类
FileInfo 成员
System.IO 命名空间
StreamWriter