<返回更多

C# 获取网络路径文件大小信息的方法

2021-10-29    大墨鱼85312996
加入收藏

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);

}

声明:本站部分内容来自互联网,如有版权侵犯或其他问题请与我们联系,我们将立即删除或处理。
▍相关推荐
更多资讯 >>>