如何使用文件夹名称c#获取文件夹路径

时间:2013-08-24 07:07:12

标签: c# window

我正在使用C#通过提供文件夹名称来获取Windows系统中特定文件夹的确切路径。他们是否可以通过提供文件夹名称来获取文件夹路径,其中文件夹名称将是唯一的。

3 个答案:

答案 0 :(得分:1)

更新

  

在运行时创建文件夹,并以当前时间作为名称。这个   过程由应用程序完成。在这里,我知道文件夹名称,但我   不知道路径,因为路径是由用户选择的   安装和安装在很长时间之前完成。

这大大改变了这个问题。为什么不使用该应用程序来告诉您它的位置:

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


我很久以前就有类似的想法,并将其作为代码项目提示写道:

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

否则你需要为PC上的每个文件夹编制索引,并使它们成为唯一的名称,并以这种方式查找完整路径。

我的其他建议是使用LogParser作为Most efficient way to find all exe files on disk using C#?它是免费的Microsoft产品,但我不确定re-dist权限,我不得不在上次使用它时将它单独包含在我的包中。它飞得很快,比高速列车快!

我找到了一个找到文件夹的Log Parser example,如果它有用,你可以尝试一下并进行调整:

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

http://visuallogparser.codeplex.com/的优秀人才   向我们提供了源代码。

在VS2010中打开VisualLogParser解决方案,忽略有关调试的提示,解决方案加载后,F5,将组合框设置为FS(FileSystem),粘贴此查询并按go。

答案 1 :(得分:0)

请参阅following link

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

答案 2 :(得分:0)

你可能会使用这样的东西,但它会相当慢,这取决于需要查看多少个文件夹。

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