2

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

demonplus
  • 5,613
  • 12
  • 49
  • 68
Sangeetha
  • 21
  • 1
  • 2
  • 6

2 Answers2

6

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");
        }
    }
}
Sir JokesALot
  • 214
  • 2
  • 5
  • var projectFolder = Directory.GetParent(Directory.GetCurrentDirectory()).Parent.Parent.FullName; took me 2 x Parent() but works – Elvis Skensberg Dec 12 '21 at 10:27
4

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;
Guy
  • 46,488
  • 10
  • 44
  • 88