I tried using @"~\TestData\Test.xml"
, it is trying to get file from bin/debug
instead of the project folder.
Asked
Active
Viewed 1.7k times
2
-
in which language you want it.in java or c#? – Sudharsan Selvaraj Aug 29 '16 at 07:00
-
I fixed it by setting property of the xml file to "Copy Always".. – Sangeetha Sep 01 '16 at 09:56
2 Answers
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".
You will get this screen (It can also be opened by double click on "Resource.resx in the solution explorer)
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