使用WebClient访问本地文件 [英] Using WebClient to access local files

查看:61
本文介绍了使用WebClient访问本地文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我开发了一个C#应用程序,该应用程序需要通过HTTP,FTP以及有时还通过本地文件(file://)访问许多不同的内容源.

I develop a C# application that needs to access many different content source both through HTTP, FTP and sometimes local files also (file://).

我想采用一种统一的方式来通过不同的协议访问这些文件,所以我选择了WebClient来实现.

I wanted to have a uniform way that I access these files through different protocols, so I had choosen WebClient to do this.

它适用于MSDN上记录的所有不同协议(FTP,HTTP,本地文件等),但是出了点问题...在几次成功请求后,我无法使用URI访问任何文件本地文件(file://c:\ some_dir \ somefile.ext).

It works well for all the different protocols as documented on MSDN (FTP, HTTP, local files, and so on), but then something goes wrong... after a couple successfull requests I just cannot access any files using URI for local files (file://c:\some_dir\somefile.ext).

我已经检查了URI是否正确,如果我在浏览器中输入URI,它将很容易地打开文件.我已经认识到一件非常有趣的事情-对于本地文件,它在一开始也能很好地工作.如果我尝试使用WebClient加载本地文件的内容,那么从一开始它就可以很好地工作.

I have already checked the URI is correct, If I enter it in a browser it opens the file easily. I have recognized one really interesting thing - It also works well in the beginning for local files. If I try to load contents of a local file using WebClient, right at the start it all works well.

我的程序如下:

  1. 主线程启动
  2. 主线程上的一些初始化
  3. 多线程从不同来源下载内容
  4. 等待所有线程完成
  5. 单线程处理下载的东西

如前所述,如果我将WebClient.DownloadData(url)调用放在第一步中,则可以正常工作,但是当我尝试在第三步的任何线程中访问COMPLETELY相同的URI时,无法获取任何以"file://"开头的URI的本地文件.

As I've described before if I put my WebClient.DownloadData(url) invocation in the first step, it works fine, but when I try to access the COMPLETELY same URI in any of the threads of the third step, it fails to get any local files with URI starting with "file://".

我使用非常简单的代码来下载/获取本地文件:

I use a very simple code to download/get local files:

WebClient wc = new WebClient();
data = wc.DownloadData(url);

我想知道哪里会出问题...也许我在前面的步骤中或在并发线程中设置了某些阻止我访问本地文件的内容?在前面的步骤中,我确实从FTP服务器和通过HTTP下载内容,也许这引起了问题?如果在主线程的开头尝试,则可以轻松访问任何本地文件.访问FTP内容时,我也会设置凭据.也许这会影响我以后的请求?

I wonder what could go wrong... Maybe I set something in the previous steps or in a concurrent thread that prevents me to access local files? In previous steps I do dowload content from FTP servers and through HTTP, maybe that causes the problem? I can easily access any local file if I try at the beginning of the main thread. When accessing FTP content I set credentials also. Maybe this is what effects my later requests?

推荐答案

好吧,事实发生后我来了,但是希望这可以对某人有所帮助.

Well, I'm here way after the fact, but hopefully, this can help someone.

最好使用Uri对象,而不要使用字符串路径.当它是本地文件时,让Uri构造函数处理"file://"部分.实际的URI甚至对本地文件而言,都倾向于使用斜杠而不是反斜杠.但是,如果只使用Uri对象,则不必担心.

Rather than string paths, you're better using Uri objects. When it's a local file, let the Uri constructor take care of the "file://" part. The actual URI prefers slashes to backslashes, even for local files. If you just use the Uri object, you don't have to worry about that, though.

public byte[] Load(string fileName)
{
    Uri uri = new Uri(fileName);
    var client = new WebClient();
    return client.DownloadData(uri);
}

当然,您需要进行错误处理等,但这基本上对我有用.

Of course, you need error-handling and such, but this is basically what works for me.

如果您只想使用File.ReadAllBytes()方法,请按以下步骤操作.

If you want to just use the File.ReadAllBytes() method, here's how you would do it.

public byte[] Load(string fileName)
{
    Byte[] retVal = null;
    Uri uri = new Uri(fileName);
    if(uri.Scheme == "file")
    {
        retVal = File.ReadAllBytes(uri.LocalPath);
    }
    else
    {
        var client = new WebClient();
        retVal = client.DownloadData(uri);
    }
    return retVal;
}

这篇关于使用WebClient访问本地文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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