3

Is there a short way to get certain parent directory path from current path.

I have a path like this:

"c:\\users\\userName\\documents\\visual studio 2013\\Projects\\SolutionName\\ProjectName\\bin\\Debug"

and i want to get the path to 'SolutionName' directory, like this

"c:\\users\\userName\\documents\\visual studio 2013\\Projects\\SolutionName"

I need it because i am storing some data in folder

"c:\\users\\userName\\documents\\visual studio 2013\\Projects\\SolutionName\\DataFolder"

and i need to access it from different projects in my solution.

Thank you all, there is what i finished with

public static string GetParent( string parentName,string FileName)
    {
        var dir = new DirectoryInfo(System.IO.Directory.GetCurrentDirectory());

        while (dir.Parent.Name != parentName)
        {
            dir = dir.Parent;
        }
        return dir.Parent.FullName+"\\Data\\"+FileName;
    }

I accepted Callum Bradbury answer because i just changed it not to be recursive.

Alex Tertyshnyk
  • 71
  • 3
  • 10
  • 1
    If you're just trying to access the folder (and not displaying the path), you can simply append ".." to the full path: "c:\\users\\userName\\documents\\visual studio 2013\\Projects\\SolutionName\\DataFolder\\..\\" – Kevin Gosse Apr 17 '15 at 09:27

5 Answers5

5

You can use the DirectoryInfo class to do this relatively easily with a little iterative function.

    public string GetParent(string path, string parentName)
    {
        var dir = new DirectoryInfo(path);

        if (dir.Parent == null)
        {
            return null;
        }

        if (dir.Parent.Name == parentName)
        {
            return dir.Parent.FullName;
        }

        return this.GetParent(dir.Parent.FullName, parentName);
    }
Callum Bradbury
  • 936
  • 7
  • 14
  • Thank you, this is what i was looking for, but i changed it so it won't be recursive. – Alex Tertyshnyk Apr 18 '15 at 21:31
  • With your changes if the parentName isn't a parent directory of the current directory, you'll get a null reference exception. Whether that's an issue or not is up to you though. Glad I could help – Callum Bradbury Apr 19 '15 at 10:06
1

if you want a clean solution follow this steps

  1. in every project of your solution try to change your Build Output Path

    enter image description here

  2. you have to make all your paths relative something like this

     "..\\datafolder\File.txt"
    
Community
  • 1
  • 1
BRAHIM Kamel
  • 13,492
  • 1
  • 36
  • 47
1

what you are looking for is:

var curd = Environment.CurrentDirectory; 
// this will give you current directory for eg: if you are in "Debug" mode then "c:\users\userName\documents\visual studio 2013\Projects\SolutionName\ProjectName\bin\Debug" and if you are in "Release" mode then "c:\users\userName\documents\visual studio 2013\Projects\SolutionName\ProjectName\bin\Release"

now what you want is your path name until SolutionName folder which is 3 level up, so all you got to do is append to the above line @"\..\..\..\"

i.e.

var curd = Environment.CurrentDirectory + @"\..\..\..\";

and you are done.

Key the number of level you want to go up from current directory the same number of @"\..\" you need to append.

manish
  • 1,450
  • 8
  • 13
0

Here are a few solutions :

string dataFolder = Directory.GetCurrentDirectory() + "..\\..\\..\\DataFolder"

string dataFolder = Path.GetDirectoryName(Path.GetDirectoryName(System.IO.Directory.GetCurrentDirectory())) + "\\DataFolder";

You can also see this link for the best approach to find your current path, it seems like GetCurrentDirectory might not be the best solution : get path for my .exe

Community
  • 1
  • 1
JP Tétreault
  • 600
  • 5
  • 15
  • I must access same folder from different projects which can be in different folders but all will be placed inside "SolutionFolder", but they can be on different levels. – Alex Tertyshnyk Apr 17 '15 at 10:59
0

Create a reg key with path you need

Microsoft.Win32.RegistryKey key;
key = Microsoft.Win32.Registry.CurrentUser.CreateSubKey("Names");
key.SetValue("Name", "Isabella");
key.Close();

When created, you can use RegistryKey class under Microsoft.Win32 namespace

Cesar BA
  • 146
  • 1
  • 5