protected void SaveConfigurationFile()
        {
            File.WriteAllLines(MainConfigurationSourcePath, new[] { @"<configuration />" });
            DesignConfigurationSource outputSource = new DesignConfigurationSource(MainConfigurationSourcePath);

            ConfigurationSource.Save(outputSource);
        }
 protected bool FileContainsSection(string file, string sectionName)
 {
     using (var configSource = new DesignConfigurationSource(file))
     {
         var section = configSource.GetLocalSection(sectionName);
         return(section != null);
     }
 }
コード例 #3
0
ファイル: with_new_providers.cs プロジェクト: janeth182/ISIL
        protected override void Arrange()
        {
            base.Arrange();

            Mock <AssemblyLocator> assemblyLocator = new Mock <AssemblyLocator>();

            assemblyLocator.Setup(x => x.Assemblies).Returns(new Assembly[] { typeof(ManageableConfigurationSourceElement).Assembly });
            this.Container.RegisterType <ManageableConfigurationSourceViewModel>(
                new InjectionConstructor(
                    typeof(ElementCollectionViewModel),
                    typeof(ConfigurationElement),
                    new InjectionParameter <AssemblyLocator>(assemblyLocator.Object)));

            targetFilePath = Path.Combine(AppDomain.CurrentDomain.BaseDirectory, "manageableSourcetextnp.config");
            File.Create(targetFilePath).Dispose();
            File.WriteAllText(targetFilePath, "<configuration/>");

            var source = new DesignConfigurationSource(targetFilePath);

            ConfigurationSourceElement =
                new ManageableConfigurationSourceElement
            {
                Name     = "manageable",
                FilePath = "file.config",
                ConfigurationManageabilityProviders =
                {
                    new ConfigurationSectionManageabilityProviderData
                    {
                        Name     = "provider",
                        TypeName = typeof(object).AssemblyQualifiedName
                    }
                }
            };
            var section =
                new ConfigurationSourceSection
            {
                SelectedSource = "manageable",
                Sources        =
                {
                    ConfigurationSourceElement
                }
            };

            source.AddLocalSection(ConfigurationSourceSection.SectionName, section);


            var sourceModel = this.Container.Resolve <ConfigurationSourceModel>();

            sourceModel.Load(source);

            SectionViewModel =
                sourceModel.Sections.Where(x => x.ConfigurationType == typeof(ConfigurationSourceSection)).Single();

            ConfigurationSourceViewModel = SectionViewModel.GetDescendentsOfType <ManageableConfigurationSourceElement>().FirstOrDefault();
            ConfigurationSourceElement   = (ManageableConfigurationSourceElement)ConfigurationSourceViewModel.ConfigurationElement;
        }
コード例 #4
0
        public bool SaveMainConfiguration(string fileName, bool saveAs)
        {
            try
            {
                if (!sourceModel.IsValidForSave() || !EnvironmentsCanSave())
                {
                    uiService.ShowError(Resources.SaveConfigurationInvalidError);
                    return(false);
                }

                if (String.IsNullOrEmpty(fileName))
                {
                    return(SaveAs());
                }

                if (!EnsureCanSaveConfigurationFile(fileName))
                {
                    return(false);
                }

                if (saveAs && !string.IsNullOrEmpty(this.configurationFileName) && File.Exists(this.configurationFileName))
                {
                    File.Copy(this.configurationFileName, fileName, true);

                    // make the target file writable (it may have inherited the read-only attribute from the original file)
                    FileAttributes attributes = File.GetAttributes(fileName);
                    File.SetAttributes(fileName, attributes & ~FileAttributes.ReadOnly);
                }


                using (var configurationSource = new DesignConfigurationSource(fileName))
                {
                    var saved = sourceModel.Save(configurationSource);
                    if (!saved)
                    {
                        return(false);
                    }
                }
                ConfigurationFilePath = fileName;
                return(true);
            }
            catch (Exception ex)
            {
                ConfigurationLogWriter.LogException(ex);
                uiService.ShowMessageWpf(string.Format(CultureInfo.CurrentCulture, Resources.SaveApplicationErrorMessage, ex.Message),
                                         Resources.SaveApplicationErrorMessageTitle, MessageBoxButton.OK);

                return(false);
            }
        }
