私は 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();
}
}
どうすれば問題を解決できますか。