FileStream 类
定义
重要
一些信息与预发行产品相关,相应产品在发行之前可能会进行重大修改。 对于此处提供的信息,Microsoft 不作任何明示或暗示的担保。
为文件提供 Stream,支持同步和异步读取和写入操作。
public ref class FileStream : System::IO::Stream
public class FileStream : System.IO.Stream
[System.Runtime.InteropServices.ComVisible(true)]
public class FileStream : System.IO.Stream
type FileStream = class
inherit Stream
[<System.Runtime.InteropServices.ComVisible(true)>]
type FileStream = class
inherit Stream
Public Class FileStream
Inherits Stream
- 继承
- 继承
- 派生
- 属性
示例
以下示例演示了一些 FileStream 构造函数。
using namespace System;
using namespace System::IO;
using namespace System::Text;
void AddText( FileStream^ fs, String^ value )
{
array<Byte>^info = (gcnew UTF8Encoding( true ))->GetBytes( value );
fs->Write( info, 0, info->Length );
}
int main()
{
String^ path = "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 );
try
{
AddText( fs, "This is some text" );
AddText( fs, "This is some more text," );
AddText( fs, "\r\nand this is on a new line" );
AddText( fs, "\r\n\r\nThe following is a subset of characters:\r\n" );
for ( int i = 1; i < 120; i++ )
{
AddText( fs, Convert::ToChar( i ).ToString() );
//Split the output at every 10th character.
if ( Math::IEEERemainder( Convert::ToDouble( i ), 10 ) == 0 )
{
AddText( fs, "\r\n" );
}
}
}
finally
{
if ( fs )
delete (IDisposable^)fs;
}
}
//Open the stream and read it back.
{
FileStream^ fs = File::OpenRead( path );
try
{
array<Byte>^b = gcnew array<Byte>(1024);
UTF8Encoding^ temp = gcnew UTF8Encoding( true );
while ( fs->Read( b, 0, b->Length ) > 0 )
{
Console::WriteLine( temp->GetString( b ) );
}
}
finally
{
if ( fs )
delete (IDisposable^)fs;
}
}
}
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))
{
AddText(fs, "This is some text");
AddText(fs, "This is some more text,");
AddText(fs, "\r\nand this is on a new line");
AddText(fs, "\r\n\r\nThe following is a subset of characters:\r\n");
for (int i=1;i < 120;i++)
{
AddText(fs, Convert.ToChar(i).ToString());
}
}
//Open the stream and read it back.
using (FileStream fs = File.OpenRead(path))
{
byte[] b = new byte[1024];
UTF8Encoding temp = new UTF8Encoding(true);
int readLen;
while ((readLen = fs.Read(b,0,b.Length)) > 0)
{
Console.WriteLine(temp.GetString(b,0,readLen));
}
}
}
private static void AddText(FileStream fs, string value)
{
byte[] info = new UTF8Encoding(true).GetBytes(value);
fs.Write(info, 0, info.Length);
}
}
open System
open System.IO
open System.Text
let addText (fs:FileStream) (value: string) =
let info = UTF8Encoding(true).GetBytes value
fs.Write(info, 0, info.Length);
let path = @"c:\temp\MyTest.txt"
// Delete the file if it exists.
if File.Exists path then
File.Delete path
//Create the file.
do
use fs = File.Create path
addText fs "This is some text"
addText fs "This is some more text,"
addText fs "\r\nand this is on a new line"
addText fs "\r\n\r\nThe following is a subset of characters:\r\n"
for i = 1 to 119 do
Convert.ToChar i
|> string
|> addText fs
do
//Open the stream and read it back.
use fs = File.OpenRead path
let b = Array.zeroCreate 1024
let temp = UTF8Encoding true
let mutable readLen = fs.Read(b,0,b.Length);
while readLen> 0 do
printfn $"{temp.GetString(b,0,readLen)}"
readLen <- fs.Read(b,0,b.Length)
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)
AddText(fs, "This is some text")
AddText(fs, "This is some more text,")
AddText(fs, Environment.NewLine & "and this is on a new line")
AddText(fs, Environment.NewLine & Environment.NewLine)
AddText(fs, "The following is a subset of characters:" & Environment.NewLine)
Dim i As Integer
For i = 1 To 120
AddText(fs, Convert.ToChar(i).ToString())
Next
fs.Close()
'Open the stream and read it back.
fs = File.OpenRead(path)
Dim b(1023) As Byte
Dim temp As UTF8Encoding = New UTF8Encoding(True)
Do While fs.Read(b, 0, b.Length) > 0
Console.WriteLine(temp.GetString(b))
Loop
fs.Close()
End Sub
Private Shared Sub AddText(ByVal fs As FileStream, ByVal value As String)
Dim info As Byte() = New UTF8Encoding(True).GetBytes(value)
fs.Write(info, 0, info.Length)
End Sub
End Class
以下示例演示如何异步写入文件。 此代码在 WPF 应用中运行,该应用具有名为 UserInput 的 TextBlock,以及与名为 Button_Click 的 Click 事件处理程序挂钩的按钮。 文件路径需要更改为计算机上存在的文件。
using System;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.IO;
namespace WpfApplication1
{
public partial class MainWindow : Window
{
public MainWindow()
{
InitializeComponent();
}
private async void Button_Click(object sender, RoutedEventArgs e)
{
UnicodeEncoding uniencoding = new UnicodeEncoding();
string filename = @"c:\Users\exampleuser\Documents\userinputlog.txt";
byte[] result = uniencoding.GetBytes(UserInput.Text);
using (FileStream SourceStream = File.Open(filename, FileMode.OpenOrCreate))
{
SourceStream.Seek(0, SeekOrigin.End);
await SourceStream.WriteAsync(result, 0, result.Length);
}
}
}
}
Imports System.IO
Imports System.Text
Class MainWindow
Private Async Sub Button_Click(sender As Object, e As RoutedEventArgs)
Dim uniencoding As UnicodeEncoding = New UnicodeEncoding()
Dim filename As String = "c:\Users\exampleuser\Documents\userinputlog.txt"
Dim result As Byte() = uniencoding.GetBytes(UserInput.Text)
Using SourceStream As FileStream = File.Open(filename, FileMode.OpenOrCreate)
SourceStream.Seek(0, SeekOrigin.End)
Await SourceStream.WriteAsync(result, 0, result.Length)
End Using
End Sub
End Class
注解
有关此 API 的详细信息,请参阅 FileStream的补充 API 备注。
构造函数
属性
CanRead |
获取一个值,该值指示当前流是否支持读取。 |
CanSeek |
获取一个值,该值指示当前流是否支持查找。 |
CanTimeout |
获取一个值,该值确定当前流是否可以超时。 (继承自 Stream) |
CanWrite |
获取一个值,该值指示当前流是否支持写入。 |
Handle |
已过时.
已过时.
已过时.
获取当前 |
IsAsync |
获取一个值,该值指示 |
Length |
获取流的长度(以字节为单位)。 |
Name |
获取在 |
Position |
获取或设置此流的当前位置。 |
ReadTimeout |
获取或设置一个值(以毫秒为单位),该值确定流在超时前尝试读取的时间。 (继承自 Stream) |
SafeFileHandle |
获取一个 SafeFileHandle 对象,该对象表示当前 FileStream 对象封装的文件的操作系统文件句柄。 |
WriteTimeout |
获取或设置一个值(以毫秒为单位),该值确定流在超时之前尝试写入的时间。 (继承自 Stream) |
方法
显式接口实现
IDisposable.Dispose() |
释放 Stream使用的所有资源。 (继承自 Stream) |
扩展方法
GetAccessControl(FileStream) |
返回文件的安全信息。 |
SetAccessControl(FileStream, FileSecurity) |
更改现有文件的安全属性。 |
CopyToAsync(Stream, PipeWriter, CancellationToken) |
使用取消令牌从 Stream 异步读取字节并将其写入指定的 PipeWriter。 |
AsInputStream(Stream) |
将适用于 Windows 应用商店应用的 .NET 中的托管流转换为 Windows 运行时中的输入流。 |
AsOutputStream(Stream) |
将适用于 Windows 应用商店应用的 .NET 中的托管流转换为 Windows 运行时中的输出流。 |
AsRandomAccessStream(Stream) |
将指定的流转换为随机访问流。 |
ConfigureAwait(IAsyncDisposable, Boolean) |
配置如何执行从异步可释放项返回的任务的 await。 |