• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 332
  • Last Modified:

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
0
kmoloney
Asked:
kmoloney
  • 3
  • 2
1 Solution
 
kmoloneyAuthor Commented:
Increased points by 100% to 500 so as to increase thus-far lackadaisical response.
0
 
Bob LearnedCommented:
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
0
 
kmoloneyAuthor Commented:
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.
0
 
Bob LearnedCommented:
Try this:

  Dim strReader As StreamReader = File.OpenText(Server.MapPath("~/App_Data/TextFile.txt"))

Bob
0
 
kmoloneyAuthor Commented:
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).

0

Featured Post

Get quick recovery of individual SharePoint items

Free tool – Veeam Explorer for Microsoft SharePoint, enables fast, easy restores of SharePoint sites, documents, libraries and lists — all with no agents to manage and no additional licenses to buy.

  • 3
  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now