944,101 Members | Top Members by Rank

Ad:
Nov 8th, 2004
0

VB.NET - Using streamreader with relative path.

Expand Post »
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\<projectname>\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?
Similar Threads
Reputation Points: 10
Solved Threads: 0
Newbie Poster
Tengu is offline Offline
2 posts
since Nov 2004
Nov 8th, 2004
0

Re: VB.NET - Using streamreader with relative path.

Quote originally posted by Tengu ...
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\<projectname>\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:

Visual Basic 4 / 5 / 6 Syntax (Toggle Plain Text)
  1. 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
Reputation Points: 16
Solved Threads: 1
Posting Whiz in Training
mnemtsas is offline Offline
200 posts
since Jul 2004
Nov 8th, 2004
0

Re: VB.NET - Using streamreader with relative path.

Quote originally posted by Tengu ...
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\<projectname>\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:

Visual Basic 4 / 5 / 6 Syntax (Toggle Plain Text)
  1. sr = New StreamReader(System.AppDomain.CurrentDomain.BaseDirectory & "file.txt")

Try that.

Mark
Reputation Points: 16
Solved Threads: 1
Posting Whiz in Training
mnemtsas is offline Offline
200 posts
since Jul 2004
Nov 8th, 2004
0

Re: VB.NET - Using streamreader with relative path.

Double Post
Reputation Points: 16
Solved Threads: 1
Posting Whiz in Training
mnemtsas is offline Offline
200 posts
since Jul 2004
Nov 8th, 2004
0

Re: VB.NET - Using streamreader with relative path.

Thanks for the help. Second one did the trick.

sr = New StreamReader(System.AppDomain.CurrentDomain.BaseDirectory & "bin\file.txt")
Reputation Points: 10
Solved Threads: 0
Newbie Poster
Tengu is offline Offline
2 posts
since Nov 2004

This thread is more than three months old

No one has posted to this discussion for at least three months. Please let old threads die and do not reply to them unless you feel you have something new and valuable to contribute that absolutely must be added to make the discussion complete. Otherwise, please start a new thread in this forum instead.
Message:
Previous Thread in Visual Basic 4 / 5 / 6 Forum Timeline: Low Quality in VB when printing with printer.paintpicture
Next Thread in Visual Basic 4 / 5 / 6 Forum Timeline: Nested For Next Loops w/ Triangles





About Us | Contact Us | Advertise | Acceptable Use Policy
Forum Index | Build Custom RSS Feed


Follow us on Twitter


© 2011 DaniWeb® LLC