Deleting a file

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • semomaniz
    Recognized Expert New Member
    • Oct 2007
    • 210

    Deleting a file

    I have a .NET page built with c#. This page creates a temporary report and then creates a pdf file. Then this file is sent to users via email. After the email is sent out i have written a code to delete the temporary file. My problem is i am getting a accessed denied exception.

    Exception Details: System.Unauthor izedAccessExcep tion: Access to the path 'c:\Inetpub\vho sts\***\***\*** *\10836.pdf' is denied.

    here is the code that checks for the file and then performs the delete.
    Code:
    string file = HttpContext.Current.Server.MapPath("../***/" + fileid + ".pdf");
    
    if (File.Exists(file))
    {
           File.Delete(file);
    }
    I have set full permission on IIS for IO purpose. I am sure this error is due to permission issue. Any advice will be greatly appretiated. Thank you in advance.
  • PRR
    Recognized Expert Contributor
    • Dec 2007
    • 750

    #2
    Look if aspnet account has permission for that particular folder... right click the folder... go to security->add->advanced ->click on find now...
    click asp.net account and add and give permission to it ...this might help...

    Comment

    • sangam56
      New Member
      • Nov 2007
      • 68

      #3
      Hi semomaniz, you said full access has been given to the in IIS for IO purpose. I suggest to add group Everyone in the security following the way as suggested by DeepBlue.

      Hope this helps. Thanks.

      Comment

      • semomaniz
        Recognized Expert New Member
        • Oct 2007
        • 210

        #4
        Thank you for the response but now i am getting a different error while trying to delete the file. The error states, the file is being used by another process. I am positive that this is because the file is being attached over the email and this is taking time to process. How do i make code to delete the file in the same click event. Is this even possible or do i have to trigger another event to delete the file?

        Comment

        • semomaniz
          Recognized Expert New Member
          • Oct 2007
          • 210

          #5
          Does any body have any idea on this? I have noticed that the code is throwing and error because of the file being attached on the email before its deletion. if i do not sent the email and compile the code the pdf file is created and then deleted but since i am attaching the file after creation and trying to delete it, i am getting the error. Any idea ??

          Thank you in advance

          Comment

          • Frinavale
            Recognized Expert Expert
            • Oct 2006
            • 9749

            #6
            Try giving your attaching process more time to attach the file.
            Consider leaving the file in the temporary folder sit there...until the next email is sent.
            Or you can leave the file sit there until the user's session times out if you're using Sessions....or you could leave it sit there until the user clicks the "ok" button that appears after the email's been sent.

            I do something similar in my web application. When the user clicks a button while viewing a web report, I create a PDF version of my report and send the user a link allowing them to download the file. When the user leaves the page I clean up the file. Since the user could simply close the browser, I also attempt to clean up the temporary report in the Global.asax Session_End event to make sure that the temporary resource is removed.

            -Frinny

            Comment

            • Curtis Rutland
              Recognized Expert Specialist
              • Apr 2008
              • 3264

              #7
              I don't know if you are manually writing this file or not, but if you are, make sure to .Flush() and .Close() your streams so that the process releases the file. Always close your streams.

              Comment

              • semomaniz
                Recognized Expert New Member
                • Oct 2007
                • 210

                #8
                I have closed all my streams. the problem that i am encountering is that the file is being used by the email client so i am not being able to delete the file right away. I am not sure if there is a method to delete the file when file is not being used by the email client.

                Comment

                Working...