954,568 Members — Technology Publication meets Social Media
Username:
Password:
Lost login information?
Have something to say? Contribute New Article Reply to this Article

VB.NET - Using streamreader with relative path.

I am making a VB.NET web application in VS.NET 2003.

I am using streamreader to access a text file for reading. I am able to access it without any problems using the full path (ie. C:\Inetpub\wwwroot\\bin\file.txt). But I want to use a relative path (ie. .\file.txt).

If I use something like:

Dim sr As System.IO.StreamReader
sr = New StreamReader(".\file.txt") 'or just "file.txt"

I get the following error:

Could not find file "c:\windows\system32\inetsrv\file.txt".
Source Error:
Line 62: sr = New StreamReader(".\file.txt")


Why does it automatically default to the directory "c:\windows\system32\inetsrv\"?
What should I do to change this?

Tengu
Newbie Poster
2 posts since Nov 2004
Reputation Points: 10
Solved Threads: 0
 

I am making a VB.NET web application in VS.NET 2003.

I am using streamreader to access a text file for reading. I am able to access it without any problems using the full path (ie. C:\Inetpub\wwwroot\\bin\file.txt). But I want to use a relative path (ie. .\file.txt).

If I use something like:

Dim sr As System.IO.StreamReader sr = New StreamReader(".\file.txt") 'or just "file.txt"

I get the following error:

Could not find file "c:\windows\system32\inetsrv\file.txt". Source Error: Line 62: sr = New StreamReader(".\file.txt")

Why does it automatically default to the directory "c:\windows\system32\inetsrv\"? What should I do to change this?

You're better off asking this in the .NET forum, and I've only dabbled in .NET. But here's one way to do it:

sr = New StreamReader(System.Windows.Forms.Application.ExecutablePath & "\file.txt")


I don't have VB.NET on this PC so I can't test it. I'm pretty sure that's how you would do it though :mrgreen:

Try that.

Mark

mnemtsas
Posting Whiz in Training
200 posts since Jul 2004
Reputation Points: 16
Solved Threads: 1
 

I am making a VB.NET web application in VS.NET 2003.

I am using streamreader to access a text file for reading. I am able to access it without any problems using the full path (ie. C:\Inetpub\wwwroot\\bin\file.txt). But I want to use a relative path (ie. .\file.txt).

If I use something like:

Dim sr As System.IO.StreamReader sr = New StreamReader(".\file.txt") 'or just "file.txt"

I get the following error:

Could not find file "c:\windows\system32\inetsrv\file.txt". Source Error: Line 62: sr = New StreamReader(".\file.txt")

Why does it automatically default to the directory "c:\windows\system32\inetsrv\"? What should I do to change this?

You're better off asking this in the .NET forum, and I've only dabbled in .NET. But here's one way to do it:

sr = New StreamReader(System.AppDomain.CurrentDomain.BaseDirectory & "file.txt")


Try that.

Mark

mnemtsas
Posting Whiz in Training
200 posts since Jul 2004
Reputation Points: 16
Solved Threads: 1
 

Double Post

mnemtsas
Posting Whiz in Training
200 posts since Jul 2004
Reputation Points: 16
Solved Threads: 1
 

Thanks for the help. Second one did the trick.

sr = New StreamReader(System.AppDomain.CurrentDomain.BaseDirectory & "bin\file.txt")

Tengu
Newbie Poster
2 posts since Nov 2004
Reputation Points: 10
Solved Threads: 0
 

This article has been dead for over three months

Post: Markdown Syntax: Formatting Help
You