C# 获取网络路径文件大小信息的方法
private void button1_Click(object sender, EventArgs e)
{
label2.Text= GetInfo(textBox1.Text);
}
private string GetInfo(string URL)
{
string Results = "";
string filetype = URL.Substring(URL.LastIndexOf(".") + 1,
(URL.Length - URL.LastIndexOf(".") - 1));
Results = "类型:" + filetype.ToUpper();
string filename = URL.Substring(URL.LastIndexOf("/") + 1,
(URL.Length - URL.LastIndexOf("/") - 1));
Results += "|名称:" + filename;
long ContentL = 0;
if (URL.ToLower().StartsWith("http://"))
{
HttpWebRequest request = (HttpWebRequest)WebRequest.Create(URL);
request.MaximumAutomaticRedirections = 4;
request.MaximumResponseHeadersLength = 4;
request.Credentials = CredentialCache.DefaultCredentials;
HttpWebResponse response = (HttpWebResponse)request.GetResponse();
ContentL = response.ContentLength;
response.Close();
Results += "|大小:" + GetSize(ContentL) + "|额外信息:" + response.Server;
}
else if (URL.ToLower().StartsWith("ftp://"))
{
FtpWebRequest request = (FtpWebRequest)WebRequest.Create(URL);
request.Method = WebRequestMethods.Ftp.GetFileSize;
request.UseBinary = true;
FtpWebResponse response = null;
response = (FtpWebResponse)request.GetResponse();
Stream Fs = response.GetResponseStream();
ContentL = response.ContentLength;
Fs.Close();
response.Close();
Results += "|大小:" + GetSize(ContentL) + "|额外信息:" + response.WelcomeMessage;
}
else
{
Results += "|大小:无法检测";
}
return Results;
}
//大小转化
private string GetSize(long L)
{
float result;
string re = "";
if (L >= 1073741824)
{
result = L / 1073741824.00F;
re = "GB";
}
//else if (L >= 1048576)
//{
// result = L / 1048576.00F;
// re = "MB";
//}
else
{
result = L / 1024.00F;
re = "KB";
}
string File_Size = result.ToString("0.00");
return (File_Size + re);
}
【C# 获取网络路径文件大小信息的方法】
推荐阅读
- 网络推广泛滥,B2B平台运营应该怎么做?
- 网络攻击者最有可能攻击的IT资产列表
- 从AD域安全防护痛点看安全可信网络体系搭建
- 有哪些购物返利网络
- NAS到底是啥?看这一篇就够了
- 无线网络的2.4G与5G啥区别?平时连哪一个?看看实测数据
- Logistic Regression:最基础的神经网络
- 简述网络编程中的Socket
- 推荐四款免费网络安全工具
- 孩子行程码怎么获取?