我正在使用 SaveFileDialog 来保存文件。现在我需要检查文件名是否已经存在。
如果存在,用户需要有机会更改名称或覆盖已经存在的文件。
我已经尝试了各种方式并进行了大量搜索,但是在技术上认为应该很容易做到,但是找不到解决方案。在 if (File.Exists(Convert.ToString(infor)) == true
) 中,必须执行检查。
SaveFileDialog sfd = new SaveFileDialog();
sfd.Filter = ".xlsx Files (*.xlsx)|*.xlsx";
if (sfd.ShowDialog() == DialogResult.OK)
{
string path = Path.GetDirectoryName(sfd.FileName);
string filename = Path.GetFileNameWithoutExtension(sfd.FileName);
for (int i = 0; i < toSave.Count; i++)
{
FileInfo infor = new FileInfo(path + @"\" + filename + "_" + exportlist[i].name + ".xlsx");
if (File.Exists(Convert.ToString(infor)) == true)
{
}
toSave[i].SaveAs(infor);
MessageBox.Show("Succesvol opgeslagen als: " + infor);
}
}
== true
是多余的。 - Chris Dunaway