Streamreader to a relative filepath
Asked Answered
B

8

13

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.

Bred answered 16/5, 2012 at 17:31 Comment(2)
Your title looks entirely unrelated to your question... where does Stopwatch come in?Stockdale
Sorry about that, I think the title carried over from my last question somehow.Bred
A
17

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

Ashworth answered 16/5, 2012 at 17:43 Comment(1)
although correct, there is an easier way. The current directory is always a "." - so just use ".\TestDir\TestFile.txt" (see my answer)Odie
M
10

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

 string filePath = System.IO.Path.GetFullPath("TestFile.txt");
 StreamReader sr = new StreamReader(filePath);
Michikomichon answered 16/5, 2012 at 17:37 Comment(2)
It's breaking at Streamreader sr = new StreamReader(filePath);Bred
@Bred That was a typo. It's StreamReader instead of Streamreader.Immoralist
M
6

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");

Meta answered 16/5, 2012 at 17:44 Comment(0)
B
3

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.

Betrothed answered 16/5, 2012 at 17:42 Comment(0)
O
3

The easiest way would be to just use the file name (not the full path) and "TestDir" and give the StreamReader a relative path.

var relativePath = Path.Combine(".","TestDir",fileName);
using (var sr = new StreamReader(relativePath))
{
   //...
}
Odie answered 19/7, 2019 at 7:39 Comment(2)
You can also do new StreamReader(@".\TestDir\filename")Haworth
yes you could, and that is also, what comes out of Path.Combine. Or you use $".\\TestDir\\{fileName}" to create a relative pathOdie
M
1

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.

Mincing answered 2/10, 2020 at 20:2 Comment(0)
T
0

You can use path.combine to get the current directory to build and then combine the file path you need

 new StreamReader(Path.Combine(Environment.CurrentDirectory, "storage"));
Thuthucydides answered 19/7, 2019 at 7:19 Comment(0)
A
0
System.IO.Path.GetDirectoryName(new System.Uri(System.Reflection.Assembly.GetExecutingAssembly().CodeBase).LocalPath)
Acrimonious answered 29/7, 2022 at 9:11 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.