0

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!

Viktor
  • 85
  • 8

1 Answers1

0

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!

jordi
  • 51
  • 4
  • I'm not 100% sure but i don't use IIS. I use default web server that comes with Visual Studio 2010(not sure if it has anything to do with IIS). I forgot to mention that i tried writing file outside of my web root directory and that works without problems. – Viktor Apr 10 '12 at 09:39
  • Then grant access for "everyone" to your application directory / to the directory the PDF should be saved. Right Click on Folder --> Properties --> Security / Permissions --> Edit --> Full access to everyone. (Maybe translations are wrong) – jordi Apr 10 '12 at 10:15
  • however, it works! ;) But take care, everyone is now permitted to read/write/delete in this folder! – jordi Apr 10 '12 at 12:48