Groups | Blog | Home
all groups > dotnet ado.net > march 2007 >

dotnet ado.net : How do you programmatically create a file in the App_Data folder?


Tony Girgenti
3/28/2007 12:59:11 PM
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

Tony Girgenti
3/28/2007 2:42:54 PM
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

[quoted text, click to view]

Jim Hughes
3/28/2007 9:09:00 PM
Try using

Dim docPath As String = Server.MapPath(".") & "\App_Data\Trips.xml"

[quoted text, click to view]

Tony Girgenti
3/29/2007 2:44:50 PM
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

[quoted text, click to view]

Tony Girgenti
3/29/2007 3:14:07 PM
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

[quoted text, click to view]

Rory Becker
3/29/2007 6:53:38 PM
[quoted text, click to view]

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

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


--
Rory

Rory Becker
3/29/2007 7:28:26 PM
[quoted text, click to view]

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 :))

--
Rory


AddThis Social Bookmark Button