示例#1
0
        private void ConfigurationList_ListChanged(object sender, ListChangedEventArgs e)
        {
            ContextConfiguration config;
            string fileName;

            switch (e.ListChangedType)
            {
            case ListChangedType.ItemChanged:
                config = optionsForm.ConfigurationList[e.NewIndex];
                configs[e.NewIndex] = config;
                fileName            = Path.Combine(configFolder, $"{config.ApplicationName}.yaml");
                ContextConfiguration.ExportToFile(fileName, config);
                break;

            case ListChangedType.ItemAdded:
                config = optionsForm.ConfigurationList[e.NewIndex];
                configs.Insert(e.NewIndex, config);
                fileName = Path.Combine(configFolder, $"{config.ApplicationName}.yaml");
                ContextConfiguration.ExportToFile(fileName, config);
                break;

            case ListChangedType.ItemDeleted:
                config   = configs[e.NewIndex];
                fileName = Path.Combine(configFolder, $"{config.ApplicationName}.yaml");
                if (File.Exists(fileName))
                {
                    File.Delete(fileName);
                    configs.RemoveAt(e.NewIndex);
                }
                break;

            default:
                break;
            }
        }
        private void ButtonExportConfiguration_Click(object sender, EventArgs e)
        {
            if (activeIndex >= 0 && activeIndex < ConfigurationList.Count)
            {
                var config = ConfigurationList[activeIndex];

                SaveFileDialog fileDialog = new SaveFileDialog
                {
                    Filter           = "YAML files (*.yaml)|*.yaml",
                    Title            = "Save configuration file",
                    FileName         = $"{config.ApplicationName}.yaml",
                    InitialDirectory = Environment.GetFolderPath(Environment.SpecialFolder.Personal)
                };
                if (fileDialog.ShowDialog() == DialogResult.OK)
                {
                    ContextConfiguration.ExportToFile(fileDialog.FileName, config);
                }
            }
        }