A.INI文件内容:
[DataBase]
Server=127.0.0.1User=saPassword=123-------------------------------
定义共用方法
public class OperatorFile { [DllImport("kernel32")] //引入“shell32.dll”API文件 public static extern int GetPrivateProfileString(string section, string key, string def, StringBuilder retVal, int size, string filePath); ////// 从INI文件中读取指定节点的内容 /// /// INI节点 /// 节点下的项 /// 没有找到内容时返回的默认值 /// 要读取的INI文件 ///读取的节点内容 public static string GetIniFileString(string section, string key, string def, string filePath) { StringBuilder temp = new StringBuilder(1024); GetPrivateProfileString(section, key, def, temp, 1024, filePath); return temp.ToString(); } }
----------------
调用:
string strServer = OperatorFile.GetIniFileString("DataBase", "Server", "", Application.StartupPath + "\\A.ini"); //获取登录用户 string strUserID = OperatorFile.GetIniFileString("DataBase", "User", "", Application.StartupPath + "\\Aini"); //获取登录密码 string strPwd = OperatorFile.GetIniFileString("DataBase", "Password", "", Application.StartupPath + "\\A.ini");