zoukankan      html  css  js  c++  java
  • C# Ini配置文件

    public class INIUserAccound
        {
    
            static IniFile Ini = new IniFile(AppDomain.CurrentDomain.BaseDirectory + @"ConfigUser.Ini");
    
            const string Session = "UserInfo";
            /// <summary>
            /// 获得用户名
            /// </summary>
            /// <returns></returns>
            public static string GetAccound()
            {
                return Ini.IniReadValue(Session, "Accound");
            }
            /// <summary>
            /// 保存用户名
            /// </summary>
            /// <param name="SkinName"></param>
            public static void SetAccound(string Accound)
            {
                Ini.IniWriteValue(Session, "Accound", Accound);
            }
    
    
            /// <summary>
            /// 获得密码
            /// </summary>
            /// <returns></returns>
            public static string GetPwd()
            {
                return Ini.IniReadValue(Session, "Pwd");
            }
            /// <summary>
            /// 保存密码
            /// </summary>
            /// <param name="SkinName"></param>
            public static void SetPwd(string Pwd)
            {
                Ini.IniWriteValue(Session, "Pwd", Pwd);
            }
            
        }
     /// <summary>
        /// 操作INI文件类    测试信息
        /// </summary>
        public class IniFile
        {
            const int DATA_SIZE = 1024;
    
            private string _path; //INI档 案 名 
            public string IniPath { get { return _path; } set { _path = value; } }
    
            [StructLayout(LayoutKind.Sequential, CharSet = CharSet.Auto)]
            public struct STRINGBUFFER
            {
                [MarshalAs(UnmanagedType.ByValTStr, SizeConst = DATA_SIZE)]
                public string szText;
            }
    
            //读写INI文件的API函数 
            [DllImport("kernel32", CharSet = CharSet.Auto)]
            private static extern long WritePrivateProfileString(string section, string key, string val, string filePath);
    
            [DllImport("kernel32", CharSet = CharSet.Auto)]
            private static extern int GetPrivateProfileString(string section, string key, string def, out STRINGBUFFER retVal, int size, string filePath);
    
            //类的构造函数,传递INI档案名 
            public IniFile(string sPath)
            {
                _path = sPath;
                string path = IniPath.Substring(0, IniPath.LastIndexOf("\"));
                if (!Directory.Exists(path)) Directory.CreateDirectory(path);
                if (!File.Exists(_path)) CreateIniFile();
            }
    
            //写INI文件 
            public void IniWriteValue(string Section, string Key, string Value)
            {
                WritePrivateProfileString(Section, Key, Value, this._path);
            }
    
            //读取INI文件指定项目的数据 
            public string IniReadValue(string Section, string Key)
            {
                int i;
                STRINGBUFFER RetVal;
                i = GetPrivateProfileString(Section, Key, null, out RetVal, DATA_SIZE, this._path);
                string temp = RetVal.szText;
                return temp.Trim();
            }
    
            //读取INI文件指定项目的数据 
            public string IniReadValue(string Section, string Key, string defaultValue)
            {
                int i;
                STRINGBUFFER RetVal;
                i = GetPrivateProfileString(Section, Key, null, out RetVal, DATA_SIZE, this._path);
                string temp = RetVal.szText;
                return temp.Trim() == "" ? defaultValue : temp.Trim();
            }
    
            /// <summary>
            /// 创建INI文件
            /// </summary>
            public void CreateIniFile()
            {
                StreamWriter w = File.CreateText(_path);
                w.Write("");
                w.Flush();
                w.Close();
            }
        }
    慎于行,敏于思!GGGGGG
  • 相关阅读:
    C#yield return用法示例
    C#多线程示例
    AspNetCore.Authentication
    C#委托与事件
    按值和按引用传递参数
    基于iView的无限级菜单
    Sortable By Attribute
    未能加载文件或程序集“BLL”或它的某一个依赖项。生成此程序集的运行时比当前加载的运行时新,无法加载此程序集。
    有关导出Excel特殊字符的问题
    openFileDialog的使用
  • 原文地址:https://www.cnblogs.com/GarsonZhang/p/5421167.html
Copyright © 2011-2022 走看看