zoukankan      html  css  js  c++  java
  • C# 处理INI文个类 INIManager

    using System;
    using System.Collections.Generic;
    using System.Runtime.InteropServices;
    using System.Text;

    namespace MeiDuShaV1.Business
    {
     
        public class INIManager
        {

            /*原型
             * BOOL WritePrivateProfileString(
                        LPCTSTR lpAppName, //是INI文件中的一个字段名.
                        LPCTSTR lpKeyName,  //是lpAppName下的一个键名,通俗讲就是变量名.
                        LPCTSTR lpString,   //是键值,也就是变量的值,不过必须为LPCTSTR型或CString型的.
                        LPCTSTR lpFileName  //文件路径
             * );

             */
            [DllImport("kernel32")]
            private static extern long WritePrivateProfileString(string section, string key, string val, string filePath);

            /* DWORD GetPrivateProfileString(
                         LPCTSTR lpAppName,
                         LPCTSTR lpKeyName,
                         LPCTSTR lpDefault, //如果INI文件中没有前两个参数指定的字段名或键名,则将此值赋给变量.
                         LPTSTR lpReturnedString,
                         DWORD nSize,
                         LPCTSTR lpFileName
             * ); */

            [DllImport("kernel32")]
            private static extern int GetPrivateProfileString(string section, string key, string def, StringBuilder retVal, int size, string filePath);

            private string filePath;

            public INIManager(string filePath)
            {
                this.filePath = filePath;
            }

            public void Write(string section ,string key,string value)
            {
                try
                {
                    WritePrivateProfileString(section, key, value, filePath);
                }
                catch(Exception ex)
                {
                    throw new Exception(ex.ToString());
                }
            }

            public string Read(string section ,string key)
            {
                try
                {
                    StringBuilder temp = new StringBuilder(255);
                    int i = GetPrivateProfileString(section, key, string.Empty, temp, 255, filePath);
                    return temp.ToString();
                }
                catch(Exception ex)
                {
                   
                    throw new Exception(ex.ToString());
                }
            }
        }

     


    }

    Code
  • 相关阅读:
    苹果和Google应该如何把二维码变成主流 | 36氪
    成绩换offer,中国版的Smarterer“一问一答”网站帮你把简历推荐给你想去的公司 | 36氪
    读过的一些书
    扫描QR码即可完成移动支付的LevelUp推出集合NFC、QR码等技术的移动支付终端,供商家免费使用 | 36氪
    “消息速递”团队推出“有声照片”,让照片同时拥有拍摄时的现场录音 | 36氪
    css3ps—ps直接生成css3 使用方法
    Google收购的Nik Software将会发力“服务器端图片处理”领域 | 36氪
    收藏本站
    让屏幕抖动一阵
    全中文日期显示
  • 原文地址:https://www.cnblogs.com/wubiyu/p/1015208.html
Copyright © 2011-2022 走看看