我正在創建一個Windows窗體項目並創建一個PDF文件。我想保存用戶想要的文件。我爲此使用了SaveFileDialog。當我點擊我的表單上的「另存爲PDF」按鈕時,我得到這個錯誤代碼。試圖讀取或寫入受保護的內存。這通常表明其他內存已損壞
Attempted to read or write protected memory. This is often an indication that other memory is corrupt.
如果我不使用SaveFileDialog(如果我給該文件一個靜態名稱),不要得到錯誤。
這裏是按鈕單擊代碼:
private void button2_Click(object sender, EventArgs e)
{
SaveFileDialog saveFileDialog1 = new SaveFileDialog();
saveFileDialog1.InitialDirectory = Convert.ToString(Environment.SpecialFolder.MyDocuments);
saveFileDialog1.Filter = "(*.pdf)|*.pdf|All Files (*.*)|*.*";
saveFileDialog1.FilterIndex = 1;
saveFileDialog1.ShowDialog();
if (saveFileDialog1.FileName != "")
{
iTextSharp.text.Document pdfDosya = new iTextSharp.text.Document(PageSize.A4, 20, 20, 10, 10);
PdfWriter.GetInstance(pdfDosya, new FileStream(saveFileDialog1.FileName, FileMode.Create));//TODO dosya ismi
pdfDosya.Open();
}
}
我怎樣才能解決這個問題。
只是一個側面說明(不是回答你的問題)。使用'if(saveFileDialog1.ShowDialog()== DialogResult.OK)'而不是'if(saveFileDialog1.FileName!=「」)''。 –
此外,'Convert.ToString(Environment.SpecialFolder.MyDocuments)==「MyDocuments」',不會產生路徑。 –
你能從異常中發佈堆棧跟蹤嗎? –