private string GetFile(OFType fileType, string currentFile) { switch (fileType) { case OFType.Database: openFileDialog.Filter = ResourceHelper.BuildFileFilter(FileFilterType.ShoeboxAnthropologyDatabase, FileFilterType.AllFiles); openFileDialog.Title = LexTextControls.ksSelectAnthropologyStdFmtFile; break; case OFType.Project: openFileDialog.Filter = ResourceHelper.BuildFileFilter(FileFilterType.ShoeboxProjectFiles, FileFilterType.AllFiles); openFileDialog.Title = LexTextControls.ksSelectShoeboxProjectFile; break; case OFType.Settings: openFileDialog.Filter = ResourceHelper.BuildFileFilter(FileFilterType.ImportMapping, FileFilterType.AllFiles); openFileDialog.Title = LexTextControls.ksSelectLoadImportSettingsFile; break; case OFType.SaveAs: openFileDialog.Filter = ResourceHelper.BuildFileFilter(FileFilterType.ImportMapping, FileFilterType.AllFiles); openFileDialog.Title = LexTextControls.ksSelectSaveImportSettingsFile; break; } openFileDialog.FilterIndex = 1; // don't require file to exist if it's "SaveAs" openFileDialog.CheckFileExists = (fileType != OFType.SaveAs); openFileDialog.Multiselect = false; bool done = false; while (!done) { // LT-6620 : putting in an invalid path was causing an exception in the openFileDialog.ShowDialog() // Now we make sure parts are valid before setting the values in the openfile dialog. string dir = string.Empty; try { dir = Path.GetDirectoryName(currentFile); } catch { } if (Directory.Exists(dir)) openFileDialog.InitialDirectory = dir; // if we don't set it to something, it remembers the last file it saw. This can be // a very poor default if we just opened a valuable data file and are now choosing // a place to save settings (LT-8126) if (File.Exists(currentFile) || (fileType == OFType.SaveAs && Directory.Exists(dir))) openFileDialog.FileName = currentFile; else openFileDialog.FileName = ""; if (openFileDialog.ShowDialog(this) == DialogResult.OK) { bool isValid = false; string sFileType; if (fileType == OFType.Database) { sFileType = LexTextControls.ksStandardFormat; isValid = IsValidSfmFile(openFileDialog.FileName); } else if (fileType == OFType.Project) { sFileType = SIL.FieldWorks.LexText.Controls.LexTextControls.ksShoeboxProject; Sfm2Xml.IsSfmFile validFile = new Sfm2Xml.IsSfmFile(openFileDialog.FileName); isValid = validFile.IsValid; } else if (fileType == OFType.SaveAs) { sFileType = LexTextControls.ksXmlSettings; isValid = true; // no requirements since the file will be overridden } else { sFileType = LexTextControls.ksXmlSettings; isValid = IsValidMapFile(openFileDialog.FileName); } if (!isValid) { string msg = String.Format(LexTextControls.ksSelectedFileXInvalidY, openFileDialog.FileName, sFileType, System.Environment.NewLine); DialogResult dr = MessageBox.Show(this, msg, LexTextControls.ksPossibleInvalidFile, MessageBoxButtons.YesNoCancel, MessageBoxIcon.Warning); if (dr == DialogResult.Yes) return openFileDialog.FileName; else if (dr == DialogResult.No) continue; else break; // exit with current still } return openFileDialog.FileName; } else done = true; } return currentFile; }
private string GetFile(OFType fileType, string currentFile) { openFileDialog.Filter = ResourceHelper.BuildFileFilter(FileFilterType.ImportMapping, FileFilterType.XML, FileFilterType.AllShoeboxDictionaryDatabases, FileFilterType.AllFiles); openFileDialog.FilterIndex = (fileType == OFType.Settings) ? 1 : (fileType == OFType.Database) ? 3 : 4; // only require the file to exist if its Database or Settings openFileDialog.CheckFileExists = true; if (fileType == OFType.SaveAs) openFileDialog.CheckFileExists = false; openFileDialog.Multiselect = false; bool done = false; while (!done) { // LT-6620 : putting in an invalid path was causing an exception in the openFileDialog.ShowDialog() // Now we make sure parts are valid before setting the values in the openfile dialog. string dir = string.Empty; try { dir = Path.GetDirectoryName(currentFile); } catch{} if (Directory.Exists(dir)) openFileDialog.InitialDirectory = dir; // if we don't set it to something, it remembers the last file it saw. This can be // a very poor default if we just opened a valuable data file and are now choosing // a place to save settings (LT-8126) if (File.Exists(currentFile) || (fileType == OFType.SaveAs && Directory.Exists(dir))) openFileDialog.FileName = currentFile; else openFileDialog.FileName = ""; openFileDialog.Title = String.Format(LexTextControls.ksSelectXFile, fileType.ToString()); if (openFileDialog.ShowDialog() == DialogResult.OK) { bool isValid = false; string text; // Before doing the 'fileType' based tests, make sure it's not a PhaseX file if (GetDictionaryFileAsPhaseFileNumber(openFileDialog.FileName) > 0) { isValid = true; // trusting that phaseX files properly named are valid text = LexTextControls.ksPhaseFile; } else if (fileType == OFType.Database) { text = LexTextControls.ksStandardFormat; Sfm2Xml.IsSfmFile validFile = new Sfm2Xml.IsSfmFile(openFileDialog.FileName); isValid = validFile.IsValid; } else if (fileType == OFType.SaveAs) { text = LexTextControls.ksXmlSettings; isValid = true; // no requirements sense the file will be overridden } else { text = LexTextControls.ksXmlSettings; isValid = MarkerPresenter.IsValidMapFile(openFileDialog.FileName); } if (!isValid) { string msg = String.Format(LexTextControls.ksSelectedFileXInvalidY, openFileDialog.FileName, text, System.Environment.NewLine); DialogResult dr = MessageBox.Show(this, msg, LexTextControls.ksPossibleInvalidFile, MessageBoxButtons.YesNoCancel, MessageBoxIcon.Warning); if (dr == DialogResult.Yes) return openFileDialog.FileName; else if (dr == DialogResult.No) continue; else break; // exit with current still } return openFileDialog.FileName; } else done = true; } return currentFile; }