public int GetFileSize(string path)
{
System.IO.FileInfo fileInfo = null;
try
{
fileInfo = new System.IO.FileInfo(path);
}
catch
{
return 0;
}
if (fileInfo != null && fileInfo.Exists)
{
return (int)System.Math.Ceiling(fileInfo.Length / 1024.0);
}
else
{
return 0;
}
}
private void Btn_Click(object sender, RoutedEventArgs e)
{
OpenFileDialog dialog = new OpenFileDialog();
dialog.Multiselect = false;
dialog.Title = "请选择文件";
dialog.Filter = "所有文件(*.*)|*.*";
var res=dialog.ShowDialog();
if (res == null)
{
return;
}
if(!res.Value)
{
return;
}
string path = dialog.FileName;
MessageBox.Show(GetFileSize(path).ToString());
}