Search code examples
c#streamreader

Want to find path of a file from the application folder?


I want to find the path of the file or to access that file which is lying with the .xaml file and the .cs file.

I have my file name.tx in

C:\Users\Itsme\Documents\Visual Studio 2010\Projects\tsa\tsa\name.txt

Now i need to access this file using StreamReader. How could i access this file without providing the complete path.

Thanks.


Solution

  • Since you want to access file which is lying in the same Directory and I am asumming you are using WPF you can use

      string _myDirectoryPath = Environment.CurrentDirectory.ToString().Substring(0, Environment.CurrentDirectory.ToString().Length - 10).ToString() + @"\Myfile.txt";
      StreamReader sr = new StreamReader(_myDirectoryPath);
    

    Update:

    I am not sure if other's will agree to this but I found a working solution.