0%

c#获取图片文件大小(亦可获取其他文件大小)

代码如下:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
/// <summary>
/// 获取图片文件大小
/// </summary>
/// <param name="path">图片路径</param>
/// <returns></returns>
public static double GetFileSize(string path)
{
System.IO.FileInfo fileInfo = null;
try
{
fileInfo = new System.IO.FileInfo(path);
}
catch (Exception e)
{
Console.WriteLine($@"获取图片{path}大小失败 Getcatch");
Console.WriteLine(e);
return 0;
}
if (fileInfo != null && fileInfo.Exists)
{
//if ((int)System.Math.Ceiling(fileInfo.Length / 1024.0) >= 1024)
//{
// Console.WriteLine((System.Math.Floor(fileInfo.Length / 1024.0 / 1024.0) + System.Math.Floor(fileInfo.Length / 1024.0 % 1024.0/10)/100) + "MB");
// return (System.Math.Floor(fileInfo.Length / 1024.0 ) );
//}
// Console.WriteLine(System.Math.Ceiling(fileInfo.Length / 1024.0)+"kb");
return System.Math.Ceiling(fileInfo.Length / 1024.0);
}
else
{
Console.WriteLine($@"获取图片{path}大小失败 Getelse");
return 0;
}
}