这是我的代码
protected void btnAdd_Click(object sender, EventArgs e)
{
if (FileUploadControl.HasFile)
{
try
{
string filename = Path.GetFileName(FileUploadControl.FileName);
switch(SubDrpDownList.SelectedIndex)
{
case 0:
if (!File.Exists(filename))
FileUploadControl.SaveAs(Server.MapPath("~/Books/Math/") + filename);
else if (File.Exists(filename))
{
throw new DuplicateWaitObjectException();
}
break;
case 1:
if (!File.Exists(filename))
FileUploadControl.SaveAs(Server.MapPath("~/Books/Physics/") + filename);
else if (File.Exists(filename))
{
throw new DuplicateWaitObjectException();
}
break;
case 2:
if (!File.Exists(filename))
FileUploadControl.SaveAs(Server.MapPath("~/Books/Drawing/") + filename);
else if (File.Exists(filename))
{
throw new DuplicateWaitObjectException();
}
break;
}
lblStatus.Text = "Upload status: File uploaded!";
}
catch (Exception ex)
{
lblStatus.Text = "Upload status: The file could not be uploaded. The following error occured: " + ex.Message;
}
}
}
根据类别,我希望将书籍上传到我使用 switch 语句实现的 diffrenet 文件夹中。问题是当我上传一本书然后第二次再次上传时,File.Exists(filename) 不起作用。我想要的是如果我两次上传同一个文件,我想抛出一个重复文件一直在尝试上传的异常。但是在 case 语句的情况下,即使在重复文件的情况下,也只会执行行 !File.Exists(filename)) 。为什么 File.Exists(filename)) 没有被执行?