Beispiel #1
0
        public static void XmlSerialize(string path, object target, XmlWriterSettings xws = null)
        {
            ChoGuard.ArgumentNotNullOrEmpty(path, "Path");
            ChoGuard.ArgumentNotNull(target, "Target");

            ChoDirectory.CreateDirectoryFromFilePath(Path.GetDirectoryName(path));

            File.WriteAllText(path, XmlSerialize(target, xws));
        }
        public static Configuration OpenExeConfiguration(string exePath)
        {
            Configuration config = ConfigurationManager.OpenExeConfiguration(exePath);

            ChoDirectory.CreateDirectoryFromFilePath(config.FilePath);
            ExeConfigurationFileMap configFile = new ExeConfigurationFileMap();

            configFile.ExeConfigFilename = config.FilePath;
            ApplicationConfiguration     = ConfigurationManager.OpenMappedExeConfiguration(configFile, ConfigurationUserLevel.None);
            return(ApplicationConfiguration);
        }
Beispiel #3
0
        public static void Serialize(string path, object target)
        {
            ChoGuard.ArgumentNotNullOrEmpty(path, "Path");
            ChoGuard.ArgumentNotNull(target, "Target");

            ChoDirectory.CreateDirectoryFromFilePath(path);

            using (FileStream f = File.OpenWrite(path))
            {
                if (target != null)
                {
                    new BinaryFormatter().Serialize(f, target);
                }
            }
        }
Beispiel #4
0
        public static void CreateXmlFileIfEmpty(string appConfigPath, string rootElementName)
        {
            ChoGuard.ArgumentNotNullOrEmpty(rootElementName, "RootElementName");

            if (!File.Exists(appConfigPath))
            {
                ChoDirectory.CreateDirectoryFromFilePath(appConfigPath);

                using (StreamWriter sw = File.CreateText(appConfigPath))
                {
                }
            }

            bool hasRoot = false;

            using (XmlReader xmlReader = XmlReader.Create(appConfigPath))
            {
                try
                {
                    if (xmlReader.MoveToContent() == XmlNodeType.Element)
                    {
                        hasRoot = true;
                    }
                }
                catch (Exception ex)
                {
                    if (ex.Message != "Root element is missing.")
                    {
                        throw;
                    }
                }
            }

            //string allText = File.ReadAllText(appConfigPath);
            if (!hasRoot) //allText.IsNullOrWhiteSpace())
            {
                File.WriteAllText(appConfigPath, @"<?xml version=""1.0"" encoding=""utf-8"" ?>{0}<{1}/>".FormatString(Environment.NewLine, rootElementName));
            }
        }