Example #1
0
        public FileDialog EnforceExt(params string[] Ext)
        {
            if (events.HasFlag(ExistingEvents.Extension))
            {
                throw new Exception("Enforced extensions can only be set once.");
            }
            events |= ExistingEvents.Extension;

            fileDialog.FileOk += delegate(object sender, CancelEventArgs e)
            {
                bool test = false;
                foreach (string ext in Ext)
                {
                    test = test ? test : ((OpenFileDialog)sender).FileName.EndsWith(ext);
                }
                if (!test)
                {
                    MessageBox.Show("Please select a filename with " + (Ext.Count() == 1 ? "the extension " : "one of the following extensions: ") + String.Join(" ", Ext), "Incorrect File Extension");
                    e.Cancel = true;
                }
            };
            return(this);
        }