Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Get relative Path of a file C#

I currently writing a project in visual studio in c#. the project full path is:

"C:\TFS\MySolution\"

I have a file that I need to load during the execution. lets say the file path is

"C:\TFS\MySolution\Project1\NeedtoLoad.xml"

I don't want to write the full path hard coded, and want to get the path in a dynamic way.

I use the following line:

    var path = Directory.GetCurrentDirectory();

The problem that every method that I found and the above code line gets me the following path:

"C:\TFS\MySolution\Project1\bin\Debug"

And what I need is

"C:\TFS\MySolution\Project1\"

so I could concatenate

NeedtoLoad.xml

to the answer.

of course I could do:

path.Substring(0, path.IndexOf("bin\\Debug"));

But it is not that elegant.

like image 222
daniel the man Avatar asked Dec 06 '16 11:12

daniel the man


People also ask

What is relative_path in C++?

path relative_path const; (since C++17) Returns path relative to root-path , that is, a pathname composed of every generic-format component of * this after root-path .

Why can't I run a program on a relative filepath?

The problem is that relative filepath are relative to the current working directory and not relative to the .exe's path. You can easily test that. Open a command window, change to a directory that is not the path where the exe is and run the program by specifiying the full path to the .exe.

How do I generate the relative path of a file?

There are 2 main methods that can be used to generate the relative path of a file in C#, the Path class, and the resources.resx file.

What is the difference between RelativeTo and getfullpath?

relativeTo or path is null. relativeTo or path is effectively empty. Paths are resolved by calling the GetFullPath method before calculating the difference. The method uses the default file path comparison for the current platform ( StringComparison.OrdinalIgnoreCase for Windows and MacOs, StringComparison.Ordinal for Linux.


Video Answer


3 Answers

You can use Directory.GetParent and its Parent member

string path = Directory.GetParent(Directory.GetCurrentDirectory()).Parent.FullName;

Will go two levels up the path tree and return "C:\TFS\MySolution\Project1".

like image 115
Guy Avatar answered Oct 06 '22 01:10

Guy


If the xml is a static part of you project (you don't override it during runtime) than probably the best thing is to include it into your dll.

  • Go to file Properties and make it Embedded Resource
  • Simply load it from dll resources, e.g.

    var asm = Assembly.GetCallingAssembly();
    using (var stream = asm.GetManifestResourceStream(resource))
    {
        var reader = new StreamReader(stream);
        return reader.ReadToEnd();
    }
    
like image 40
mikka Avatar answered Oct 05 '22 23:10

mikka


If that file is part of your project, you should make sure it's copied to your project output when you build.

Choose the file in solution explorer, and go to the properties pane in Visual Studio (F4). Select 'Content' as the 'Build action'. Now, when you build your solution, the file will be copied to the output directory (bin/Debug or bin/Release).

like image 29
gnud Avatar answered Oct 06 '22 00:10

gnud