How do you programmatically create a file in the App_Data folder?

  • Thread starter Thread starter Tony Girgenti
  • Start date Start date
T

Tony Girgenti

Hello.

Developing a web site in VS2005, SP1, VB, .NET 2.0, ASP.NET 2.0 on XP Pro,
SP2.

I'm trying to write a file to the App_Data folder in my project using:

Dim docPath As String = "~\App_Data\Trips.xml"
Using sw As StreamWriter = File.CreateText(docPath)
sw.WriteLine(doc.InnerXml)
sw.Close()
End Using

It gives me an exception as:
System.IO.DirectoryNotFoundException = {"Could not find a part of the path
'C:\Program Files\Microsoft Visual Studio
8\Common7\IDE\~\App_Data\Trips.xml'."}

I tried "\App_Data\Trips.xml", "App_Data\Trips.xml", "~App_Data\Trips.xml".

Any help would be gratefully appreciated.

Thanks,
Tony
 
One thing i forgot to add. It works fine if i use "C:\TRIPS.XML".

I need to know how to put it in the App_Data folder in my project.

Thanks,
Tony
 
Hello Jim.

Thanks for that info. That worked, but i had to use Server.MapPath(".."),
because my web page for this part of the website is in a seperate folder. I
have all of my web pages in seperate folders by category.

How do i use the MapPath to start at the root of my website so that i can
get to the App_Data folder programmatically from wherever i'm at in the
folder structure?

Thanks,
Tony
 
Hello Jim.
Thanks for that info. That worked, but i had to use
Server.MapPath(".."), because my web page for this part of the website
is in a seperate folder. I have all of my web pages in seperate
folders by category.

How do i use the MapPath to start at the root of my website so that i
can get to the App_Data folder programmatically from wherever i'm at
in the folder structure?

If memory serves, MapPath("~") will start at the web application level

Thus MapPath("~/MyFolder") should get "MyFolder" in the current WebApp
 
Hello Rory.

That worked. Excellent.

But shouldn't
"Dim docPath As String = "~\App_Data\Trips.xml"

work the same as
"Dim docPath As String = Server.MapPath("~") & "\App_Data\Trips.xml" ?

Thanks,
Tony
 
That worked. Excellent.
But shouldn't
"Dim docPath As String = "~\App_Data\Trips.xml"
work the same as
"Dim docPath As String = Server.MapPath("~") & "\App_Data\Trips.xml" ?

From...

ms-help://MS.VSCC.v80/MS.MSDN.v80/MS.VisualStudio.v80.en/dv_aspnetcon/html/2447f50c-b849-483c-8093-85ed53e7a5bd.htm

...."The ~ operator is recognized only for server controls and in server code.
You cannot use the ~ operator for client elements."

So basically the ASP.Net side (server side) of things will understand "~"
because it will internally call MapPath.

You can use it as the SRC for an image on a serverside control and the system
will translate nicely.

However don't try to save files to "~\SomeDir\Somefile.ext" using just basic
file io, as the system will not understand what you're talking about.

MapPath is used to translate in just such occassions.

It might be worth double checking this information as it is some time since
I've worked in this area :)

Now that I finally have a copy of VS2005 I might have to dive back in and
have some fun with master pages etc :) (So much stuff to play with, So little
time :))
 
Back
Top