示例#1
0
        /// <summary>
        /// Attempts to find the specified XML scenario description file, either at its original
        /// location or below <see cref="ScenarioFolder"/>.</summary>
        /// <param name="file">
        /// An absolute or relative file path to an XML scenario description file.</param>
        /// <returns>
        /// A <see cref="RootedPath"/> wrapping the absolute path at which the specified <paramref
        /// name="file"/> was found.</returns>
        /// <exception cref="FileNotFoundException">
        /// The specified scenario <paramref name="file"/> could not be found anywhere.</exception>
        /// <exception cref="ArgumentNullOrEmptyException">
        /// <paramref name="file"/> is a null reference or an empty string.</exception>
        /// <remarks>
        /// <b>SearchScenarioTree</b> prepends <see cref="ScenarioFolder"/> to the specified
        /// <paramref name="file"/> if it contains a relative path. If no file exists at the
        /// resulting absolute path, <b>SearchScenarioTree</b> then attempts to find the file name
        /// anywhere in the subdirectory tree below <see cref="ScenarioFolder"/>.</remarks>

        public static RootedPath SearchScenarioTree(string file)
        {
            if (String.IsNullOrEmpty(file))
            {
                ThrowHelper.ThrowArgumentNullOrEmptyException("file");
            }

            // prepend default scenario path to relative paths
            RootedPath path = CreateCommonPath(ScenarioFolder, file);

            // check if file exists at specified location
            if (!File.Exists(path.AbsolutePath))
            {
                // look for file name in scenario directory tree
                string search = IOUtility.SearchDirectoryTree(ScenarioFolder, path.FileName);
                path = path.Change(search);

                // file not found anywhere, abort operation
                if (path.IsEmpty)
                {
                    ThrowHelper.ThrowFileNotFoundException(file, Strings.ErrorGameScenario);
                }
            }

            Debug.Assert(File.Exists(path.AbsolutePath));
            return(path);
        }
示例#2
0
        /// <summary>
        /// Shows an <see cref="SaveFileDialog"/> allowing the user to enter or select a file to
        /// save.</summary>
        /// <param name="title">
        /// The title bar text for the <see cref="SaveFileDialog"/>.</param>
        /// <param name="filter">
        /// The file type filter for displayed files.</param>
        /// <param name="index">
        /// The index of the initially selected file type filter.</param>
        /// <param name="extension">
        /// The default extension for files entered by the user.</param>
        /// <param name="path">
        /// A <see cref="RootedPath"/> wrapping the absolute file path initially selected in the
        /// dialog.</param>
        /// <param name="directory">
        /// The directory initially shown in the dialog if <paramref name="path"/> is empty.</param>
        /// <returns>
        /// A <see cref="RootedPath"/> wrapping the absolute file path selected by the user, or an
        /// empty path if the user cancelled the dialog.</returns>
        /// <exception cref="ArgumentNullException">
        /// <paramref name="path"/> is a null reference.</exception>
        /// <remarks>
        /// The specified <paramref name="path"/> may be empty to indicate that no file should be
        /// initially selected. In this case, the dialog initially shows the specified <paramref
        /// name="directory"/>, which is otherwise ignored.</remarks>

        private static RootedPath SaveDialog(string title, string filter,
                                             int index, string extension, RootedPath path, string directory)
        {
            if (path == null)
            {
                ThrowHelper.ThrowArgumentNullException("path");
            }

            SaveFileDialog dialog = new SaveFileDialog();

            dialog.DefaultExt       = extension;
            dialog.Filter           = filter;
            dialog.FilterIndex      = index;
            dialog.InitialDirectory = directory ?? "";
            dialog.RestoreDirectory = true;
            dialog.Title            = title;
            dialog.ValidateNames    = true;

            // pre-select specified file path, if any
            if (!path.IsEmpty)
            {
                dialog.InitialDirectory = path.DirectoryName;
                dialog.FileName         = path.FileName;
            }

            // allow creation of new files
            dialog.CheckFileExists = false;
            dialog.CheckPathExists = false;

            // return empty path if user cancels
            var owner = System.Windows.Application.Current.MainWindow;

            if (dialog.ShowDialog(owner) != true)
            {
                return(path.Clear());
            }

            // return selected file path
            return(path.Change(dialog.FileName));
        }