Link to home
Start Free TrialLog in
Avatar of Peter Chan
Peter ChanFlag for Hong Kong

asked on

Unable to open it

Hi,
Any advice to the error I got below to this line?
105                    XmlTextWriter textwrt = new XmlTextWriter("C:\\inetpub\\common.xml", null);

Server Error in '/Login' Application.
--------------------------------------------------------------------------------

Access to the path 'C:\inetpub\common.xml' is denied.
Description: An unhandled exception occurred during the execution of the current web request. Please review the stack trace for more information about the error and where it originated in the code.

Exception Details: System.UnauthorizedAccessException: Access to the path 'C:\inetpub\common.xml' is denied.

ASP.NET is not authorized to access the requested resource. Consider granting access rights to the resource to the ASP.NET request identity. ASP.NET has a base process identity (typically {MACHINE}\ASPNET on IIS 5 or Network Service on IIS 6) that is used if the application is not impersonating. If the application is impersonating via <identity impersonate="true"/>, the identity will be the anonymous user (typically IUSR_MACHINENAME) or the authenticated request user.

To grant ASP.NET access to a file, right-click the file in Explorer, choose "Properties" and select the Security tab. Click "Add" to add the appropriate user or group. Highlight the ASP.NET account, and check the boxes for the desired access.

Source Error:


