File.WriteAllBytes不会阻止 [英] File.WriteAllBytes does not block

查看:1567
本文介绍了File.WriteAllBytes不会阻止的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有这样一个如此简单的代码:

I have a simple piece of code like so:

File.WriteAllBytes(Path.Combine(temp, node.Name), stuffFile.Read(0, node.FileHeader.FileSize));



有人会认为, WriteAllBytes 将是一个阻止呼叫,因为它在C#5.0异步同行,并没有任何的MSDN文档,这是无阻塞的任何地方注明。然而,当一个文件是一个合理的规模(未块状,但某处20MB的领域)的呼叫之后其打开文件似乎写入完成之前被调用,并且该文件被打开(该程序抱怨其损坏,这是正确的)和 WriteAllBytes 则抱怨该文件是在另一个进程打开。这里发生了什么?!出于好奇的缘故,这是用来打开文件的代码:

One would think that WriteAllBytes would be a blocking call as it has Async counterparts in C# 5.0 and it doesn't state anywhere in any MSDN documentation that it is non-blocking. HOWEVER when a file is of a reasonable size (not massive, but somewhere in the realms of 20mb) the call afterwards which opens the file seems to be called before the writing is finished, and the file is opened (the program complains its corrupted, rightly so) and the WriteAllBytes then complains the file is open in another process. What is going on here?! For curiosity sake, this is the code used to open the file:

System.Diagnostics.Process.Start(Path.Combine(temp, node.Name));



任何人之前遇到这种怪事的?或者是我是一个金发碧眼和做错了什么?

Anyone experience this sort of weirdness before? Or is it me being a blonde and doing something wrong?

如果它确实是堵,什么可能会造成这个问题?

If it is indeed blocking, what could possibly be causing this issue?

编辑:我把完整的方法了。

var node = item.Tag as FileNode;
stuffFile.Position = node.FileOffset;
string temp = Path.GetTempPath();
File.WriteAllBytes(Path.Combine(temp, node.Name), stuffFile.Read(0, node.FileHeader.FileSize));
System.Diagnostics.Process.Start(Path.Combine(temp, node.Name));



什么似乎是发生的是,的Process.Start 被调用之前 WriteAllBytes 完成后,其试图打开该文件,然后 WriteAllBytes 抱怨另一过程在文件上的锁。

What seems to be happening is that Process.Start is being called BEFORE WriteAllBytes is finished, and its attempting to open the file, and then WriteAllBytes complains about another process holding the lock on the file.

推荐答案

没有,WriteAllBytes是一个阻塞,同步方法。正如你所说,如果不是,文档会这么说。

No, WriteAllBytes is a blocking, synchronous method. As you stated, if it were not, the documentation would say so.

可能的病毒扫描程序仍在忙于扫描你刚才写的文件,并负责锁定文件。尝试暂时禁用扫描仪来测试我的假设。

Possibly the virus scanner is still busy scanning the file that you just wrote, and is responsible for locking the file. Try temporarily disabling the scanner to test my hypothesis.

这篇关于File.WriteAllBytes不会阻止的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