views:

106

answers:

2

I want to write an .xml file using the following code into the App_Data/posts, why is it causing an error:

 Stream writer  = new FileStream("..'\'App_Data'\'posts'\'" + new Guid(post_ID.ToString()).ToString() + ".xml", FileMode.Create);
+1  A: 

Please post the exception you are getting; not just "it does not work" - this can be all sorts of problems. Here is a few things to check:

Check whether the ASP .NET process has write access to that directory.

Also, it looks like you are escaping the backspaces in the path wrong. And when working with ASP .NET, your paths should be relative to the application root directory. Try this:

string path = HttpContext.Current.Server.MapPath("~/App_Data/posts/" + new Guid(post_ID.ToString()).ToString() + ".xml"
Stream writer  = new FileStream(path, FileMode.Create);

Finally, ensure that the posts directory exists - or the file creation will fail.

driis
how do I check the ASP.NET has write access to that directory?
EquinoX
also it says that I don't have a definition for MapPath and therefore the MapPath is underlined
EquinoX
First, you can check if the exception you get is an UnauthorizedException - in that case it is a permissions problem. The exception you are getting should really be posted with your question. If it is a permissions problem, use Windows Explorer to grant Read/Write access rights to the IIS_WPG group for the App_Data folder.
driis
@Alexander - it's Server.MapPath - see update.
driis
+1  A: 

Remove the extraneous single quotes and escape your backslashes properly.

Or even better, use Server.MapPath (available in the Page and UserControl base classes and the HttpContext among other things).

Server.MapPath("~/App_Data/posts/" + new Guid(post_ID.ToString()).ToString() + ".xml")

Out of curiosity, what is the type of post_ID? Why are you converting it into a string, then into a guid, and then back to a string?

Matti Virkkunen