了解 File.Create 和 File.Write 中的 FileStream
Understand FileStream in File.Create and File.Write
我不明白为什么我需要在访问文件并写入之前处理 File.Create 或将创建内容包装在 using 站中。为什么我需要处理 Stream class 的 File.Create 初始化的实例,并且必须创建另一个 Stream实例要写入吗?他们在不同的线程上,为什么?
为什么创建、写入和删除不能共享同一个Stream?也许我只是不明白Stream。
例如:
File.Create(...);
File.ReadLine(...); <-- The process cannot access the file ... because it is being used by another process
using (File.Create(...)) {};
File.ReadLine(...); <-- OK
请参阅 MSDN 上 File.Create
页面的 备注 部分(重点是我的):
The FileStream object created by this method has a default FileShare
value of None; no other process or code can access the created file
until the original file handle is closed.
我不明白为什么我需要在访问文件并写入之前处理 File.Create 或将创建内容包装在 using 站中。为什么我需要处理 Stream class 的 File.Create 初始化的实例,并且必须创建另一个 Stream实例要写入吗?他们在不同的线程上,为什么?
为什么创建、写入和删除不能共享同一个Stream?也许我只是不明白Stream。
例如:
File.Create(...);
File.ReadLine(...); <-- The process cannot access the file ... because it is being used by another process
using (File.Create(...)) {};
File.ReadLine(...); <-- OK
请参阅 MSDN 上 File.Create
页面的 备注 部分(重点是我的):
The FileStream object created by this method has a default FileShare value of None; no other process or code can access the created file until the original file handle is closed.