-3

I need to find path of a folder. Let's say folder name is Test. If my application is installed on c:\drive so in that case Test folder can be at any level in c:\drive and the page in which I need path can also be at any level in c:\drive.

thank you for your help.

Andrei
  • 42,814
  • 35
  • 154
  • 218
user1282615
  • 1
  • 2
  • 4

3 Answers3

1

Try this.

static void Main(string[] args)
    {

       string pathToDirctory = @"C:\\";
       string baseDirectory = pathToDirctory;


       string path = SearchAllFolders(pathToDirctory,"Test");


    }

    private static string SearchAllFolders(string path, string search)
    {
        string folderPath = string.Empty;
        try
        {
            if ((File.GetAttributes(path) & FileAttributes.ReparsePoint)
                != FileAttributes.ReparsePoint)
            {
                foreach (string folder in Directory.GetDirectories(path))
                {
                    if (folder.Contains("RECYCLE.BIN"))
                    {
                        continue;
                    }
                    string p = Path.GetFileName(folder);

                    if ( p.Equals(search))
                    {
                        return folder;
                    }
                    else
                    {
                        string f = SearchAllFolders(folder ,search);
                        if (f != null)
                        {
                            return f;
                        }
                    }

                }
            }
        }
        catch (UnauthorizedAccessException) { }

        return null;
    }
Muhammad Umar
  • 3,761
  • 1
  • 24
  • 36
0

You can try to find the folder by name using a recursive function:

http://support.microsoft.com/kb/303974

bit
  • 31
  • 6
0

Try this code as you code in asp.net web application

string baseDirectory = AppDomain.CurrentDomain.BaseDirectory;
foreach (string dir in Directory.GetDirectories(baseDirectory, "Test", SearchOption.AllDirectories)) 
{
    // dir will be a path of "Test" folder.
    // You will get more than one path if it found more than one folder names Test.
}

baseDirectory will store a path of a folder you installed the application.

Alice
  • 1,255
  • 1
  • 9
  • 7
  • This will throw exception `UnAuthorizedAccessException`. – Muhammad Umar Nov 20 '13 at 07:57
  • It's access denie exception that you have to set permission to your folder for allow accessing. – Alice Nov 20 '13 at 08:01
  • Please also learn from here http://technet.microsoft.com/en-us/library/bb727008.aspx – Alice Nov 20 '13 at 08:03
  • According to your solution it will search inside "baseDirectory", but "Test" Folder may also be in parent of baseDirectory or many layer upper than baseDirectory . – user1282615 Nov 22 '13 at 10:14
  • According to your question, I understood that you want to find Test folder path which is in any level of application's installed path, and baseDirectory is the path you mean. Maybe I understood wrong. – Alice Nov 22 '13 at 13:13