IsolatedStorageFileStream.Close 方法

定义

释放与 IsolatedStorageFileStream 对象关联的资源。Releases resources associated with the IsolatedStorageFileStream object.

public:
 override void Close();
public override void Close ();
override this.Close : unit -> unit
Public Overrides Sub Close ()

示例

下面的代码示例演示 Close 方法。The following code example demonstrates the Close method.

IsolatedStorageFileStream source =
     new IsolatedStorageFileStream(this.userName,FileMode.Open,isoFile);
 // This stream is the one that data will be read from
 Console.WriteLine("Source can be read?" + (source.CanRead?"true":"false"));
 IsolatedStorageFileStream target =
     new IsolatedStorageFileStream("Archive\\ " + this.userName,FileMode.OpenOrCreate,isoFile);
 // This stream is the one that data will be written to
 Console.WriteLine("Target is writable?" + (target.CanWrite?"true":"false"));
 // Do work...
 // After you have read and written to the streams, close them
 source.Close();
 target.Close();
Dim source As New IsolatedStorageFileStream(UserName,FileMode.Open,isoFile)
 ' This stream is the one that data will be read from
 If source.CanRead Then
     Console.WriteLine("Source can read ? true")
 Else
     Console.WriteLine("Source can read ? false")
 End If
 Dim target As New IsolatedStorageFileStream("Archive\\ " & UserName, _
                                             FileMode.OpenOrCreate, _
                                             isoFile)
 ' This stream is the one that data will be written to
 If target.CanWrite Then
     Console.WriteLine("Target is writable? true")
 Else
     Console.WriteLine("Target is writable? false")
 End If
 ' After you have read and written to the streams, close them
 source.Close()
 target.Close()

注解

先前写入缓冲区的任何数据将在文件流关闭之前复制到文件中,因此在调用 Close 之前无需调用 FlushAny data previously written to the buffer is copied to the file before the file stream is closed, so it is not necessary to call Flush before invoking Close.

调用 Close 后,对文件流执行的任何操作都可能引发异常。Following a call to Close, any operations on the file stream might raise exceptions. 在调用 Close 一次后,如果再次调用,它将不执行任何操作。After Close has been called once, it does nothing if called again. Finalize() 方法将调用 Close,以便在垃圾回收器完成对象之前关闭文件流。The Finalize() method invokes Close so the file stream is closed before the garbage collector finalizes the object.

IsolatedStorageFileStream 对象需要一个 IsolatedStorageFile 对象来确定所访问文件的存储上下文。IsolatedStorageFileStream objects require an IsolatedStorageFile object that determines the storage context for the files accessed. 对于在不传递 IsolatedStorageFile 对象的情况下打开的流,将为正在执行的程序集创建默认的 IsolatedStorageFile 对象,然后在调用 Close 期间关闭。For streams opened without passing an IsolatedStorageFile object, a default IsolatedStorageFile object is created for the executing assembly and then closed during the call to Close.

备注

Close 方法调用释放设置为 true Dispose,以释放其资源,然后调用 SuppressFinalize 以取消垃圾回收器对此对象的终止。The Close method calls Dispose with disposing set to true to release its resources, then calls SuppressFinalize to suppress finalization of this object by the garbage collector.

适用于