c#保存数据到ini

1 api函数声明

#region api函数声明
        [DllImport("kernel32")]  //-------返回0表示失败 非0为成功
        private static extern long WritePrivateProfileString(string section, string key, string val, string filePath);
        [DllImport("kernel32")]  //-------返回取得字符串缓冲区的长度
        private static extern long GetPrivateProfileString(string section, string key, string def,StringBuilder retVal, int size, string filePath);
 #endregion

2 写入到ini文件

#region 写ini文件
        ///
        ///ini文件中的节名
        ///ini 文件中的健
        ///要写入该健所对应的值
        ///ini文件路径
        ///
        public static bool WriteIniData(string Section, string key, string val, string inifilePath)
        {
            if (File.Exists(inifilePath))
            {
                long opSt = WritePrivateProfileString(Section, key, val, inifilePath);
                if (opSt == 0)
                {
                    return false;
                }
                else
                {
                    return true;
                }
            }
            else
            {
                CreateFile(inifilePath);
                long opSt = WritePrivateProfileString(Section, key, val, inifilePath);
                if (opSt == 0)
                {
                    return false;
                }
                else
                {
                    return true;
                }
            }
        }
#endregion

3 读取ini文件

#region  读取ini文件
        /// 
        /// 
        /// 
        /// 节点名称
        /// 对应的key
        /// 读不到值时返回的默认值
        /// 文件路径
        /// 
        public static string ReadIniData(string section, string key, string noText, string iniFilePath)
        {
            if (File.Exists(iniFilePath))
            {
                StringBuilder temp = new StringBuilder(1024);
                long k = GetPrivateProfileString(section, key, noText, temp, 1024, iniFilePath);
                if (k != 0)
                {
                    return temp.ToString();
                }
                else
                {
                    return string.Empty;
                }
            }
            else
            {
                return string.Empty;
            }
        }
#endregion

4 简单的调用

#region   把密码写入ini文件
        public bool savePwdToIni(string pwd)
        {
            string path = System.AppDomain.CurrentDomain.BaseDirectory;
            path += "\\" + "ini" + "\\Password.ini";

            bool b = WriteIniData("Section_1","pwd",pwd,path);

            return b;
        }
#endregion

#region   从ini读取密码
        public string readPwdFromIni()
        {
            string path = System.AppDomain.CurrentDomain.BaseDirectory;
            path += "\\" + "ini" + "\\Password.ini";

            string s = ReadIniData("Section_1","pwd","",path);

            return s;
        }
#endregion

5 创建文件

#region   创建文件
        public static void CreateFile(string path)
        {
            if (!string.IsNullOrEmpty(path))
            {
                try
                {
                    string dr = Path.GetDirectoryName(path);

                    if (!Directory.Exists(dr))
                    {
                        Directory.CreateDirectory(dr);
                    }
                    if (!File.Exists(path))
                    {
                        FileStream fs = File.Create(path);

                        fs.Close();
                    }
                }
                catch (Exception e)
                {
                    Log.LogHelper.Error("CreateFile()" + e.ToString());
                }
            }
        }
#endregion

 

 

 

 

你可能感兴趣的:(c#,c#)