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:13  苏秦与真相  阅读(66)  评论(0编辑  收藏  举报