简体   繁体   中英

How to get the folder path using folder name c#

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.

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.

以下链接

string dirName = new DirectoryInfo(@"c:\projects\roott\wsdlproj\devlop\beta2\text").Name;

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;
    }
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM