Streamreader to a relative filepath

79,528

Solution 1

Is the Folder "TestDir" always in the executable directory? if so, try this

    string dir =System.IO.Path.GetDirectoryName(
      System.Reflection.Assembly.GetExecutingAssembly().Location);

    string file = dir + @"\TestDir\TestFile.txt";

This will give you the path of the exe plus the folder inside it and the textfile

Solution 2

You can use the GetFullPath() method. Try this:

 string filePath = System.IO.Path.GetFullPath("TestFile.txt");
 StreamReader sr = new StreamReader(filePath);

Solution 3

A few things:

First, FileInfo.FullName gives the absolute path for the file, so you don't need to prepend the full directory path before the file in the StreamReader instance.

Second, FileInfo file = new FileInfo(TestFile.txt); should fail unless you actually have a class called TestFile with a txt property.

Finally, with almost every File method, they use relative paths already. So you SHOULD be able to use the stream reader on JUST the relative path.

Give those few things a try and let us know.

Edit: Here's what you should try:

FileInfo file = new FileInfo("TestFile.txt");
StreamReader sr = new StreamReader(fullFile.FullName);
//OR
StreamReader sr = new StreamReader("TestFile.txt");

However, one thing I noticed is that the TestFile is located in TestDir. If your executable is located in ProgDir as you're stating, then this will still fail because your relative path isn't right.

Try changing it to TestDir\TestFile.txt instead. IE: StreamReader sr = new StreamReader("TestDir\TestFile.txt");

Solution 4

The FileInfo constructor takes a single parameter of type string. Try putting quotes around TestFile.txt.

Change

FileInfo file = new FileInfo(TestFile.txt);

to

FileInfo file = new FileInfo("TestFile.txt");

Unless TestFile is an object with a property named txt of type string, in which case you have to create the object before trying to use it.

Solution 5

I had a similar issue and resolved it by using this method:

StreamReader sr = File.OpenText(MapPath("~/your_path/filename.txt"))

This could be a good option if you need a more relative path to the file for working with different environments.

Share:
79,528
gfppaste
Author by

gfppaste

I'm a computer Science student at Rutgers University. I finished my bachelor's in Cell Biology, realized I hated it, and then realized I love computers.

Updated on October 03, 2020

Comments

  • gfppaste
    gfppaste over 3 years

    I was wondering, if anyone could tell me how to point a StreamReader to a file inside the current working directory of the program.

    E.g.: say I have program Prog saved in the directory "C:\ProgDir\". I commit "\ProgDir" to a shared folder. Inside ProgDir is another directory containing files I'd like to import into Prog (e.g. "\ProgDir\TestDir\TestFile.txt") I'd like to make it so that the StreamReader could read those TestFiles, even when the path to the directory has changed;

    (E.G., on my computer, the path to the Testfiles is

    C:\ProgDir\TestDir\TestFile.txt

    but on the other person's computer, the directory is

    C:\dev_code\ProgDir\TestDir\TestFile.txt

    ).

    How would I get a StreamReader to be ale to read from TestFile.txt on the other person's computer? (to clarify, the filenames do not change, the only change is the path ProgDir)

    I tried the following:

    string currentDir = Environment.CurrentDirectory;
    DirectoryInfo directory = new DirectoryInfo(currentDir);
    FileInfo file = new FileInfo("TestFile.txt");
    
    string fullDirectory = directory.FullName;
    string fullFile = file.FullName;
    
    StreamReader sr = new StreamReader(@fullDirectory + fullFile);
    

    ( pulled this from : Getting path relative to the current working directory?)

    But I'm getting "TestFile does not exist in the current context". Anyone have any idea as to how I should approach this?

    Thank you.

  • gfppaste
    gfppaste almost 12 years
    It's breaking at Streamreader sr = new StreamReader(filePath);
  • Zenadix
    Zenadix over 9 years
    @gfppaste That was a typo. It's StreamReader instead of Streamreader.
  • Andreas
    Andreas almost 5 years
    although correct, there is an easier way. The current directory is always a "." - so just use ".\TestDir\TestFile.txt" (see my answer)
  • Michael Fulton
    Michael Fulton about 2 years
    You can also do new StreamReader(@".\TestDir\filename")