【问题标题】:ITextSharp add number to file name if it existsITextSharp 将数字添加到文件名(如果存在)
【发布时间】:2017-12-21 23:23:01
【问题描述】:

愚蠢的问题,但正在使用一种方法为带有ITextSharp 的收据创建 PDF,并且文件的名称基于客户名称和购买日期 但如果我在同一天有 2 次购买,ITextSharp 只会覆盖文件。我想要它,所以它会在文件名的末尾添加 (1)、(2) 等

string aux1 = data.getString("select full_name from clients where id=" + cboClient.SelectedValue);

string aux2 = getWrittenDate(DateTime.Today);

SaveFileDialog sfd = new SaveFileDialog();

sfd.FileName = "receipt" + aux1 + aux2 + ".pdf";    

Document doc = new Document(iTextSharp.text.PageSize.LETTER, 10, 10, 42, 35);
PdfWriter wri = PdfWriter.GetInstance(doc, new FileStream((sfd.FileName), FileMode.Create));
doc.Open();

【问题讨论】:

    标签: c# visual-studio desktop-application


    【解决方案1】:

    您可以使用File.Exists(filePath) 检查文件是否已存在,并在这种情况下手动重命名。

    一个好的解决方案是将购买时间添加到文件名中以使其唯一。

    希望这会有所帮助:)

    【讨论】:

      猜你喜欢
      • 2011-12-11
      • 2017-02-10
      • 2012-11-30
      • 2013-04-14
      • 1970-01-01
      • 2021-07-06
      • 1970-01-01
      • 2015-05-07
      相关资源
      最近更新 更多