0

I'm trying to list all folders of my c drive excluding the document folder which i do not seem to have access to. This first seemed rather simple to me but i found myself still struggling with it despite the seemingly rich .net library.

I can't post any code as what i have tried so far does not really work.

Hope someone will suggest something.

Meelfan Bmfp
  • 605
  • 1
  • 7
  • 30
  • "i found myself still struggling with it" Perhaps you could, at a minimum, describe what you're struggling with. It looks like it might be permissions, but that would be a guess. At the moment, this question reads like a request for a tutorial, which is off-topic for SO. – spender Jun 15 '15 at 12:00
  • start with root directory, iterate over folders/files, check size. keep in list or dictionary. use to your heart content. Not being able to show anything if you tried sounds rather .... suspicious ... – Noctis Jun 15 '15 at 12:01
  • I have administrative right on my laptop. the reason i'm excluding documents folder is because of i get "access denied error" – Meelfan Bmfp Jun 15 '15 at 12:03
  • Bear in mind that from Windows Vista, the folder `C:\Documents and Settings\ ` is not a true folder. It is a [junction point](https://en.wikipedia.org/wiki/NTFS_junction_point) to `C:\Users\ `. – Phylogenesis Jun 15 '15 at 12:05
  • I've already voted as "too broad", however this is a clear dupe of http://stackoverflow.com/questions/5098011/directory-enumeratefiles-unauthorizedaccessexception – spender Jun 15 '15 at 12:07
  • I started out with the code describerd here : https://msdn.microsoft.com/en-us/library/vstudio/bb546154(v=vs.100).aspx – Meelfan Bmfp Jun 15 '15 at 12:08
  • @MeelfanBmfp - Please edit your question with your code. – Enigmativity Jun 15 '15 at 12:37

2 Answers2

1

A small example here:

class DirectorySize
{
    public string DirectoryName;
    public long DirectorySizes;
}

public class Program
{
    static void Main(string[] args)
    {
        string[] cDirectories = Directory.GetDirectories("C:\\");

        List<DirectorySize> listSizes = new List<DirectorySize>();

        for (int i = 0; i < cDirectories.Length; i++)
        {
            long size = GetDirectorySize(cDirectories[i]);

            if(size != -1)
            {
                listSizes.Add(new DirectorySize() { DirectoryName = cDirectories[i], DirectorySizes = size });
            }
        }
    }

    private static long GetDirectorySize(string folderPath)
    {
        try
        {
           DirectoryInfo di = new DirectoryInfo(folderPath);
           return di.EnumerateFiles("*", SearchOption.AllDirectories).Sum(fi => fi.Length);
        }
        catch
        {
           // If you get Access denied error handle it here
           return -1;
        }
    }
}
Orkun Bekar
  • 1,447
  • 1
  • 15
  • 36
0

Another way of doing this is to implement a LINQ extension method (shamelessly stolen from the top answer to this question:

public static class Extensions
{
    public static IEnumerable<T> SkipExceptions<T>(this IEnumerable<T> values)
    {
        using (var enumerator = values.GetEnumerator())
        {
            bool next = true;
            while (next)
            {
                try
                {
                    next = enumerator.MoveNext();
                }
                catch
                {
                    continue;
                }

                if (next) yield return enumerator.Current;
            }
        }
    }
}

Then you can run your enumerator as follows:

DirectoryInfo di = new DirectoryInfo(@"C:\");
var size =
  di.EnumerateFiles("*", SearchOption.AllDirectories)
    .SkipExceptions()
    .Sum(fi => fi.Length);
Community
  • 1
  • 1
Phylogenesis
  • 7,775
  • 19
  • 27