Line 103:                Userid.InnerText = "Unable to check against the database: ";
Line 104:                Userid.InnerText += ex.Message;
Line 105:            }
Line 106:            finally
Line 107:            {
 

Source File: C:\dev_proj_old\Visual Studio 2008\Projects\Login\Login\Default.aspx.cs    Line: 105

Stack Trace:


[UnauthorizedAccessException: Access to the path 'C:\inetpub\common.xml' is denied.]
   System.IO.__Error.WinIOError(Int32 errorCode, String maybeFullPath) +10550419
   System.IO.FileStream.Init(String path, FileMode mode, FileAccess access, Int32 rights, Boolean useRights, FileShare share, Int32 bufferSize, FileOptions options, SECURITY_ATTRIBUTES secAttrs, String msgPath, Boolean bFromProxy) +2580
   System.IO.FileStream..ctor(String path, FileMode mode, FileAccess access, FileShare share, Int32 bufferSize, FileOptions options, String msgPath, Boolean bFromProxy) +138
   System.IO.FileStream..ctor(String path, FileMode mode, FileAccess access, FileShare share) +89
   System.Xml.XmlTextWriter..ctor(String filename, Encoding encoding) +63
   Edit_User._Default.LoginButton_Click(Object sender, EventArgs e) in C:\dev_proj_old\Visual Studio 2008\Projects\Login\Login\Default.aspx.cs:105
   System.Web.UI.WebControls.Button.OnClick(EventArgs e) +115
   System.Web.UI.WebControls.Button.RaisePostBackEvent(String eventArgument) +140
   System.Web.UI.Page.RaisePostBackEvent(IPostBackEventHandler sourceControl, String eventArgument) +29
   System.Web.UI.Page.ProcessRequestMain(Boolean includeStagesBeforeAsyncPoint, Boolean includeStagesAfterAsyncPoint) +2981

 


--------------------------------------------------------------------------------
Version Information: Microsoft .NET Framework Version:2.0.50727.5456; ASP.NET Version:2.0.50727.5456
Avatar of Jorge Paulino
Jorge Paulino
Flag of Portugal image

Do you have access to the file? Is the file already opened/used by other application?
Avatar of Peter Chan

ASKER

Yes, the login is authorized to create files in that folder. No other app is accessing it.
But the application uses windows authentication? That message says that you don't have access to the file.

Try to use IO class to check if the file exists (you will have the same error).
Basically for web applications you cannot access the path outside its virtual directory.

To test this, give read rights to every one to the XML file.
You are definitely going outside of web/application scope, while you may be be able to configure this locally, by tweaking IIS permissions, it's unlikely you will do so in production; unless you have access to the IIS Management Console on the production server.

Alan
Thanks all.
I now have this Xml folder in the project
http://dl.dropbox.com/u/40211031/t176.png

or please see this instead
https://skydrive.live.com/?cid=17ec75244bac022f#cid=17EC75244BAC022F&id=17EC75244BAC022F!263

I tried to open the file like this in Project A
XmlTextWriter textwrt = new XmlTextWriter("~/Xml/common.xml", null);

but I get this when running it within Win 08 server, after I've published the project on it
Server Error in '/Login' Application.
--------------------------------------------------------------------------------

Could not find a part of the path 'c:\windows\system32\inetsrv\~\Xml\common.xml'.
Description: An unhandled exception occurred during the execution of the current web request. Please review the stack trace for more information about the error and where it originated in the code.

Exception Details: System.IO.DirectoryNotFoundException: Could not find a part of the path 'c:\windows\system32\inetsrv\~\Xml\common.xml'.

Source Error:


Line 104:                Userid.InnerText += ex.Message;
Line 105:            }
Line 106:            finally
Line 107:            {
Line 108:                conn.Close();
 

Source File: C:\dev_proj_old\Visual Studio 2008\Projects\Login\Login\Default.aspx.cs    Line: 106

Stack Trace:


[DirectoryNotFoundException: Could not find a part of the path 'c:\windows\system32\inetsrv\~\Xml\common.xml'.]
   System.IO.__Error.WinIOError(Int32 errorCode, String maybeFullPath) +239
   System.IO.FileStream.Init(String path, FileMode mode, FileAccess access, Int32 rights, Boolean useRights, FileShare share, Int32 bufferSize, FileOptions options, SECURITY_ATTRIBUTES secAttrs, String msgPath, Boolean bFromProxy) +2580
   System.IO.FileStream..ctor(String path, FileMode mode, FileAccess access, FileShare share, Int32 bufferSize, FileOptions options, String msgPath, Boolean bFromProxy) +138
   System.IO.FileStream..ctor(String path, FileMode mode, FileAccess access, FileShare share) +89
   System.Xml.XmlTextWriter..ctor(String filename, Encoding encoding) +63
   Edit_User._Default.LoginButton_Click(Object sender, EventArgs e) in C:\dev_proj_old\Visual Studio 2008\Projects\Login\Login\Default.aspx.cs:106
   System.Web.UI.WebControls.Button.OnClick(EventArgs e) +115
   System.Web.UI.WebControls.Button.RaisePostBackEvent(String eventArgument) +140
   System.Web.UI.Page.RaisePostBackEvent(IPostBackEventHandler sourceControl, String eventArgument) +29
   System.Web.UI.Page.ProcessRequestMain(Boolean includeStagesBeforeAsyncPoint, Boolean includeStagesAfterAsyncPoint) +2981

 


--------------------------------------------------------------------------------
Version Information: Microsoft .NET Framework Version:2.0.50727.5456; ASP.NET Version:2.0.50727.5456
Try using mappath to map the path to the virtual path (~\Xml\common.xml)

string FILENAME = Server.MapPath("~\Xml\common.xml");
XmlTextWriter textwrt = new XmlTextWriter(FILENAME);

Alan
Thanks. I have these
                    string FILENAME = Server.MapPath(@"~\Xml\common.xml");
                    XmlTextWriter textwrt = new XmlTextWriter(FILENAME, null);

I've republished the project but I've got this when running it
Server Error in '/Login' Application.
--------------------------------------------------------------------------------

Could not find a part of the path 'C:\inetpub\wwwroot\Login\Xml\common.xml'.
Description: An unhandled exception occurred during the execution of the current web request. Please review the stack trace for more information about the error and where it originated in the code.

Exception Details: System.IO.DirectoryNotFoundException: Could not find a part of the path 'C:\inetpub\wwwroot\Login\Xml\common.xml'.

Source Error:


Line 105:            }
Line 106:            finally
Line 107:            {
Line 108:                conn.Close();
Line 109:            }
 

Source File: C:\dev_proj_old\Visual Studio 2008\Projects\Login\Login\Default.aspx.cs    Line: 107

Stack Trace:


[DirectoryNotFoundException: Could not find a part of the path 'C:\inetpub\wwwroot\Login\Xml\common.xml'.]
   System.IO.__Error.WinIOError(Int32 errorCode, String maybeFullPath) +239
   System.IO.FileStream.Init(String path, FileMode mode, FileAccess access, Int32 rights, Boolean useRights, FileShare share, Int32 bufferSize, FileOptions options, SECURITY_ATTRIBUTES secAttrs, String msgPath, Boolean bFromProxy) +2580
   System.IO.FileStream..ctor(String path, FileMode mode, FileAccess access, FileShare share, Int32 bufferSize, FileOptions options, String msgPath, Boolean bFromProxy) +138
   System.IO.FileStream..ctor(String path, FileMode mode, FileAccess access, FileShare share) +89
   System.Xml.XmlTextWriter..ctor(String filename, Encoding encoding) +63
   Edit_User._Default.LoginButton_Click(Object sender, EventArgs e) in C:\dev_proj_old\Visual Studio 2008\Projects\Login\Login\Default.aspx.cs:107
   System.Web.UI.WebControls.Button.OnClick(EventArgs e) +115
   System.Web.UI.WebControls.Button.RaisePostBackEvent(String eventArgument) +140
   System.Web.UI.Page.RaisePostBackEvent(IPostBackEventHandler sourceControl, String eventArgument) +29
   System.Web.UI.Page.ProcessRequestMain(Boolean includeStagesBeforeAsyncPoint, Boolean includeStagesAfterAsyncPoint) +2981

 


