C#进程无法访问文件,因为它正在被另一个进程使用 [英] C# process cannot access file because it is being used by another process

查看:1034
本文介绍了C#进程无法访问文件,因为它正在被另一个进程使用的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

请帮助我解决我面临的问题.

Please help me out with the problem I am facing.

我正在用C#为XML文件编写一个导入程序.每次运行导入时,都需要从URL下载XML文件.

I am writing an importer in C# for an xml file. Every time I run the import I need to download the XML file from a URL.

我已经将以下代码下载了:

I have wirtten the following code to download it:

var xmlPath = @"C:\Desktop\xxx.xml";
public void DownloadFile(string url, string saveAs)
{
    using(var webClient = new WebClient())
    {
        webClient.DownloadFileAsync(new Uri(url), saveAs);
    }
}

_downloader.DownloadFile(Config.FeedUrl, xmlPath);调用该方法.网址位于配置文件(Config.FeedUrl)中.

and _downloader.DownloadFile(Config.FeedUrl, xmlPath); to call the method. The Url is in the config file (Config.FeedUrl).

然后,当我尝试GetProperties(xmlPath);时,出现异常进程无法访问该文件,因为该文件正在被另一个进程使用.

Then when I am trying to GetProperties(xmlPath); I get the Exception "Process Cannot access the file because the file is being used by another process.

我确保目的地存在,但是我不确定为什么会收到此错误.

I made sure that the destination exists but i am not sure why I get this error.

有人可以帮我吗?

谢谢

推荐答案

当您尝试访问属性时,似乎您的异步下载操作尚未完成.在访问文件之前,您确定下载已完成吗?

Looks like your asynch download operation is yet to complete when you try to access the properties. Have you made sure that the download is completed before accessing the file?

您可以在DownloadFileCompleted事件中访问文件.

You can access the file in the DownloadFileCompleted event.

http://msdn.microsoft.com/zh-CN/library/system.net.webclient.downloadfilecompleted.aspx

这篇关于C#进程无法访问文件,因为它正在被另一个进程使用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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