-3

I've a part of a string path: "\MVVM\MyFirstTest2016\MyFirstTest\bin\Debug\MyFirstTest.exe"

I want to search the above path in C: and need to get the complete full path of the directory.

I tried with Path.GetFullPath("") and other in-built methods but didnt get complete full path.

venkat
  • 5,648
  • 16
  • 58
  • 83
  • Possible duplicate of [How do I get the path of the assembly the code is in?](http://stackoverflow.com/questions/52797/how-do-i-get-the-path-of-the-assembly-the-code-is-in) – haddow64 Jan 13 '16 at 08:27
  • No...Not from Assembly. The above part of string resides somewhere in the C Drive. Now i want to identify the same and need to retrive the complete full path using that part of string path. – venkat Jan 13 '16 at 08:27
  • `path = "C:" + path;` ? – Gerald Schneider Jan 13 '16 at 08:28
  • Are you using a web application to get the path ? try for web .. Request.Url.AbsoluteUri – Muks Jan 13 '16 at 08:30
  • No...I'm using WPF application. Not Web – venkat Jan 13 '16 at 08:30
  • @sukumar If there is `C:\\Folder1\\MVVM\MyFirstTest2016\MyFirstTest\bin\Debug\MyFirstTest.exe` and `C:\\Folder2\\MVVM\MyFirstTest2016\MyFirstTest\bin\Debug\MyFirstTest.exe` which one do you want to get ? – tchelidze Jan 13 '16 at 08:31
  • that path is resided in only one folder. I do not have duplicate folders. incase if multiple folders exists then I want the first result from the search results. – venkat Jan 13 '16 at 08:32
  • @sukumar What you mean by `first one` ? – tchelidze Jan 13 '16 at 08:33
  • First search result. C:\\Folder1\\MVVM\MyFirstTest2016\MyFirstTest\bin\Debug\MyFirstTest.exe – venkat Jan 13 '16 at 08:34
  • @sukumar did you check this reference https://support.microsoft.com/en-us/kb/303974 – mr. Holiday Jan 13 '16 at 08:38

3 Answers3

0

Here's the code: The sourceDir will be the full path.

 string defaultFolder = System.AppDomain.CurrentDomain.BaseDirectory;
    string navigateToFolder = "\\MVVM\\MyFirstTest2016\\MyFirstTest\\bin\\Debug\\MyFirstTest.exe";
    string sourceDir = Path.Combine(defaultFolder, navigateToFolder);
Muks
  • 134
  • 9
  • That's assuming the supplied path is relative to the current process which it may not be –  Jan 13 '16 at 08:41
  • Agree with Micky on this. Path should be under the same domain. – Muks Jan 13 '16 at 08:48
0

It sounds like your problem is similar to this question. You've got a partial path, but no idea where it is on this drive. The naive approach would be as follows.

You're first step would be to start at the root of the drive, and get the list of directories:

string[] dirs = Directory.GetDirectories(@"c:\", "*");

You'd then check if any of these strings matched the first directory of your root path (MVVM). If it does, you'd go into that folder and check if it contained the next directory. If it does, check the next and next, etc. until you've exhausted the path.

If not, you'd iterate over the directories, and run the same logic: get the directories, check if any match your first folder, etc.

So a bit of pseudo code would look like:

string directoryPath = "\MVVM\MyFirstTest2016\MyFirstTest\bin\Debug\MyFirstTest.exe"
string[] splitPath = directoryPath.split("\")
check("c:\")

public void check(string directory)
    string[] directories = Directory.GetDirectories(@directory, "*")
    if(checkDirectories(directories, splitPath))
        // Success!
    else 
        for(string subDirectory : directories)
            string newDirectory = Path.combine(directory, subDirectory)
            check(newDirectory)

public boolean checkDirectories(string[] directories, string[] splitPath)
    // Horrible, but just for example - finding the file at the end
    if(splitPath.size == 1)
        // Get file list in current directory and check the last part of splitPath
    if(directories.contains(splitPath[0])
        // Recursively call checkDirectories with the sub directories of this folder, an splitPath missing the first item. This can be done using Array.Copy(splitPath, 1, newArray, 0)

Obviously that's nowhere near runnable, but it should give you the basic idea. The other question I linked earlier also has an accepted answer which will help more.

Community
  • 1
  • 1
Kenco
  • 683
  • 9
  • 21
0

You could iterate over all directories and check if the sub directory is available.

The normal Directory.EnumerateDirectories may throw a UnauthorizedAccessException which stops the process of finding the directory.

So you could write your own EnumerateDirectories as shown below.

The example provided will return the folders found.

void Main()
{
    string path = @"temp\A\B";
    var parts = path.Split(new [] { Path.DirectorySeparatorChar });
    var rootPath = "c:\\";

    foreach(var result in DirectoryEx.EnumerateDirectories(rootPath, parts.First()))
    {
        var checkPath = Path.Combine(result, String.Join(""+Path.DirectorySeparatorChar, parts.Skip(1).ToArray()));
        if (Directory.Exists(checkPath))
            Console.WriteLine("Found : " + checkPath);
    }
}

public class DirectoryEx
{
    public static IEnumerable<string> EnumerateDirectories(string dir, string name)
    {
        IEnumerable<string>  dirs;

        // yield return may not be used in try with catch.
        try { dirs = Directory.GetDirectories(dir); } 
        catch(UnauthorizedAccessException) { yield break; }

        foreach (var subdir in dirs)
        {
            if(Path.GetFileName(subdir).Equals(name, StringComparison.OrdinalIgnoreCase))
                yield return subdir;

            foreach(var heir in EnumerateDirectories(subdir, name))
                yield return heir;
        }
    }
}

In my case results in :

Found : c:\dev\temp\A\B
Found : c:\Temp\A\B
Dbuggy
  • 901
  • 8
  • 16