Sangeetha
Sangeetha

Reputation: 21

How to get relative path of file which is in project folder?

I tried using @"~\TestData\Test.xml", it is trying to get file from bin/debug instead of the project folder.

Upvotes: 2

Views: 18101

Answers (2)

Sir JokesALot
Sir JokesALot

Reputation: 214

You can achieve this by getting the parent of the current directory, using the Directory class in System.IO namespace. See code example.

using System.IO;

namespace ConsoleApplication15
{
    class Program
    {
        static void Main(string[] args)
        {
            var projectFolder = Directory.GetParent(Directory.GetCurrentDirectory()).Parent.FullName;
            var file = Path.Combine(projectFolder, @"TestData\Test.xml");
        }
    }
}

Upvotes: 6

Guy
Guy

Reputation: 51009

Go to Solution explorer -> right click on TestData folder and choose "Add New Item"

Choose "Resources file", rename it to Resource and click "Add".

enter image description here

You will get this screen (It can also be opened by double click on "Resource.resx in the solution explorer) enter image description here

Give name to the file and put the relative path in the value (may require one level up or down). To get the path you can now use

string path = Resource.XMLFile;

Upvotes: 4

Related Questions