• 读写Ini文件


    // 声明INI文件的写操作函数 WritePrivateProfileString()
            [System.Runtime.InteropServices.DllImport("kernel32")]
            private static extern long WritePrivateProfileString(string section, string key, string val, string filePath);
    
            // 声明INI文件的读操作函数 GetPrivateProfileString()
            [System.Runtime.InteropServices.DllImport("kernel32")]
            private static extern int GetPrivateProfileString(string section, string key, string def, System.Text.StringBuilder retVal, int size, string filePath);
            public static string WriteValue(string section, string key, string value)
            {
                string sPath = getIniFilePath();
    
                long i = WritePrivateProfileString(section, key, value, sPath);
                if (i == 0)
                    return "fail";
                else
                    return "success";
    
            }
            public static string ReadValue(string section, string key)
            {
                string sPath = getIniFilePath();
                StringBuilder temp = new StringBuilder(255);
                int i = GetPrivateProfileString(section, key, "", temp, 255, sPath);
                if (i == 0)
                    return "fail";
                else
                    return temp.ToString();
            }
    
            private static string getIniFilePath()
            {
                string sPath = null;
                if (System.Environment.CurrentDirectory == AppDomain.CurrentDomain.BaseDirectory)//windows应用程序则相等
                    sPath = AppDomain.CurrentDomain.BaseDirectory;
                else
                    sPath = AppDomain.CurrentDomain.BaseDirectory + @"config.ini";
                return sPath;
            }
  • 相关阅读:
    python正则表达式
    正则表达式
    python装饰器
    冒泡排序算法与递归
    C语言typedef定义结构体数组,下面这段代码是什么意思?
    链表实现的简单循环队列
    数组实现的简单循环队列
    悬空指针
    NULL代表什么
    Unity学习——Network Transform和 Network Transform Child组件
  • 原文地址:https://www.cnblogs.com/zhumeng1582/p/3425795.html
Copyright © 2020-2023  润新知