I'm after some advice really.

I wish to create an application for myself and a friend using DropBox as a means to store and sync a CSV with data in.

Is it possible to programatically search through a computers directory to locate folder called 'Dropbox' without hardcoding its location?

If so how would I go about this?

Thanks in advance,

Have managed to do it using the following:

            string[] foldersFound = Directory.GetDirectories(@"C:\Users");

            foreach (string folder in foldersFound)
            {
                try
                {
                    string[] subFoldersFound = Directory.GetDirectories(folder);

                    foreach (string subfolder in subFoldersFound)
                    {
                        if (subfolder.Contains("\\Dropbox"))
                            Console.WriteLine(subfolder);
                    }
                }
                catch (UnauthorizedAccessException ex) { }
            }

Hi Mikey,

just spotted your code snippet and thought that it would be really handy for something I am working on myself.

Only thing that I did notice is that you have set it to search for a subfolder inside of the 'users' folder, which is perfectly OK if your end user is using Vista or Win 7 OS but not good otherwise.

I currently use XP OS, so I suppose I could change the folder to 'Documents and Settings' but again this would be limiting unless I knew that my end user was using XP.

Does it work with just '@"C:\"' as the variable?

Regards..,

Sorry for the delay in reply, this is off my phone so can't code.

You can Do this but will need another foreach loop between c the inner loop looking for documents and settings or users.

This would make it os proof :)

Think the code can be improved so will try tomorrow

If you are looking in a special folder you can use the SpecialFolder enumerator to make sure you get the path right everytime:

Environment.GetFolderPath(Environment.SpecialFolder.MyDocuments)

There's quite a few other special folders to note that are represented by that enumerator.

Edited 4 Years Ago by skatamatic

Wow thats really useful cheers skata.

In this case it is not applicable as im looking at the folder heirachy one up from 'My Documents' but will definately remember that one for the future.

@MadTogger,

Updated the code if your interested.

        string[] topFoldersFound = Directory.GetDirectories(@"C:\");
        string DropboxPath = "null";

        foreach (string topFolder in topFoldersFound)
        {
            try
            {
                if (topFolder.Contains("Users") | topFolder.Contains("Documents and Settings"))
                {
                    string[] folderFound = Directory.GetDirectories(topFolder, Environment.UserName);

                    if (folderFound.Length > 0)
                    {
                        string[] subFoldersFound = Directory.GetDirectories(folderFound[0]);

                        foreach (string subFolder in subFoldersFound)
                        {
                            if (subFolder.Contains("Dropbox"))
                                DropboxPath = subFolder;
                        }
                    }

                }
            }
            catch (UnauthorizedAccessException ex) { }
        }

The reasons behind this being I figured if the computer has multiple user accounts and the user running the program is say second on the list and the first also had dropbox installed it would take their path, same for users after my target one.

The new code also allows it to work on either OS without difficulty and also removes the middle tier of looping by using a search pattern of the user's username while looking in either the Documents and Settings folder or the Users folder.

Edited 4 Years Ago by Mike Askew

This question has already been answered. Start a new discussion instead.