Question

I have a list of jobs and i give users the option to get job description in form of a pdf file. I use MigraDoc/PDFsharp to generate pdf files. The problem is, after i render the pdf document and want to save it somewhere on the DevServer i get UnauthorizedAccessException on creating FileStream in PDFsharp PdfDocument.Save() method. I never really used Windows for anything more advanced than playing games and i'm not sure why would i get this exception since i'm logged in as Administrator user and i guess that my ASP.NET application is running with Administrator privilleges and should be able to write files pretty much anywhere on filesystem.

The Code.

        GridViewRow jobRow = (GridViewRow)(sender as Control).Parent.Parent;

        Document jobDocument = new Document();
        Section xyz = jobDocument.AddSection();
        xyz.AddParagraph("Wonderfull job");

        PdfDocumentRenderer pdfRenderer = new PdfDocumentRenderer(true);
        pdfRenderer.Document = jobDocument;
        pdfRenderer.RenderDocument();

        string filename = "Job_" + jobRow["3"] + ".pdf"; // Job_[title].pdf
        pdfRenderer.PdfDocument.Save(filename);

Last line is the line that causes the exception. Any suggestions? I'm not an ASP.NET developer and I'm forced to use ASP.NET for my school project so this may be a very simple problem but i really don't know what to do and what to search. Thanks for answering!

Was it helpful?

Solution

Administrator account =! IIS user. The IIS User needs to have writing privileges, too! On most machines it is know as "IIS_User" or maybe "network service". First you can grant writing permissions to every User. If this works for you, you know what to do. First try to set an absolute path as filename, maybe a directory outside of "C:\inetpub\wwwroot"? Hope this helps!

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top