在.NET中获取标题/文件大小 [英] Getting a header/filesize in .NET
问题描述
目前我正在使用webbrowser控件加载页面并提取数据,然后使用 WebClient.Download
获取文件。有没有办法在下载整个文件之前可以使用WebClient检查文件大小?
我发现了一个很好的文章, em> 在您之前获取文件长度超过HTTP下载它 ,它为我提供了一种非常好的方式:
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中获取标题/文件大小的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!