kmoloney
asked on
Referencing a file in VS2005 file system web app - bin\debug and bin\release
I'm using Visual Studio 2005 to build web apps.
When I create a file system web.app, references to a given file (e.g., "~/APP_DATA/Datafile.xml") can't seem to find it, but when I build and deploy it, it does.
I'm guessing this has something to do with the additional directory levels ("Bin\Debug" or "Bin\Release") that are added to the project when testing it on the Virtual Server. So I added a bunch of code along the lines of:
dim f as new file
if file.exists(Directory.Curr entDirecto ry & "APP_DATA/Datafile.xml") then
f=Directory.CurrentDirecto ry & "APP_DATA/Datafile.xml")
Else
f=Directory.CurrentDirecto ry & "../../APP_DATA/Datafile.x ml")
End If
There's got to be a better way. In fact, I don't even know why this works?...
Any help?
Kevin
When I create a file system web.app, references to a given file (e.g., "~/APP_DATA/Datafile.xml")
I'm guessing this has something to do with the additional directory levels ("Bin\Debug" or "Bin\Release") that are added to the project when testing it on the Virtual Server. So I added a bunch of code along the lines of:
dim f as new file
if file.exists(Directory.Curr
f=Directory.CurrentDirecto
Else
f=Directory.CurrentDirecto
End If
There's got to be a better way. In fact, I don't even know why this works?...
Any help?
Kevin
Since I don't use the Web Project model for web sites, I haven't had this problem. I would have thought that the root indicator '~' would have been sufficient. It is usually the case that the folder is relative to the page location.
Are you using master pages? They will rebase the folder references to the location of the master page.
Bob
Are you using master pages? They will rebase the folder references to the location of the master page.
Bob
ASKER
Example:
I have a web application saved in C:\Documents and Settings\kmoloney\My Documents\Visual Studio 2005\Projects\DeleteMe2.
In this project, I have:
- an App_Data folder, that has one text file, "TextFile.txt" - it has a few lines of miscellaneous words and phrases.
- A Default.aspx page in the root.
- web.config file in the root, which I only modified to allow debugging.
On the default.aspx page, I have a button control (btnReadFile) and a list box (lstItems).
The idea is to read the items in the file when the button is clicked, and add them to the listbox control.
Here's my code for btnReadFile_Click (I'm importing system.IO):
Protected Sub btnReadFile_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles btnReadFile.Click
Dim strReader As StreamReader
strReader = File.OpenText("~/App_Data/ TextFile.t xt")
While strReader.Peek <> -1
Me.lstItems.Items.Add(strR eader.Read Line())
End While
End Sub
The error message I receive is:
Could not find a part of the path 'C:\Program Files\Microsoft Visual Studio 8\Common7\IDE\~\App_Data\T extFile.tx t'.
So I tried using Directory.GetCurrentDirect ory - here's the modified code.
Protected Sub btnReadFile_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles btnReadFile.Click
Dim strReader As StreamReader
strReader = File.OpenText(Directory.Ge tCurrentDi rectory & "~/App_Data/TextFile.txt")
While strReader.Peek <> -1
Me.lstItems.Items.Add(strR eader.Read Line())
End While
End Sub
And I received the same error message as above.
I have a web application saved in C:\Documents and Settings\kmoloney\My Documents\Visual Studio 2005\Projects\DeleteMe2.
In this project, I have:
- an App_Data folder, that has one text file, "TextFile.txt" - it has a few lines of miscellaneous words and phrases.
- A Default.aspx page in the root.
- web.config file in the root, which I only modified to allow debugging.
On the default.aspx page, I have a button control (btnReadFile) and a list box (lstItems).
The idea is to read the items in the file when the button is clicked, and add them to the listbox control.
Here's my code for btnReadFile_Click (I'm importing system.IO):
Protected Sub btnReadFile_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles btnReadFile.Click
Dim strReader As StreamReader
strReader = File.OpenText("~/App_Data/
While strReader.Peek <> -1
Me.lstItems.Items.Add(strR
End While
End Sub
The error message I receive is:
Could not find a part of the path 'C:\Program Files\Microsoft Visual Studio 8\Common7\IDE\~\App_Data\T
So I tried using Directory.GetCurrentDirect
Protected Sub btnReadFile_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles btnReadFile.Click
Dim strReader As StreamReader
strReader = File.OpenText(Directory.Ge
While strReader.Peek <> -1
Me.lstItems.Items.Add(strR
End While
End Sub
And I received the same error message as above.
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER
Server.MapPath...that works.
I'm not totally understanding the difference, LearnedOne. What's GetCurrentDirectory vs. Server.MapPath (you get the points, by the way -- thank you).
I'm not totally understanding the difference, LearnedOne. What's GetCurrentDirectory vs. Server.MapPath (you get the points, by the way -- thank you).
ASKER