コード例 #5
0
 /// <summary>
 /// Loads a new <see cref="ConfigurationSourceModel"/> from <paramref name="configurationFilePath"/>.
 /// </summary>
 /// <param name="configurationFilePath">The path to the configuration file that should be loaded.</param>
 public void Load(string configurationFilePath)
 {
     using (var source = new DesignConfigurationSource(configurationFilePath))
     {
         if (sourceModel.Load(source))
         {
             ConfigurationFilePath = configurationFilePath;
             IsDirty = false;
         }
         else
         {
             sourceModel.New();
         }
     }
 }
        protected override void Arrange()
        {
            base.Arrange();

            var resourceHelper = new ResourceHelper <ConfigFiles.ConfigFileLocator>();

            mainFilePath            = resourceHelper.DumpResourceFileToDisk("empty.config", "ds_abs_path");
            mainConfigurationSource = new DesignConfigurationSource(mainFilePath);

            var mainFileDirectory = Path.GetDirectoryName(mainFilePath);

            expectedFilePath = Path.Combine(mainFileDirectory, "absolutefile.config");

            configurationSourceElement = new FileConfigurationSourceElement("absolutefile", expectedFilePath);
        }
        protected override void Arrange()
        {
            base.Arrange();

            var resources = new ResourceHelper <ConfigFileLocator>();

            resources.DumpResourceFileToDisk("configurationsource_main.config");
            resources.DumpResourceFileToDisk("configurationsource_satellite.config");

            var mainConfigFilePath = Path.Combine(Environment.CurrentDirectory, "configurationsource_main.config");

            designSource = new DesignConfigurationSource(mainConfigFilePath);

            configurationSourceModel = Container.Resolve <ConfigurationSourceModel>();
        }
コード例 #8
0
        protected override void Arrange()
        {
            base.Arrange();

            var resources = new ResourceHelper <ConfigFileLocator>();

            resources.DumpResourceFileToDisk("configurationsource_main_with_missing.config");

            designSource             = new DesignConfigurationSource("configurationsource_main_with_missing.config");
            configurationSourceModel = Container.Resolve <ConfigurationSourceModel>();

            UIServiceMock.Setup(x => x.ShowWindow(It.IsAny <Window>()));
            UIServiceMock.Setup(
                x => x.ShowMessageWpf(It.IsAny <string>(), It.IsAny <string>(), It.IsAny <MessageBoxButton>()))
            .Returns(MessageBoxResult.OK).Verifiable("Message not invoked");
        }
コード例 #9
0
        protected override void Arrange()
        {
            base.Arrange();

            var resources = new ResourceHelper <ConfigFileLocator>();

            resources.DumpResourceFileToDisk("configurationsource_with_invalid_satellite.config");
            resources.DumpResourceFileToDisk("configurationsource_invalid_satellite.config");

            designSource             = new DesignConfigurationSource("configurationsource_with_invalid_satellite.config");
            configurationSourceModel = Container.Resolve <ConfigurationSourceModel>();

            UIServiceMock.Setup(x => x.ShowWindow(It.IsAny <Window>()));
            UIServiceMock.Setup(
                x => x.ShowError(It.IsAny <Exception>(), It.IsAny <string>())).Verifiable();
        }
