Wrox Programmer Forums

Need to download code?

View our list of code downloads.

Go Back   Wrox Programmer Forums > ASP.NET and ASP > ASP.NET 4 > ASP.NET 4 General Discussion
Password Reminder
Register
| FAQ | Members List | Calendar | Search | Today's Posts | Mark Forums Read
ASP.NET 4 General Discussion For ASP.NET 4 discussions not relating to a specific Wrox book
Welcome to the p2p.wrox.com Forums.

You are currently viewing the ASP.NET 4 General Discussion section of the Wrox Programmer to Programmer discussions. This is a community of tens of thousands of software programmers and website developers including Wrox book authors and readers. As a guest, you can read any forum posting. By joining today you can post your own programming questions, respond to other developers’ questions, and eliminate the ads that are displayed to guests. Registration is fast, simple and absolutely free .
DRM-free e-books 300x50
Reply
 
Thread Tools Search this Thread Display Modes
  #1 (permalink)  
Old August 7th, 2010, 04:25 PM
Friend of Wrox
 
Join Date: Feb 2009
Posts: 194
Thanks: 5
Thanked 3 Times in 3 Posts
Default ASP.NET permissions

I have written some code using the XMLTextWriter to write some stuff into an XML file. However It keeps throwing an exception saying that Access to the file is denied.

I have gone and changed the security settings for this file so that "Everyone" has full control over the file however I still incur this error when attempting to write to this file.

What else would cause this? I am building and running the application using the built in server within visual web developer.

Any ideas?

Cheers
__________________
Follow me on twitter.

Where I work.

Connect with me on LinkedIn

Blog
Reply With Quote
  #2 (permalink)  
Old August 8th, 2010, 03:48 AM
Imar's Avatar
Wrox Author
Points: 72,073, Level: 100
Points: 72,073, Level: 100 Points: 72,073, Level: 100 Points: 72,073, Level: 100
Activity: 100%
Activity: 100% Activity: 100% Activity: 100%
 
Join Date: Jun 2003
Location: Utrecht, Netherlands.
Posts: 17,089
Thanks: 80
Thanked 1,587 Times in 1,563 Posts
Default

Can you provide more information such as the relevant code and the exact error message? Maybe there are other things (temp folder for example) involved, or maybe the error message is sneding you in the wrong direction.

Cheers,

Imar
__________________
Imar Spaanjaars
http://Imar.Spaanjaars.Com
Follow me on Twitter

Author of Beginning ASP.NET 4.5 : in C# and VB, Beginning ASP.NET Web Pages with WebMatrix
and Beginning ASP.NET 4 : in C# and VB.
Did this post help you? Click the button below this post to show your appreciation!
Reply With Quote
  #3 (permalink)  
Old August 8th, 2010, 05:07 AM
Friend of Wrox
 
Join Date: Feb 2009
Posts: 194
Thanks: 5
Thanked 3 Times in 3 Posts
Default

Sure,

The exception message that I get is:

Access to the path 'C:\Program Files\Common Files\Microsoft Shared\DevServer\10.0\ErrorLog.xml' is denied.

It seems to be occurring when this fires:

XmlTextWriter textWriter = new XmlTextWriter("ErrorLog.xml", Encoding.UTF8);


However when googling for solutions I was advised that I would need to set the permissions on the actual file itself.

The code I am attemtping to use is to record each time an exception occurs:

Code:
public static void CreateXMLErrorLog(Exception error)
{
    //Create xml log entry
    XmlTextWriter textWriter = new XmlTextWriter("ErrorLog.xml", Encoding.UTF8);

    //Construct xml
    textWriter.WriteStartDocument();
    textWriter.WriteStartElement("error");
    textWriter.WriteStartElement("date", DateTime.Now.ToShortDateString());
    textWriter.WriteEndElement();
    textWriter.WriteStartElement("time", DateTime.Now.ToShortTimeString());
    textWriter.WriteEndElement();
    textWriter.WriteStartElement("errorDetails", error.ToString());
    textWriter.WriteEndElement();
    textWriter.WriteEndElement();
    textWriter.WriteEndDocument();
    textWriter.Close();
  }
