Share via


Access to the path 'C:\ is denied.

Question

Tuesday, April 16, 2013 9:20 PM

this error display with me:

Access to the path 'C:\ 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. 

            // Read in the contents of the Receipt.htm HTML template file
            string contents = File.ReadAllText(Server.MapPath("~/email_templates/Mail2.htm"));
            contents = contents.Replace("[Fullname]", (txtFName.Text + " " + txtLName.Text));

            string contentsmn = File.ReadAllText(Server.MapPath("~/email_templates/Mail1.htm"));
            contentsmn = contentsmn.Replace("[Fullname]", txtFName.Text + " " + txtLName.Text);

            string Booking = File.ReadAllText(Server.MapPath("~/email_templates/details.htm"));
            string MailClint = "";
            MailClint += "<b>" + txtFName.Text + " " + txtLName.Text + ".</b><br/>";
            MailClint += "<b>Address: </b>" + txtCountry.Text+ ", " + txtCity.Text+ " " + txtAddress.Text + ".<br/>";
            MailClint += "<b>Phone : </b>" + txtTel.Text + ".<br/>";
            Booking = Booking.Replace("[Clint]", MailClint.ToString());

            string MailBody = "";
            MailBody += "<table border='0.3' style='background-color:#cccccc;'><tr><td><b>Arrival Date: </td><td>" + lblArrival.Text + ".</td><tr>";
            MailBody += "<tr><td><b>Departure: </td><td>" + lblDeparture.Text+ ".</td><tr>";
                MailBody += "<tr><td><b>Well stay for: </td><td>" + lblNights.Text+ " Nights.</td><tr>";
                MailBody += "<tr><td><b>Person(s):</td><td>" + lblPersons.Text+ ".</td><tr>";
                MailBody += "<tr><td><b>Price: </td><td>" + lblRooms.Text + " * " + lblPrice.Text + " " + Currancy+ ".</td><tr>";
                MailBody += "<tr><td><b>Total Cost: </td><td> After discount" +Price * MaxPer + " " + Currancy + " </td><tr></table>";
                MailBody += "<br/><br/>";

            MailBody += "";

            Booking = Booking.Replace("[Details]", MailBody.ToString());
            Booking = Booking.Replace("[GTotal]", Price * MaxPer + " " + Currancy );

            var doc = new Document();
            MemoryStream memoryStream = new MemoryStream();
            PdfWriter writer = PdfWriter.GetInstance(doc, memoryStream);

            // Open the Document for writing
            doc.Open();

            var parsedHtmlElements = HTMLWorker.ParseToList(new StringReader(Booking), null);
            foreach (var htmlElement in parsedHtmlElements)
            {
                try
                {
                    doc.Add(htmlElement as IElement);
                }
                catch (Exception)
                {

                    throw;
                }

            }

            writer.CloseStream = false;

            doc.Close();
            memoryStream.Position = 0;

            MailMessage mm = new MailMessage((txtEMail.Text).ToString(), (txtEMail.Text).ToString())
            {
                Subject = "Reservation Details",
                IsBodyHtml = true,
            };

            MailMessage mn = new MailMessage((txtEMail.Text).ToString(), (txtEMail.Text).ToString())
            {
                Subject = "Welcom ",
                IsBodyHtml = true,
            };

            mm.Attachments.Add(new Attachment(memoryStream, "Reservation-Details.pdf"));

            mm.Body = contents;
            mn.Body = (contentsmn);
            SmtpClient smtp = new SmtpClient
            {
                Host = "smtpout.secureserver.net",
                Port = 587,
                EnableSsl = false,
                Credentials = new NetworkCredential("[email protected]", "******")

            };

            smtp.Send(mn);
            smtp.Send(mm);

          

All replies (6)

Tuesday, April 16, 2013 11:26 PM ✅Answered

Sharkawy,

Does perhaps the html file Details.htm or any of the other html files referenced in code contain a link to any image on the c:\ drive?  If so, your web app does not have permission to it.  Move it to the web folder, and make a relative path to the image.

You are not making it very easy for us to help you...

Mike


Tuesday, April 16, 2013 9:49 PM

Please tell us which line generates the exception.  Based only on the error, you are trying read from or write to the c:\ which the IIS process won't likely have permission to access.

Mike


Tuesday, April 16, 2013 9:50 PM

What does the stack trace say?

Marcus Björklund

Please use "Mark As Answer" if my post has answered your question, and/or vote for it if you find it helpful. Thanks!


Tuesday, April 16, 2013 10:27 PM