--------------------------------------------------------------------------------
Version Information: Microsoft .NET Framework Version:2.0.50727.5456; ASP.NET Version:2.0.50727.5456
ASKER CERTIFIED SOLUTION
Avatar of Alan Warren
Alan Warren
Flag of Philippines image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Yes, this does exist in the server
C:\inetpub\wwwroot\Login\Xml

I've adjusted the codes to be

                    ...
106                    string FILENAME = Server.MapPath(@"..\Xml\common.xml");
107                    ?XmlTextWriter textwrt = new XmlTextWriter(FILENAME, null);
                    ...

but I get this
Server Error in '/Login' Application.
--------------------------------------------------------------------------------

Access to the path 'C:\inetpub\wwwroot\Xml\common.xml' is denied.
Description: An unhandled exception occurred during the execution of the current web request. Please review the stack trace for more information about the error and where it originated in the code.

Exception Details: System.UnauthorizedAccessException: Access to the path 'C:\inetpub\wwwroot\Xml\common.xml' is denied.

ASP.NET is not authorized to access the requested resource. Consider granting access rights to the resource to the ASP.NET request identity. ASP.NET has a base process identity (typically {MACHINE}\ASPNET on IIS 5 or Network Service on IIS 6) that is used if the application is not impersonating. If the application is impersonating via <identity impersonate="true"/>, the identity will be the anonymous user (typically IUSR_MACHINENAME) or the authenticated request user.

To grant ASP.NET access to a file, right-click the file in Explorer, choose "Properties" and select the Security tab. Click "Add" to add the appropriate user or group. Highlight the ASP.NET account, and check the boxes for the desired access.

Source Error:


Line 105:            }
Line 106:            finally
Line 107:            {
Line 108:                conn.Close();
Line 109:            }
 

Source File: C:\dev_proj_old\Visual Studio 2008\Projects\Login\Login\Default.aspx.cs    Line: 107

Stack Trace:


[UnauthorizedAccessException: Access to the path 'C:\inetpub\wwwroot\Xml\common.xml' is denied.]
   System.IO.__Error.WinIOError(Int32 errorCode, String maybeFullPath) +10550419
   System.IO.FileStream.Init(String path, FileMode mode, FileAccess access, Int32 rights, Boolean useRights, FileShare share, Int32 bufferSize, FileOptions options, SECURITY_ATTRIBUTES secAttrs, String msgPath, Boolean bFromProxy) +2580
   System.IO.FileStream..ctor(String path, FileMode mode, FileAccess access, FileShare share, Int32 bufferSize, FileOptions options, String msgPath, Boolean bFromProxy) +138
   System.IO.FileStream..ctor(String path, FileMode mode, FileAccess access, FileShare share) +89
   System.Xml.XmlTextWriter..ctor(String filename, Encoding encoding) +63
   Edit_User._Default.LoginButton_Click(Object sender, EventArgs e) in C:\dev_proj_old\Visual Studio 2008\Projects\Login\Login\Default.aspx.cs:107
   System.Web.UI.WebControls.Button.OnClick(EventArgs e) +115
   System.Web.UI.WebControls.Button.RaisePostBackEvent(String eventArgument) +140
   System.Web.UI.Page.RaisePostBackEvent(IPostBackEventHandler sourceControl, String eventArgument) +29
   System.Web.UI.Page.ProcessRequestMain(Boolean includeStagesBeforeAsyncPoint, Boolean includeStagesAfterAsyncPoint) +2981

 


--------------------------------------------------------------------------------
Version Information: Microsoft .NET Framework Version:2.0.50727.5456; ASP.NET Version:2.0.50727.5456

when running it. Why?

Many Thanks & Best Regards,
HuaMin
Hi HuaMin,

That's an improvement, it's found the file:
Exception Details: System.UnauthorizedAccessException: Access to the path 'C:\inetpub\wwwroot\Xml\common.xml' is denied.

You need to grant permissions for the actual folder, the folder named "XML".
C:\inetpub\wwwroot\Xml\

Now there's lot's of opinions about how much permission should or should not be extended, but it's your development server, on your machine, so I wouldn't worry about that too much for now.

