6

I am building a Windows phone 8 app.

At design time I load a sample XML file to get sample data. It works well but I want to use path to file that is relative to solution root so it can work for all developpers with the same code.

Here is my current code:

var path = @"C:\Users\Tom\MyProject\SampleData\stub.xml";
xml = new StreamReader(path).ReadToEnd();

I tried a relative path like @"SampleData\stub.xml". It works on phone but at design time I get this error:

DirectoryNotFoundException: Could not find a part of the path 'C:\Program Files (x86)\Microsoft Visual Studio 11.0\Common7\IDE\SampleData\login.xml'.

Uwe Keim
  • 39,551
  • 56
  • 175
  • 291
Tom Esterez
  • 21,567
  • 8
  • 39
  • 44
  • 1
    The relative path has to have some base to relate to. I guess it is your working direcotry. ;-) Include the file in your project and set action: copy if newer – Pavel Voronin Nov 09 '12 at 15:09
  • This isn't exactly the same question, but basically you need to use one of the solutions from this question I think: http://stackoverflow.com/questions/2425944/how-to-get-the-project-path-at-design-time – Dana Cartwright Nov 09 '12 at 18:14
  • Thanks Dana, here is the only solution tha worked for me: http://stackoverflow.com/questions/723172/project-bin-folder-path-at-compile-time – Tom Esterez Nov 12 '12 at 09:53

1 Answers1

3

Here is what I did :

var path = "SampleData/stub.xml";
var res = Application.GetResourceStream(new Uri(path, UriKind.Relative));
xml = new StreamReader(res.Stream).ReadToEnd();
Dariusz Woźniak
  • 9,640
  • 6
  • 60
  • 73
Tom Esterez
  • 21,567
  • 8
  • 39
  • 44