0

In my project I created a Text file and placed it inside the folder Resources. Is there any way to access that file?

I have a WPF control that access this file and I can't get it to work.

I've already tried to go by project path StreamReader myFile = new StreamReader("C:\ProjectFolder\ProjectName\Resources\file.txt") and this solution there's a conflict.

The first solution let's me, in code mode work normally, but at runtime the app crashes because it can't find the path. Says the path to (Project)\bin\debug\file.txt does not exist. On the other hand the second solution let's me run/degug the app perfectly but when I try to edit the xaml Design of the control that hosts the code, it tells me that cannot find full path to file.

Can anyone help? Thanks a lot

Community
  • 1
  • 1
David
  • 261
  • 1
  • 17

1 Answers1

1

Try this:

Stream myFile = new StreamReader(@"C:\ProjectFolder\ProjectName\Resources\file.txt");

The slashes are interpreted by the compiler as escape sequences, and using the '@' intructs the compiler to take the string literally without honouring the escape sequences.

Jack
  • 1,477
  • 15
  • 20
  • Thanks a lot. It actually works :DDD. But I have a question: When the app is finished and I create the setup so it can be installed on different machines, will this still work??? – David Aug 23 '11 at 18:42
  • @David, I don't think so, but I don't have the solution. Seemingly the path would have to be provided in the app configuration or be made relative. – Jack Aug 23 '11 at 18:44