WPF使用ARCGIS App文件配置Cs后台文件

using System;
using System.Collections.Generic;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Windows;

using ESRI.ArcGIS.esriSystem;
using System.Xml.Serialization;
using System.IO;
using SWGW.Properties;
using System.Diagnostics;
namespace SWGW
{
    /// <summary>
    /// App.xaml 的交互逻辑
    /// </summary>
    public partial class App : Application
    {

        #region License
        protected override void OnStartup(StartupEventArgs e)
        {
            SettingsManager.OnStartup();
            base.OnStartup(e);

            if (!LicenseManager.InitializeLicense.InitializeEngineLicense())
            {
                MessageBox.Show("无法初始化ArcGIS Engine的授权或地球扩展。截止...");
            }
           
        }
        #endregion

        #region 私有类
        static class SettingsManager
        {
            #region Class Members
            static Settings settings = new Settings();
            const string applicationDirectory = "SWGW";
            //配置文件名称
            const string settingsFileName = "XXXX.xml";
            #endregion Class Members

            #region Constructor

            static SettingsManager()
            {
                EnsureDirectoryExists();
            }

            #endregion Constructor

            #region Properties

            public static Settings ApplicationSettings
            {
                get { return settings; }
            }

            #endregion Properties

            #region Startup, Exit

            public static void OnStartup()
            {
                LoadSettings();
            }

            public static void OnExit()
            {
                SaveSettings();
            }

            #endregion Overrides

            #region Other Functions

            static string SettingsFileName
            {
                get
                {
                    return Path.Combine(
                        Path.Combine(
                        Environment.GetFolderPath(Environment.SpecialFolder.LocalApplicationData),
                        applicationDirectory),
                        settingsFileName);
                }
            }

            static void LoadSettings()
            {
                Settings tmp;

                try
                {
                    XmlSerializer xml = new XmlSerializer(typeof(Settings));

                    using (Stream stream = new System.IO.FileStream(SettingsFileName,
                        FileMode.Open, FileAccess.Read, FileShare.Read))
                    {
                        tmp = (Settings)xml.Deserialize(stream);
                    }
                }
                catch (Exception e)
                {
                    Trace.WriteLine(e.Message);
                    return;
                }

                settings = tmp;
            }

            static void SaveSettings()
            {
                try
                {
                    XmlSerializer xml = new XmlSerializer(typeof(Settings));

                    using (Stream stream = new System.IO.FileStream(SettingsFileName,
                           FileMode.Create, FileAccess.Write, FileShare.None))
                    {
                        xml.Serialize(stream, settings);
                    }
                }
                catch (Exception e)
                {
                    Trace.WriteLine(e.Message);
                }
            }

            [System.Diagnostics.CodeAnalysis.SuppressMessage("Microsoft.Design", "CA1031:DoNotCatchGeneralExceptionTypes")]
            static void EnsureDirectoryExists()
            {
                try
                {
                    DirectoryInfo info = new DirectoryInfo(
                        Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.LocalApplicationData),
                        applicationDirectory));

                    if (!info.Exists)
                    {
                        info.Create();
                    }
                }
                catch (Exception ex)
                {
                    Trace.WriteLine(ex.Message);
                }
            }

            #endregion Other Functions
        }


        #endregion
    }
}

 

posted @ 2016-07-23 10:24  清风小筑  阅读(569)  评论(0编辑  收藏  举报