zoukankan      html  css  js  c++  java
  • 读写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;
            }
  • 相关阅读:
    LVS负载均衡原理详解和使用
    Linux---RPM和YUM
    【Codeforces】CF Round #676 (Div. 2)
    莫队学习笔记
    AtCoder Beginner Contest 187 题解
    【CodeForces】 Educational Codeforces Round 94
    【CodeForces】CF Round 649 (Div.2)
    【CodeForces】CF Round 648 (Div.2)
    【AtCoder】ABC 169
    【题解 LOJ2546「JSOI2018」潜入行动】
  • 原文地址:https://www.cnblogs.com/zhumeng1582/p/3425795.html
Copyright © 2011-2022 走看看