First time i've used that so I was hoping to test the results from it.
__________________
Follow me on twitter.

Where I work.

Connect with me on LinkedIn

Blog
Reply With Quote
  #4 (permalink)  
Old August 8th, 2010, 05:28 AM
Imar's Avatar
Wrox Author
Points: 72,073, Level: 100
Points: 72,073, Level: 100 Points: 72,073, Level: 100 Points: 72,073, Level: 100
Activity: 100%
Activity: 100% Activity: 100% Activity: 100%
 
Join Date: Jun 2003
Location: Utrecht, Netherlands.
Posts: 17,089
Thanks: 80
Thanked 1,587 Times in 1,563 Posts
Default

Are you running this on Windows 7? I think access is blocked to the Program Files folder if you're not running this as an Administrator. Setting permissions then doesn't make a difference.

Try specifying a path like this:

Code:
 
XmlTextWriter textWriter = new XmlTextWriter(@"C:\Test\ErrorLog.xml"), Encoding.UTF8);
And assign the permissions to the C:\Test folder. Does it work then?

Imar
__________________
Imar Spaanjaars
http://Imar.Spaanjaars.Com
Follow me on Twitter

Author of Beginning ASP.NET 4.5 : in C# and VB, Beginning ASP.NET Web Pages with WebMatrix
and Beginning ASP.NET 4 : in C# and VB.
Did this post help you? Click the button below this post to show your appreciation!
Reply With Quote
  #5 (permalink)  
Old August 8th, 2010, 05:56 AM
Friend of Wrox
 
Join Date: Feb 2009
Posts: 194
Thanks: 5
Thanked 3 Times in 3 Posts
Default

Yes it works when I make those changes and use the test file as suggested.

I closed VS down and ran it as an administrator and tried it again using the original code and I got no exception this time but it doesn't seem to have written anything into the XML file.

Thanks for your help!
__________________
Follow me on twitter.

Where I work.

Connect with me on LinkedIn

Blog
Reply With Quote
  #6 (permalink)  
Old August 8th, 2010, 06:10 AM
Imar's Avatar
Wrox Author
Points: 72,073, Level: 100
Points: 72,073, Level: 100 Points: 72,073, Level: 100 Points: 72,073, Level: 100
Activity: 100%
Activity: 100% Activity: 100% Activity: 100%
 
Join Date: Jun 2003
Location: Utrecht, Netherlands.
Posts: 17,089
Thanks: 80
Thanked 1,587 Times in 1,563 Posts
Default

Hui Will,

It's generally a bad idea to write to Program Files from your application. You're better off storing data in the user's folder or in a folder outside Windows folders.

Cheers,

Imar
__________________
Imar Spaanjaars
http://Imar.Spaanjaars.Com
Follow me on Twitter

Author of Beginning ASP.NET 4.5 : in C# and VB, Beginning ASP.NET Web Pages with WebMatrix
and Beginning ASP.NET 4 : in C# and VB.
Did this post help you? Click the button below this post to show your appreciation!
Reply With Quote
  #7 (permalink)  
Old August 8th, 2010, 06:17 AM
Friend of Wrox
 
Join Date: Feb 2009
Posts: 194
Thanks: 5
Thanked 3 Times in 3 Posts
Default

Ok, I wanted to have the file as an xml file that logs any errors that occur.

When I put this onto a server will this work? I had just intended to include an XML file in the application and have the errors written to that file path but this is a bad idea?
__________________
Follow me on twitter.

Where I work.

Connect with me on LinkedIn

Blog
Reply With Quote
  #8 (permalink)  
Old August 8th, 2010, 06:33 AM
Imar's Avatar
Wrox Author
Points: 72,073, Level: 100
Points: 72,073, Level: 100 Points: 72,073, Level: 100 Points: 72,073, Level: 100
Activity: 100%
Activity: 100% Activity: 100% Activity: 100%
 
