Rumah > 类库下载 > C#类库 > teks badan

C#创建ini文件、读取值、修改值

大家讲道理
Lepaskan: 2016-11-10 09:21:55
asal
3488 orang telah melayarinya

调用方法:

int iBaudRate = InIHelper.ReadConfig("COM_SETUP", "波特率")

InIHelper类:

public class InIHelper
    {
        private static string FileName = Application.StartupPath + "\\AppConfig.ini";
        /// <summary>
        /// 读取配置文件
        /// </summary>
        /// <typeparam name="T"></typeparam>
        /// <param name="section"></param>
        /// <param name="key"></param>
        /// <returns></returns>
        public static T ReadConfig<T>(string section, string key)
        {
            if (File.Exists(FileName))
            {
                IniFile f = new IniFile(FileName);
                string value = f.ReadContentValue(section, key);
 
                if (String.IsNullOrWhiteSpace(value))
                    return default(T);
 
                if (typeof(T).IsEnum)
                    return (T)Enum.Parse(typeof(T), value, true);
 
                return (T)Convert.ChangeType(value, typeof(T));
            }
            else
            {
                return default(T);
            }
        }
 
        /// <summary>
        /// 写配置文件
        /// </summary>
        /// <param name="section"></param>
        /// <param name="key"></param>
        /// <param name="value"></param>
        public static void WriteConfig(string section, string key, string value)
        {
            //如果文件不存在,则创建
            if (!File.Exists(FileName))
            {
                using (FileStream myFs = new FileStream(FileName, FileMode.Create)) { }
            }
 
            IniFile f = new IniFile(FileName);
            f.WriteContentValue(section, key, value);
        }
    }
Salin selepas log masuk

核心代码:

public class IniFile
    {
        public string Path;
 
        public IniFile(string path)
        {
            this.Path = path;
        }
 
        /// <summary>
        /// 写入INI文件
        /// </summary>
        /// <param name="section">节点名称[如[TypeName]]</param>
        /// <param name="key">键</param>
        /// <param name="val">值</param>
        /// <param name="filepath">文件路径</param>
        /// <returns></returns>
        [DllImport("kernel32")]
        private static extern long WritePrivateProfileString(string section, string key, string val, string filepath);
 
        /// <summary>
        /// 读取INI文件
        /// </summary>
        /// <param name="section">节点名称</param>
        /// <param name="key">键</param>
        /// <param name="def">值</param>
        /// <param name="retval">stringbulider对象</param>
        /// <param name="size">字节大小</param>
        /// <param name="filePath">文件路径</param>
        /// <returns></returns>
        [DllImport("kernel32")]
        private static extern int GetPrivateProfileString(string section, string key, string def, StringBuilder retval, int size, string filePath);
 
        /// <summary>
        /// 写入
        /// </summary>
        /// <param name="section"></param>
        /// <param name="key"></param>
        /// <param name="iValue"></param>
        public void WriteContentValue(string section, string key, string iValue)
        {
            WritePrivateProfileString(section, key, iValue, this.Path);
        }
 
        /// <summary>
        /// 读取INI文件中的内容方法
        /// </summary>
        /// <param name="Section">键</param>
        /// <param name="key">值</param>
        /// <returns></returns>
        public string ReadContentValue(string Section, string key)
        {
            StringBuilder temp = new StringBuilder(1024);
            GetPrivateProfileString(Section, key, "", temp, 1024, this.Path);
            return temp.ToString();
        }
    }
Salin selepas log masuk


sumber:php.cn
Kenyataan Laman Web ini
Kandungan artikel ini disumbangkan secara sukarela oleh netizen, dan hak cipta adalah milik pengarang asal. Laman web ini tidak memikul tanggungjawab undang-undang yang sepadan. Jika anda menemui sebarang kandungan yang disyaki plagiarisme atau pelanggaran, sila hubungi admin@php.cn
Artikel terbaru oleh pengarang
Tutorial Popular
Lagi>
Muat turun terkini
Lagi>
kesan web
Kod sumber laman web
Bahan laman web
Templat hujung hadapan