/// <summary> /// Captures all file IO Exceptions and will appropriately invoke on the UI thread to attempt /// opening of a file from local drive. /// </summary> /// <param name="sender">Required but not utilized.</param> /// <param name="e">Used to figure out what type of error occured and the next steps.</param> private void onFileOpen(Object sender, OnNoFileOpenArgs e) { Debug.WriteLine("onFileOpen called on ThreadID: " + Thread.CurrentThread.ManagedThreadId); if (this.InvokeRequired) { this.Invoke((MethodInvoker) delegate { onFileOpen(sender, e); }); } else { OpenFileDialog openDialog = new OpenFileDialog(); openDialog.Filter = "mdf files (*.mdf)|*.mdf|txt files (*.txt)|*.txt|All files (*.*)|*.*"; openDialog.RestoreDirectory = true; if (openDialog.ShowDialog() == DialogResult.OK) { string fileName = openDialog.FileName; switch (e.fileError) { case FileErrorType.NoCommandsFile: if (Regex.IsMatch(fileName, @".*(\.txt)\Z")) { Task.Factory.StartNew(() => irc.loadlocalCommands(e.channel, fileName)); } else { Debug.WriteLine("File type was incorrect."); } break; case FileErrorType.NoDatabaseFile: if (Regex.IsMatch(fileName, @".*(\.mdf)\Z")) { SqlConnection conn = new SqlConnection(@"Data source=.\SQLExpress; Integrated Security=true; AttachDbFilename=" + fileName + "; Trusted_Connection=Yes; User Instance=true; Database=dbo.Users; " + "MultipleActiveResultSets=true; Connect Timeout=30"); irc.connDict.TryAdd(e.channel, conn); Task.Factory.StartNew(() => irc.loadDatabase(e.channel, e.listofUsersSql)); } else { Debug.WriteLine("File type was incorrect."); } break; } } } }