C# 获得文件大小(KB)

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


        }

 

    原文作者:Time2017
    原文地址: https://blog.csdn.net/Time2017/article/details/106714109
    本文转自网络文章,转载此文章仅为分享知识,如有侵权,请联系博主进行删除。
点赞