コード例 #1
0
        private void AddDatabase(string filepath)
        {
            string filename    = Path.GetFileName(filepath);
            string newFilePath = Path.Combine(FileSystem.DbPath, filename);

            // If the DB is already loaded, let user reconfigure
            var fileAlreadyLoaded = CheckFileLoaded(filepath);

            if (fileAlreadyLoaded != null)
            {
                if (MessageBox.Show("This database is already loaded. Would you like to configure it?", "Database", MessageBoxButtons.YesNo) == DialogResult.Yes)
                {
                    EditDatabase(fileAlreadyLoaded);
                }
                return;
            }

            // If the DB is not loaded, but already in DB folder, we don't need to copy
            bool inDbFolder = CheckSameFile(filepath, newFilePath);

            if (!inDbFolder)
            {
                // Don't overwrite existing file
                if (File.Exists(newFilePath))
                {
                    if (MessageBox.Show("A database with the same filename is already exists. The new database file will be renamed.", "Database Notice", MessageBoxButtons.OKCancel) == DialogResult.OK)
                    {
                        newFilePath = FileSystem.GetUniqueFilename(newFilePath);
                    }
                    else
                    {
                        return;
                    }
                }

                File.Copy(filepath, newFilePath);
            }

            // Set up some reasonable defaults
            var newDbEntry = new DBConfig.Database();

            newDbEntry.Format   = DBFormat.ClrMamePro;
            newDbEntry.Filename = FileSystem.MakePathRelative(FileSystem.DbPath, newFilePath);

            DialogResult result = DBEdit.EditEntry(newDbEntry);

            // If the user cancels, and the file was copied to DB folder, delete the folder.
            if (result == DialogResult.Cancel && !inDbFolder)
            {
                FileSystem.IgnoreFileErrors(() => File.Delete(newFilePath));
            }
            else
            {
                Program.DatabaseConfig.Databases.Add(newDbEntry);
                defaultSelection = newDbEntry;
            }

            // Re-populate list
            PopulateList();
            MakeDefaultSelection();
            return;
        }