Feedback

C# - Get Directory Size Recursively

Veröffentlicht von am 3/13/2011
(0 Bewertungen)
Get Directory Size Recursively
        public static long GetDirectorySize(string p)
        {
            string[] a = Directory.GetFiles(p, "*.*");
            long b = 0;
            foreach (string name in a)
            {
                FileInfo info = new FileInfo(name);
                b += info.Length;
            }
            return b;
        }

        public static long GetDirectorySize(string b, Boolean recurse)
        {
            if (!recurse)
                return GetDirectorySize(b);

            long result = 0;
            Stack<string> stack = new Stack<string>();
            stack.Push(b);

            while (stack.Count > 0)
            {
                string dir = stack.Pop();
                try
                {
                    result += GetDirectorySize(dir);
                    foreach (string dn in Directory.GetDirectories(dir))
                    {
                        stack.Push(dn);
                    }
                }
                catch { }
            }
            return result;
        }
Abgelegt unter Get, Directory, Size, Recursively.

Kommentare zum Snippet

 

Logge dich ein, um hier zu kommentieren!