Join Date: Jun 2003
Location: Utrecht, Netherlands.
Posts: 17,089
Thanks: 80
Thanked 1,587 Times in 1,563 Posts
Default

Quote:
When I put this onto a server will this work?
That will depend on the server and its configuration.
Quote:
I had just intended to include an XML file in the application and have the errors written to that file path but this is a bad idea?
Probably. Again, it depends. For a web application, you could write to App_Data instead. For other types of applications, I prefer to create a separate folder like d:\LogFiles and create a sub folder for each application that needs to log. Easier to configure and manage.....

Imar
__________________
Imar Spaanjaars
http://Imar.Spaanjaars.Com
Follow me on Twitter

Author of Beginning ASP.NET 4.5 : in C# and VB, Beginning ASP.NET Web Pages with WebMatrix
and Beginning ASP.NET 4 : in C# and VB.
Did this post help you? Click the button below this post to show your appreciation!
Reply With Quote
  #9 (permalink)  
Old August 8th, 2010, 08:01 AM
Friend of Wrox
 
Join Date: Feb 2009
Posts: 194
Thanks: 5
Thanked 3 Times in 3 Posts
Default

Thanks for the help, will look into the App_Data.

Cheers
__________________
Follow me on twitter.

Where I work.

Connect with me on LinkedIn

Blog
Reply With Quote
  #10 (permalink)  
Old August 8th, 2010, 12:57 PM
Friend of Wrox
 
Join Date: Feb 2009
Posts: 194
Thanks: 5
Thanked 3 Times in 3 Posts
Default

Alright I don't seem to have the permissions issue I was having when attempting to write to the App_Data folder? Is this because ASP.NET is automatically configured to have access to this folder?

I don't know too much about the App_Data folder, is this a protected folder by default? Would it be a good place to store .pdf or other types of files that I don't want made available to the public and then just read them from the folder?

I am now using this code to create an error log but I am overwriting previous entries each time...

Code:
public static void CreateXMLErrorLog(Exception error)
  {
    //Create xml log entry
    XmlTextWriter textWriter = new XmlTextWriter(HttpContext.Current.Server.MapPath("~/App_Data/ErrorLog.xml"), Encoding.UTF8);

    //Construct xml
    textWriter.WriteStartDocument();
    textWriter.WriteStartElement("error");
    textWriter.WriteElementString("date", DateTime.Now.ToShortDateString());
    textWriter.WriteElementString("time", DateTime.Now.ToShortTimeString());
    textWriter.WriteElementString("errorDetails", error.ToString());
    textWriter.WriteEndElement();
    textWriter.WriteEndDocument();
    textWriter.Close();
  }
This is just because of the XmlTextWriter I suppose what would be a better option so that I can just add in new entries instead of overwriting each time?
__________________
Follow me on twitter.

Where I work.

Connect with me on LinkedIn

Blog
Reply With Quote
Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off
Trackbacks are Off
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
assign report permissions thru vb .net code polofson Reporting Services 0 April 15th, 2008 05:31 PM
Turning a regular ASP.NET App into an AJAX ASP.Net donrafeal7 Ajax 2 August 31st, 2007 12:33 AM
Restrict ASP.NET app DB permissions using Integrat wirerider ASP.NET 2.0 Professional 0 March 6th, 2007 08:23 PM
Permissions error when executing DTS from ASP pjmair SQL Server DTS 9 February 6th, 2004 02:47 PM
IIS 6.0 and ASP.NET Permissions dikkjo ASP.NET 1.0 and 1.1 Basics 4 July 29th, 2003 03:06 AM



All times are GMT -4. The time now is 07:16 PM.


Powered by vBulletin®
Copyright ©2000 - 2020, Jelsoft Enterprises Ltd.
© 2013 John Wiley & Sons, Inc.