本文介绍了在.NET中获取标题/文件大小的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个链接到两个文件。它们可能是同一个文件,但它们可能不是相同的URL。我想通过在完整下载之前检查内容长度来确定它们是否相同。这是最好的方法?



目前我正在使用webbrowser控件加载页面并提取数据,然后使用 WebClient.Download 获取文件。有没有办法在下载整个文件之前可以使用WebClient检查文件大小?

解决方案

我发现了一个很好的文章, em> ,它为我提供了一种非常好的方式:

  static public long GetFileSize (string url)
{
using(WebClient obj = new WebClient())
using(Stream s = obj.OpenRead(url))
return long.Parse(obj。 responseHeaders响应[ 内容长度]的ToString());
}


I have a link to two files. They may be the same file, but they may not be the same URL. I want to figure out if they are the same by checking the content-length before doing a full download. What's the best way to do this?

Currently I am using webbrowser control to load the page and extract data and then using WebClient.Download to get the file. Is there a way I can use WebClient to check the filesize before downloading the entire file?

解决方案

I found an excellent article, Get file length over HTTP before you download it, which provides a way that works very well for me:

    static public long GetFileSize(string url)
    {
        using (WebClient obj = new WebClient())
        using (Stream s = obj.OpenRead(url))
            return long.Parse(obj.ResponseHeaders["Content-Length"].ToString());
    }

这篇关于在.NET中获取标题/文件大小的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-28 11:54