0

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.

Community
  • 1
  • 1
Nikhil Kumar
  • 117
  • 3
  • 12
  • 3
    It doesn't "Throw exception message box" it actaully handle exception by showing message box. If You throw exception in some point it will dive to the top until it will be handled. – MajkeloDev Nov 26 '15 at 11:05
  • Yes C# handles the exception and gives appropriate message if the file exists or not. I want to know how to implement similar kind of handling for extensions. specifically I want the back ground window(file dialog) to be open when the error message comes. – Nikhil Kumar Nov 26 '15 at 11:08
  • Paste Your code and we will take a look. – MajkeloDev Nov 26 '15 at 11:30
  • Shouldn't you just do all that validation in the `FileOk` handler? It's very unclear what you're trying to do here. – Luaan Nov 26 '15 at 11:50
  • You can validate in 'FileOk' , but still over there also there are no exceptions for file extensions. – Nikhil Kumar Nov 26 '15 at 11:53

0 Answers0