0

I'm building an executable application from WPF in visual studio, and I have a local text file added to the project (StringList.txt) that I'm trying to read into a List object. What would the path for that file be for a StreamReader such that the executable can access the file when I deploy it and run in on another computer?

So far I have:

   List<String> list = new List<String>();

   StreamReader sr = new StreamReader("~/Desktop/Deploy/StringList.txt");

    String line = sr.ReadLine();
    while (line != null)
    {
        fields.Add(line);
        line = sr.ReadLine();
    }

    sr.Close();

**Deploy is the publish folder location

EliteZalba
  • 55
  • 3
  • 11
  • I think that person was trying to save a file to the project folder or something, but I'm just trying to access the local file within my project. Whatever that answer was didn't work. Thanks though. – EliteZalba Jun 24 '15 at 15:32
  • These are the settings I needed to change to get it to work: [Solution][1] [1]: http://stackoverflow.com/questions/6416564/how-to-read-a-text-file-in-projects-root-directory – EliteZalba Jun 25 '15 at 18:16

1 Answers1

1

The ~/Desktop/Deploy/StringList.txt wont work for WPF.. Try this:

// Get the directory where your entry assembly is located (your application)
var assemblyPath = Path.GetDirectoryName(Assembly.GetEntryAssembly().Location);

// Add your sub directory and filename
var filename = assemblyPath + "\\Desktop\\Deploy\\StringList.txt";

// Use it on your StreamReader
StreamReader sr = new StreamReader(filename);

This will open the file within a subdirectory of your program.

Jeroen van Langen
  • 21,446
  • 3
  • 42
  • 57