[UnauthorizedAccessException: Access to the path 'C:\' is denied.]
   System.IO.__Error.WinIOError(Int32 errorCode, String maybeFullPath) +9722910
   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, Boolean useLongPath) +1142
   System.IO.FileStream..ctor(String path, FileMode mode, FileAccess access, FileShare share, Int32 bufferSize, FileOptions options, String msgPath, Boolean bFromProxy) +78
   System.Net.FileWebStream..ctor(FileWebRequest request, String path, FileMode mode, FileAccess access, FileShare sharing, Int32 length, Boolean async) +76
   System.Net.FileWebResponse..ctor(FileWebRequest request, Uri uri, FileAccess access, Boolean asyncHint) +94

[WebException: Access to the path 'C:\' is denied.]
   System.Net.FileWebResponse..ctor(FileWebRequest request, Uri uri, FileAccess access, Boolean asyncHint) +396
   System.Net.FileWebRequest.GetResponseCallback(Object state) +291

[WebException: Access to the path 'C:\' is denied.]
   System.Net.FileWebRequest.EndGetResponse(IAsyncResult asyncResult) +271
   System.Net.FileWebRequest.GetResponse() +260
   iTextSharp.text.Image.GetInstance(Uri url) +53
   iTextSharp.text.html.simpleparser.HTMLWorker.StartElement(String tag, Hashtable h) +2441
   iTextSharp.text.xml.simpleparser.SimpleXMLParser.Go(TextReader reader) +1287
   iTextSharp.text.html.simpleparser.HTMLWorker.ParseToList(TextReader reader, StyleSheet style, Hashtable interfaceProps) +131
   iTextSharp.text.html.simpleparser.HTMLWorker.ParseToList(TextReader reader, StyleSheet style) +7
   Hotel_Reservierung.LoadHtml() +2212
   Hotel_Reservierung.btnCheckout_Click(Object sender, EventArgs e) +5
   System.Web.UI.WebControls.Button.OnClick(EventArgs e) +118
   System.Web.UI.WebControls.Button.RaisePostBackEvent(String eventArgument) +112
   System.Web.UI.WebControls.Button.System.Web.UI.IPostBackEventHandler.RaisePostBackEvent(String eventArgument) +10
   System.Web.UI.Page.RaisePostBackEvent(IPostBackEventHandler sourceControl, String eventArgument) +13
   System.Web.UI.Page.RaisePostBackEvent(NameValueCollection postData) +36
   System.Web.UI.Page.ProcessRequestMain(Boolean includeStagesBeforeAsyncPoint, Boolean includeStagesAfterAsyncPoint) +5563

Tuesday, April 16, 2013 10:28 PM

Access to the path 'C:\ 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:\ 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 and IIS 7, and the configured application pool identity on IIS 7.5) 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: 

The source code that generated this unhandled exception can only be shown when compiled in debug mode. To enable this, please follow one of the below steps, then request the URL:

1. Add a "Debug=true" directive at the top of the file that generated the error. Example:

  <%@ Page Language="C#" Debug="true" %>

or:

2) Add the following section to the configuration file of your application:

<configuration>
   <system.web>
       <compilation debug="true"/>
   </system.web>
</configuration>

Note that this second technique will cause all files within a given application to be compiled in debug mode. The first technique will cause only that particular file to be compiled in debug mode.

Important: Running applications in debug mode does incur a memory/performance overhead. You should make sure that an application has debugging disabled before deploying into production scenario.

Tuesday, April 16, 2013 10:57 PM

[...]

[WebException: Access to the path 'C:\' is denied.]
   System.Net.FileWebRequest.EndGetResponse(IAsyncResult asyncResult) +271
   System.Net.FileWebRequest.GetResponse() +260
   iTextSharp.text.Image.GetInstance(Uri url) +53
   iTextSharp.text.html.simpleparser.HTMLWorker.StartElement(String tag, Hashtable h) +2441
   iTextSharp.text.xml.simpleparser.SimpleXMLParser.Go(TextReader reader) +1287
   iTextSharp.text.html.simpleparser.HTMLWorker.ParseToList(TextReader reader, StyleSheet style, Hashtable interfaceProps) +131
   iTextSharp.text.html.simpleparser.HTMLWorker.ParseToList(TextReader reader, StyleSheet style) +7
   Hotel_Reservierung.LoadHtml() +2212
   Hotel_Reservierung.btnCheckout_Click(Object sender, EventArgs e) +5

[...]

OK, so the error seems to be caused by a reference to an image..?

The code in your original post, in which method is it located? Does that method contain any other code that could be causing the error? Because I can't find any references to the code you posted in the stack trace...

Marcus Björklund

Please use "Mark As Answer" if my post has answered your question, and/or vote for it if you find it helpful. Thanks!