-1

I have this error when deploying app. Could not find a part of the path 'c:\windows\system32\inetsrv\~\App_Data\xxxx.pdf'.

Exception Details: System.IO.DirectoryNotFoundException: Could not find a part of the path 'c:\windows\system32\inetsrv\~\App_Data\XXXX.pdf'.

the app default method is to redirect to pdf file.

this is my code

 FileStream fs = new FileStream("~/App_Data/xxx.pdf", FileMode.Create);

        Document doc = new Document(PageSize.A4, 25, 25, 30, 30);
        doc.SetMargins(40f, 40f, 40f, 20f);
        PdfWriter writer = PdfWriter.GetInstance(doc, fs);
        doc.Open();

plz any help?

thanks,

Tuta
  • 133
  • 2
  • 13

1 Answers1

1

Your path file for FileStream uses ASP.NET relative path with tilde prefix, try using Server.MapPath method to map it into correct path:

FileStream fs = new FileStream(System.Web.HttpContext.Current.Server.MapPath("~/App_Data/xxx.pdf"), FileMode.Create);

If the code exists inside action method in a controller class, just use HttpContext.Current.Server.MapPath:

FileStream fs = new FileStream(HttpContext.Current.Server.MapPath("~/App_Data/xxx.pdf"), FileMode.Create);

Similar issues:

Read contents of a file using a relative path in a Web Application

ASP.NET C# - Save FileStream on server

Tetsuya Yamamoto
  • 24,297
  • 8
  • 39
  • 61
  • its give me red underline Server. and this error. An object reference is required for the non static field, method or property. – Tuta Sep 25 '17 at 08:02
  • Try using fully-qualified name: `System.Web.HttpContext.Current.Server.MapPath("~/App_Data/xxx.pdf")` or if it inside a controller: `HttpContext.Current.Server.MapPath("~/App_Data/xxx.pdf")`. – Tetsuya Yamamoto Sep 25 '17 at 08:06