简体   繁体   中英

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.

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.

I'm not sure what the question is, but if you're just tired of typing "C:\\Users\\Itsme\\Documents\\Visual Studio 2010\\Projects\\tsa\\tsa\\name.txt" all the time, try something like this.

    const string locationOfTextFiles = "C:\\Users\\Itsme\\Documents\\Visual Studio 2010\\Projects\\tsa\\tsa\\";

        var names = new string[]{ "nameOne", "nameTwo", "nameThree"};

        foreach(string name in names)
        {
             using (StreamReader sr = new StreamReader(locationOfTextFiles + name + ".txt"))
                {
                    String line = sr.ReadToEnd();
                }
        }

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM