Jana Pasch Jana Pasch - 1 month ago 14
C# Question

Getting the error message: “Could not find a part of the path"

I create a Folder (File) and i want that after i click the button (btnGenerate_Click) it should create a pdf file and save it into the Folder, named "File". But i constantly getting the error message: "Could not find a part of the path "C:\User\VS\Intra\Intra.Admin\File\" But the Location of the path is correct....



protected void btnGenerate_Click(object sender, EventArgs e)
{
string FilePath = MapPath("~/File/"); //here!


iTextSharp.text.Document pdfDoc = new iTextSharp.text.Document(iTextSharp.text.PageSize.A4, 20f, 20f, 20f, 20f);
PdfWriter.GetInstance(pdfDoc, new FileStream(FilePath, FileMode.Create));
StringWriter sw = new StringWriter();
HtmlTextWriter hw = new HtmlTextWriter(sw);

GridView1.AllowPaging = false;
GridView1.HeaderRow.Cells[1].Text = "Message";
GridView1.HeaderRow.Font.Bold = true;
GridView1.RenderControl(hw);

StringReader sr = new StringReader(sw.ToString());
HTMLWorker htmlparser = new HTMLWorker(pdfDoc);
PdfWriter.GetInstance(pdfDoc, Response.OutputStream);
pdfDoc.Open();
htmlparser.Parse(sr);
pdfDoc.Close();

Response.Write(pdfDoc);

Response.ContentType = "Application/pdf";
Response.WriteFile(FilePath);
Response.End();

}




Answer

FilePath is a directory. You should append a filename, so something like this:

string FilePath = MapPath("~/File/" + fileName);