I writing code to upload pdf file onto the third party server. I have done some path validation checks. I am checking if the file exists or not. Now for this validation the c# automatically throws an exception message box, without closing file dialog(browser window). I want to implement same validation for file extension. I tried checking for extension and then throwing the exception in message box, but the file dialog closes. I am attaching a screen shot so that this becomes clearer. This type of validation I want and the background window should remain open
This validation I want to implement for file extension. As far as I know there are not exceptions for validating file extensions.
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;
}
The top three statements are in a different class called moFrmIntComplaint. As these variables are used in the browse_click method, this why I have mentioned these variables here. I have tried using delegate because of this question.
Even this doesn't help.