Link to home
Start Free TrialLog in
Avatar of kmoloney
kmoloneyFlag for United States of America

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.CurrentDirectory & "APP_DATA/Datafile.xml") then
   f=Directory.CurrentDirectory & "APP_DATA/Datafile.xml")
Else
   f=Directory.CurrentDirectory & "../../APP_DATA/Datafile.xml")
End If

There's got to be a better way.  In fact, I don't even know why this works?...

Any help?

Kevin
Avatar of kmoloney
kmoloney
Flag of United States of America image

ASKER

Increased points by 100% to 500 so as to increase thus-far lackadaisical response.
Avatar of Bob Learned
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
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.txt")
        While strReader.Peek <> -1
            Me.lstItems.Items.Add(strReader.ReadLine())
        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\TextFile.txt'.

So I tried using Directory.GetCurrentDirectory - 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.GetCurrentDirectory & "~/App_Data/TextFile.txt")
        While strReader.Peek <> -1
            Me.lstItems.Items.Add(strReader.ReadLine())
        End While
    End Sub

And I received the same error message as above.
ASKER CERTIFIED SOLUTION
Avatar of Bob Learned
Bob Learned
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
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).