Get relative Path of a file C#

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".


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