英文:
How to open a File in the same process twice in 2 different FileStreams in C#
问题
我有2个类似这样的文件流:
using (var fs = new FileStream(source, FileMode.Open, FileAccess.Write, FileShare.Read)){
//这里有一些代码
var checksum = getChecksum(source);
}
public long getChecksum(string source){
using (var fs = new FileStream(source, FileMode.Open, FileAccess.Read, FileShare.Read)){
//错误:由于另一个进程正在使用文件'C:\Temp\archive.dat',因此无法访问该进程。
}
}
为什么会出现这个错误?我已经将FileAccess设置为Read / ReadWrite / ...,但什么都不起作用。而且我不能将流传递给getChecksum()
函数,因为我在其他地方使用该函数。
上面的流不应该允许读取吗?
此线程中没有找到我的答案。
英文:
I have 2 Filestreams like this:
using (var fs = new FileStream(source, FileMode.Open, FileAccess.Write, FileShare.Read)){
//some code here
var checksum = getChecksum(source);
}
public long getChecksum(string source){
using (var fs = new FileStream(source, FileMode.Open, FileAccess.Read, FileShare.Read)){
//Error: The process cannot access the file 'C:\Temp\archive.dat' because it is being used by another process.
}
}
why does this error appear? I already set the FileAccess to Read / ReadWrite / ... but nothing worked. And I can't pass the Stream to the getChecksum()
function, because I use the function in other places.
shouldn't the above stream allow for reading?
https://stackoverflow.com/questions/8214518/create-2-filestream-on-the-same-file-in-the-same-process I couldn't find my awnser in this thread.
答案1
得分: 1
你正在尝试在第一个文件流仍然打开的情况下打开第二个文件流,因为using
块仍然在作用域内。文档 表示在使用 FileShare.Read
时可能需要额外的权限。
在你链接的另一个线程中,有人提到可能需要将第二个流的 FileShare
模式设置为 FileShare.ReadWrite
,以便第一个仍然具有访问权限。这很可能是问题的主要原因,因为第二个流试图获得独占访问权限,除了读取之外,但已经有一个写入流打开。
另外,你可以尝试在调用 getCheckSum()
之前在第一个文件流上调用 Flush()
以强制将数据写入。
using (var fs = new FileStream(source, FileMode.Open, FileAccess.Write, FileShare.Read)){
// 这里放一些代码
// 强制将缓冲数据写入
fs.Flush();
var checksum = getChecksum(source);
}
public long getChecksum(string source){
using (var fs = new FileStream(source, FileMode.Open, FileAccess.Read, FileShare.ReadWrite)){ // 使用 FileShare.ReadWrite 以便第一个文件流仍然能够访问
}
}
英文:
You are trying to open a second FileStream while the first is still open as the using block is still in scope. The docs say that when using FileShare.Read additional permissions may be required.
In the other thread you linked someone mentioned may need to set the FileShare mode on the second stream to FileShare.ReadWrite so the first still has access. This is probably the most likely cause as the second is trying to obtain exclusive access except for reads, but there is already a write stream open.
Also you could try calling Flush()
on the first FileStream to force the data to be written before calling getCheckSum()
.
using (var fs = new FileStream(source, FileMode.Open, FileAccess.Write, FileShare.Read)){
//some code here
//Force the buffered data to be written
fs.Flush()
var checksum = getChecksum(source);
}
public long getChecksum(string source){
using (var fs = new FileStream(source, FileMode.Open, FileAccess.Read, FileShare.ReadWrite)){ //FileShare.ReadWrite so first FileStream can still access
}
}
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论