1

I'm using C# to get the exact path of the specific folder in windows system by giving the folder name. Is their any way to get the folder path by giving the folder name, where the folder name will be unique.

user2477462
  • 117
  • 6
  • 2
    Doesn't this sound like how to find a people in the world by his name? There are many people with duplicated names. – King King Aug 24 '13 at 07:08
  • you mean you want to search through a drive and return the full path where folder name matches? – Ehsan Aug 24 '13 at 07:09
  • @KingKing - and even if you know the name is unique, in worst case, you have to go through 7 billion people to find the right one... – Corak Aug 24 '13 at 07:13
  • @Corak Not really, we should find in the country first, then city, then street, then house, ... That would be fast. – King King Aug 24 '13 at 07:14
  • @KingKing - yes, but you only know the name, so you need to go through every country and every city and every street and every house. If you're lucky, the person is in the first house. But it could also be the very last house. – Corak Aug 24 '13 at 07:19
  • Folder is created at run time with current time as the name. This process is done by the application. Here i know the folder name but i didn't know path, because path is selected by the user during installation and installation is done before very long time. – user2477462 Aug 24 '13 at 07:40
  • Wait... the application knows where to create the folder?! Then it should also know exactly where it is! Or am I missing something? – Corak Aug 24 '13 at 07:45
  • The problem is application doesn't store the full path.. its depends on the user.. – user2477462 Aug 24 '13 at 07:54
  • @user2477462 I upvoted your question, but can you edit it to make it clearer. Look at my update and let me know whats wrong with `Application.StartUpPath` – Jeremy Thompson Aug 24 '13 at 07:57
  • @user2477462 - Yes, but where does the application create the folders? If you don't specify it, as Jeremy said, they're probably in the `Application.StartUpPath`. – Corak Aug 24 '13 at 08:18

3 Answers3

1

Update:

Folder is created at run time with current time as the name. This process is done by the application. Here i know the folder name but i didn't know path, because path is selected by the user during installation and installation is done before very long time.

That changes the question considerably. Why not use the application to tell you where it lives:

http://msdn.microsoft.com/en-us/library/system.windows.forms.application.startuppath.aspx


I had a similar idea ages ago and wrote about it as a Code Project Tip:

http://www.codeproject.com/Tips/132804/Open-folders-using-a-Run-Command

Otherwise you would need to index every folder on the PC and make them unique names and look up the full path that way.

The other suggestion I have is using LogParser as the Most efficient way to find all exe files on disk using C#? Its a free Microsoft product but I'm not sure about re-dist permissions, I had to include it in my package separately last time I used it. It full on flys, faster than a speeding train!

I found a Log Parser example that finds folders, you could try it out and adapt it if its useful:

SELECT TOP 1 * FROM C:\TFS\Project\*.* WHERE INDEX_OF(Path, 'Database') > 0

The good folks over at http://visuallogparser.codeplex.com/ have provided us with the source code.

Open the VisualLogParser solution in VS2010, ignore the prompt about debugging, after the solution loads, F5, set the combo-box to FS (FileSystem), paste in this query and press go.

Community
  • 1
  • 1
Jeremy Thompson
  • 61,933
  • 36
  • 195
  • 321
0

See following link

string dirName = new DirectoryInfo(@"c:\projects\roott\wsdlproj\devlop\beta2\text").Name;
Soner Gönül
  • 97,193
  • 102
  • 206
  • 364
Sharad
  • 743
  • 2
  • 14
  • 23
  • 2
    Pretty sure OP wants to do the opposite, plug in the folder name and get the full path. Obviously only possible where all folder names are unique. – Jeremy Thompson Aug 24 '13 at 07:18
0

You could probably use something like this, but it'll be rather slow, depending on how many folders needed to be looked through.

Use it like FindFullPath(rootFolder, folderNameToLookFor)

public static string FindFullPath(string path, string folderName)
{
    if (string.IsNullOrWhiteSpace(folderName) || !Directory.Exists(path))
    {
        return null;
    }
    var di = new DirectoryInfo(path);
    return findFullPath(di, folderName);
}
private static string findFullPath(DirectoryInfo directoryInfo, string folderName)
{
    if (folderName.Equals(directoryInfo.Name, StringComparison.InvariantCultureIgnoreCase))
    {
        return directoryInfo.FullName;
    }
    try
    {
        var subDirs = directoryInfo.GetDirectories();
        return subDirs.Select(subDir => findFullPath(subDir, folderName)).FirstOrDefault(fullPath => fullPath != null);
    }
    catch
    {
        // DirectoryNotFound, Security, UnauthorizedAccess
        return null;
    }
}
Corak
  • 2,688
  • 2
  • 21
  • 26