コード例 #10
0
        protected override void Arrange()
        {
            base.Arrange();
            base.Arrange();

            var resourceHelper = new ResourceHelper <ConfigFiles.ConfigFileLocator>();

            mainFilePath            = resourceHelper.DumpResourceFileToDisk("empty.config", "ds_subdir_path");
            mainConfigurationSource = new DesignConfigurationSource(mainFilePath);

            configurationSourceElement = new FileConfigurationSourceElement("relativefile", "subdir\\relative.config");

            var mainFileDirectory = Path.GetDirectoryName(mainFilePath);

            expectedFilePath = Path.Combine(mainFileDirectory, configurationSourceElement.FilePath);

            Directory.CreateDirectory(Path.GetDirectoryName(expectedFilePath));
        }
        protected override void Arrange()
        {
            base.Arrange();

            targetFilePath = Path.Combine(AppDomain.CurrentDomain.BaseDirectory, "manageableSource.config");
            File.Create(targetFilePath).Dispose();
            File.WriteAllText(targetFilePath, "<configuration/>");

            var source = new DesignConfigurationSource(targetFilePath);

            var section =
                new ConfigurationSourceSection
            {
                SelectedSource = "manageable",
                Sources        =
                {
                    new ManageableConfigurationSourceElement
                    {
                        Name     = "manageable",
                        FilePath = "file.config",
                    }
                }
            };

            source.AddLocalSection(ConfigurationSourceSection.SectionName, section);


            var sourceModel = this.Container.Resolve <ConfigurationSourceModel>();

            sourceModel.Load(source);

            SectionViewModel =
                sourceModel.Sections.Where(x => x.ConfigurationType == typeof(ConfigurationSourceSection)).Single();

            ConfigurationSourceViewModel = SectionViewModel.GetDescendentsOfType <ManageableConfigurationSourceElement>().FirstOrDefault();
        }
コード例 #12
0
 ///<summary>
 /// Returns a new <see cref="IDesignConfigurationSource"/> configured based on this configuration element.
 ///</summary>
 ///<returns>Returns a new <see cref="IDesignConfigurationSource"/> or null if this source does not have design-time support.</returns>
 public override IDesignConfigurationSource CreateDesignSource(IDesignConfigurationSource rootSource)
 {
     return(DesignConfigurationSource.CreateDesignSource(rootSource, FilePath));
 }
コード例 #13
0
        public bool SaveDelta()
        {
            if (string.IsNullOrEmpty(applicationModel.ConfigurationFilePath))
            {
                var saveDialogResult = uiService.ShowMessageWpf(
                    DesignResources.SaveDeltaUnsavedMainConfigurationMessage,
                    DesignResources.SaveDeltaDialogTitle,
                    System.Windows.MessageBoxButton.OKCancel);

                if (saveDialogResult == System.Windows.MessageBoxResult.Cancel)
                {
                    return(false);
                }
                if (!applicationModel.SaveMainConfiguration())
                {
                    return(false);
                }
            }

            if (String.IsNullOrEmpty(EnvironmentDeltaFile))
            {
                SaveFileDialog saveEnvironmentDeltaDialog = new SaveFileDialog
                {
                    Filter = DesignResources.DeltaDialogFilter,
                    Title  = DesignResources.SaveDeltaDialogTitle
                };

                var dialogResult = uiService.ShowFileDialog(saveEnvironmentDeltaDialog);
                if (dialogResult.DialogResult != true)
                {
                    return(false);
                }
                EnvironmentDeltaFile = dialogResult.FileName;
            }

            string path = EnvironmentDeltaFile;

            if (!System.IO.Path.IsPathRooted(path))
            {
                string mainDirectory = System.IO.Path.GetDirectoryName(applicationModel.ConfigurationFilePath);
                path = System.IO.Path.Combine(mainDirectory, path);
            }

            if (File.Exists(path) && !String.Equals(LastEnvironmentDeltaSavedFilePath, path))
            {
                string overwriteDeltaConfigurationFileWarning       = string.Format(CultureInfo.CurrentCulture, DesignResources.SaveDeltaOverwriteExistingFile, path);
                var    overwriteDeltaConfigurationFileWarningResult = uiService.ShowMessageWpf(overwriteDeltaConfigurationFileWarning, DesignResources.SaveDeltaDialogTitle, MessageBoxButton.OKCancel);

                if (overwriteDeltaConfigurationFileWarningResult == MessageBoxResult.Cancel)
                {
                    return(false);
                }
            }
            if (applicationModel.EnsureCanSaveConfigurationFile(path))
            {
                DesignConfigurationSource source = new DesignConfigurationSource(path);
                Save(source);

                LastEnvironmentDeltaSavedFilePath = path;
                return(true);
            }
            return(false);
        }