如何在C#中异步打开文件 [英] How do I Open A File Asynchronously in C#
本文介绍了如何在C#中异步打开文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
即使在开始复制之前,异步执行文件打开的最佳方法是什么。我得到的例外是文件无法访问,因为它被另一个进程使用。我的代码如下所示:
What is the best way to execute file opening asynchronously even before copying begins. The exception I get is that the file is not accessible because its being used by another process.My code is shown below:
try
{
using (var sourceStream = new FileStream(pathToDownloadedMedia, FileMode.Open, FileAccess.Read, FileShare.None, bufferSize: 4096, useAsync: true))
{
using (var destinationStream = File.Create(pathToProcessedMedia))
{
await sourceStream.CopyToAsync(destinationStream);
}
}
}
catch (Exception)
{
throw new ApplicationException();
}
推荐答案
使用FileShare.None
你明确定义你得到的行为。
更好地使用FileShare.ReadWrite
,看看会发生什么。< br $> b $ b
祝你好运。
By usingFileShare.None
you explicitly define the behaviour that you get.
Better useFileShare.ReadWrite
and see what happens.
Good luck.
这篇关于如何在C#中异步打开文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文