Browse to the C:\inetpub\wwwroot\ folder using your MyComputer window.
Right click the folder 'XML' and choose properties.
In the properties dialog, on the Security tab, click the Edit button.
In the list find/select IIS_IUSRS, then check the appropriate checkboxes, then click ok.
They (IIS users) will need to be able to list folder contents, read, possibly modify too, if you intend to update this xml file; if all else fails, give them full control, it's possibly only you that uses IIS on your dev machine anyway.

Alan
Many thanks Alan.
Hi HuaMin,
do you have a web.config in that xml folder, restricting access to authenticated users?

If not, it should work, the following is a test page ran in a different localhost site:
<%@ Page Language="VB" %>
<%@ Import Namespace="System.Xml" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

<script runat="server">

  Protected Sub Page_Load(sender As Object, e As System.EventArgs) Handles Me.Load
    ' Response.Write(Server.MapPath("../site1/xml/"))
    
    Dim strFilePath As String = Server.MapPath("../site1/xml/Items.xml")
    
    ' Create an isntance of XmlTextReader and call Read method to read the(file)
    ' This works
    Dim textReader As XmlTextReader = New XmlTextReader(strFilePath)
    
    ' This works too
    ' Dim textReader As XmlTextReader = New XmlTextReader("C:\inetpub\wwwroot\site1\xml\Items.xml")
    
    
    ' This won't compile
    ' Dim textReader As XmlTextReader = New XmlTextReader(Server.MapPath(@"..\site1\xml\Items.xml"))


    textReader.Read()
    ' If the node has value
    If textReader.HasValue Then
      ' Move to fist element
      textReader.MoveToElement()
      Response.Write("XmlTextReader Properties Test<br />")
      Response.Write("===================<br />")
      ' Read this element's properties and display them on console
      Response.Write("Name:" + textReader.Name + "<br />")
      Response.Write("Base URI:" + textReader.BaseURI + "<br />")
      Response.Write("Local Name:" + textReader.LocalName + "<br />")
      Response.Write("Attribute Count:" + textReader.AttributeCount.ToString() + "<br />")
      Response.Write("Depth:" + textReader.Depth.ToString() + "<br />")
      Response.Write("Line Number:" + textReader.LineNumber.ToString() + "<br />")
      Response.Write("Node Type:" + textReader.NodeType.ToString() + "<br />")
      Response.Write("Attribute Count:" + textReader.Value.ToString() + "<br />")
    End If
    
  End Sub
</script>

<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
    <title></title>
</head>
<body>
    <form id="form1" runat="server">
    <div>
    
    </div>
    </form>
</body>
</html>

Open in new window

Returns:
XmlTextReader Properties Test
===================
Name:xml
Base URI:file:///C:/inetpub/wwwroot/site1/xml/Items.xml
Local Name:xml
Attribute Count:2
Depth:0
Line Number:1
Node Type:XmlDeclaration
Attribute Count:version="1.0" encoding="utf-8"

Alan ";0)
Hi HuaMin,

Using C#, no issues too:

  protected void Page_Load(object sender, System.EventArgs e)
  {
	// Response.Write(Server.MapPath("../site1/xml/"))

	string strFilePath = Server.MapPath("../site1/xml/Items.xml");

	// Create an isntance of XmlTextReader and call Read method to read the(file)
	// This works
	System.Xml.XmlTextReader textReader = new System.Xml.XmlTextReader(strFilePath);

	// This works too
	// Dim textReader As XmlTextReader = New XmlTextReader("C:\inetpub\wwwroot\site1\xml\Items.xml")


	// This won't compile
	// Dim textReader As XmlTextReader = New XmlTextReader(Server.MapPath(@"..\site1\xml\Items.xml"))


	textReader.Read();
	// If the node has value
	if (textReader.HasValue)
	{
	  // Move to fist element
	  textReader.MoveToElement();
	  Response.Write("XmlTextReader Properties Test<br />");
	  Response.Write("===================<br />");
	  // Read this element's properties and display them on console
	  Response.Write("Name:" + textReader.Name + "<br />");
	  Response.Write("Base URI:" + textReader.BaseURI + "<br />");
	  Response.Write("Local Name:" + textReader.LocalName + "<br />");
	  Response.Write("Attribute Count:" + textReader.AttributeCount.ToString() + "<br />");
	  Response.Write("Depth:" + textReader.Depth.ToString() + "<br />");
	  Response.Write("Line Number:" + textReader.LineNumber.ToString() + "<br />");
	  Response.Write("Node Type:" + textReader.NodeType.ToString() + "<br />");
	  Response.Write("Attribute Count:" + textReader.Value.ToString() + "<br />");
	}

  }

Open in new window

Alan