C# 直接获取文件夹占用空间大小

        /// <summary>
        /// 
        /// </summary>
        /// <param name="path"></param>
        /// <returns></returns>
        public static double GetDirectorySize(string path)
        {
            double result = 0d;
            try
            {
                if (Directory.Exists(path))
                {
                    dynamic fso = Activator.CreateInstance(Type.GetTypeFromProgID("Scripting.FileSystemObject"));
                    dynamic fldr = fso.GetFolder(path);
                    result = (double)fldr.size;
                    result = result / 1024 / 1024 / 1024;
                }
                else
                {
                    result = 0d;
                }
            }
            catch (Exception ex)
            {
                result = 0d;
            }

            return result;
        }

 

posted @ 2024-01-09 14:12  苏秦与真相  阅读(284)  评论(0编辑  收藏  举报