Search code examples
c#streamreader

Use StreamReader with GetFolderPath()?


I have a line to get the file path of a text file:

string file = Path.Combine(Environment.GetFolderPath
    (Environment.SpecialFolder.ApplicationData), "file.txt");

Which returns:

file = "C:\\Users\\Benjamin\\AppData\\Roaming\\file.txt"

However, when I try and use a StreamReader on it, it returns a FileNotFound exception

StreamReader rdr = new StreamReader(file); // Throws the FileNotFound exception.

Does the fact that the original location is different to the one returned? (Original location: C:\Users\Benjamin\Documents\file.txt)


Solution

  • I'm guessing that C:\Users\Benjamin\AppData\Roaming\file.txt does not exist.

    If you want C:\Users\Benjamin\Documents\file.txt, use Environment.SpecialFolder.MyDocuments instead of Environment.SpecialFolder.ApplicationData