zoukankan      html  css  js  c++  java
  • c#本地文件配置xml

        /// <summary>
        /// 处理xml文件
        /// </summary>
        public class HandelXmlFile
        {
            private string _configPath;
            ///文件地址
            private void SetPath<T>()
            {
                var type = typeof(T);
                _configPath = AppDomain.CurrentDomain.BaseDirectory + @"Config" + type.Name + ".config";
                if (!File.Exists(_configPath))
                    File.Create(_configPath);
            }
            /// <summary>
            /// 保存xml文件
            /// </summary>
            public void Save<T>(T model)
            {
                try
                {
                    SetPath<T>();
                    using (FileStream fs = new FileStream(_configPath, FileMode.Create, FileAccess.ReadWrite, FileShare.ReadWrite))
                    {
                        XmlSerializerHelper.Serialize(fs, model);
                    }
                }
                catch {  }
            }
    
            public T OpenFiles<T>()
            {
                try
                {
                    SetPath<T>();
                    if (File.Exists(_configPath))
                    {
                        using (FileStream fs = new FileStream(_configPath, FileMode.Open, FileAccess.ReadWrite, FileShare.ReadWrite))
                        {
                            return XmlSerializerHelper.DeSerialize<T>(fs);
                        }
                    }
                    return default(T);
                }
                catch { return default(T); }
            }
        }
  • 相关阅读:
    P2P编程(十)
    9.25
    9.22
    pycharm常用快捷命令
    sublime常用快捷方式
    3.1
    总想听你说起不曾喜欢你
    1.1
    python 网络编程和并发编程题
    知识点梳理 网络基础
  • 原文地址:https://www.cnblogs.com/shuaimeng/p/10768557.html
Copyright © 2011-2022 走看看