我编写代码将pdf文件上传到第三方服务器上。我做了一些路径验证检查。我正在检查文件是否存在。现在,对于此验证,c#会自动抛出异常消息框,而不会关闭文件对话框(浏览器窗口)。我想为文件扩展实现相同的验证。我尝试检查扩展,然后在消息框中抛出异常,但文件对话框关闭。我附加了一个屏幕截图,以便更清楚。 This type of validation I want and the background window should remain open
我要为文件扩展实现此验证。据我所知,验证文件扩展名没有例外。
public System.Windows.Forms.OpenFileDialog cmndBrowseOpen;
public System.Windows.Forms.SaveFileDialog cmndBrowseSave;
public System.Windows.Forms.DialogResult cmdBrowseResult
public void cmdBrowse_Click()
{
const string sFilter = "PDFs (*.pdf)|*.pdf";
string sMsg = String.Empty;
string sTitle = String.Empty;
FileInfo oFile = null;
try
{
if (moFrmIntComplaint != null)
{
moFrmIntComplaint.cmndBrowseOpen.Filter = sFilter;
moFrmIntComplaint.cmndBrowseOpen.Title = ResourceHandler.Resources.GetString("BrowseFile:Title");
moFrmIntComplaint.cmdBrowseResult = moFrmIntComplaint.cmndBrowseOpen.ShowDialog();
string fileExtension = Path.GetExtension(moFrmIntComplaint.cmndBrowseOpen.FileName);
moFrmIntComplaint.cmndBrowseOpen.DefaultExt = ".pdf";
if (fileExtension != ".pdf")
{
moFrmIntComplaint.cmndBrowseOpen.FileOk += delegate(object s, System.ComponentModel.CancelEventArgs ev)
{
ev.Cancel = true;
};
}
if (String.IsNullOrEmpty(moFrmIntComplaint.cmndBrowseOpen.FileName) == false)
{
oFile = new FileInfo(moFrmIntComplaint.cmndBrowseOpen.FileName);
if ((oFile != null) && (File.Exists(moFrmIntComplaint.cmndBrowseOpen.FileName) && (fileExtension == ".pdf")))
{
if (Convert.ToDouble(oFile.Length) > dMaxFileSize)
{
sTitle = ResourceHandler.Resources.GetString("BrowseFile:FilSizTitle");
sMsg = ResourceHandler.Resources.GetString("BrowseFile:FileSize");
Cerner.Foundations.Measurement.TimedMessageBox.Show(sMsg, sTitle, MessageBoxButtons.OK, MessageBoxIcon.Exclamation);
moFrmIntComplaint.lblFileName.Text = System.String.Empty;
moFrmIntComplaint.lblFilePath.Text = System.String.Empty;
moFrmIntComplaint.cmdRemove.Visible = false;
}
else
{
moFrmIntComplaint.lblFileName.Text = Path.GetFileName(moFrmIntComplaint.cmndBrowseOpen.FileName);
moFrmIntComplaint.lblFilePath.Text = moFrmIntComplaint.cmndBrowseOpen.FileName;
moFrmIntComplaint.cmdRemove.Left = moFrmIntComplaint.lblFileName.Left + moFrmIntComplaint.lblFileName.Width + 5;
moFrmIntComplaint.cmdRemove.Visible = true;
}
}
}
}
return;
}
前三个语句位于另一个名为moFrmIntComplaint的类中。由于这些变量在browse_click方法中使用,这就是我在这里提到这些变量的原因。我尝试过使用委托because of this question.
即使这没有帮助。