1.声明变量
#region "声明变量" /// <summary> /// 写入INI文件 /// </summary> /// <param name="section">节点名称[如[TypeName]]</param> /// <param name="key">键</param> /// <param name="val">值</param> /// <param name="filepath">文件路径</param> /// <returns></returns> [DllImport("kernel32")] private static extern long WritePrivateProfileString(string section, string key, string val, string filepath); /// <summary> /// 读取INI文件 /// </summary> /// <param name="section">节点名称</param> /// <param name="key">键</param> /// <param name="def">值</param> /// <param name="retval">stringbulider对象</param> /// <param name="size">字节大小</param> /// <param name="filePath">文件路径</param> /// <returns></returns> [DllImport("kernel32")] private static extern int GetPrivateProfileString(string section, string key, string def, StringBuilder retval, int size, string filePath); private string strFilePath = Application.StartupPath + "\FileConfig.ini";//获取INI文件路径 private string strSec = ""; //INI文件名 #endregion
2.写入到ini文件
private void WriteIni() { try { //根据INI文件名设置要写入INI文件的节点名称 //此处的节点名称完全可以根据实际需要进行配置 strSec = Path.GetFileNameWithoutExtension(strFilePath); WritePrivateProfileString(strSec, "UserName", EDncrypt.MD5Encrypt(txtUser.Text.Trim()), strFilePath); WritePrivateProfileString(strSec, "Password", EDncrypt.MD5Encrypt(txtPass.Text.Trim()), strFilePath); WritePrivateProfileString(strSec, "DataBase", EDncrypt.MD5Encrypt(txtDataBase.Text.Trim()), strFilePath); WritePrivateProfileString(strSec, "Host", EDncrypt.MD5Encrypt(txtServer.Text.Trim()), strFilePath); MessageBox.Show("写入成功"); } catch (Exception ex) { MessageBox.Show(ex.Message.ToString()); } }
3.读取ini文件
private void ReadIni() { if (File.Exists(strFilePath))//读取时先要判读INI文件是否存在 { strSec = Path.GetFileNameWithoutExtension(strFilePath); txtUser.Text = ContentValue(strSec, "UserName"); txtPass.Text = ContentValue(strSec, "Password"); txtDataBase.Text = ContentValue(strSec, "DataBase"); txtServer.Text = ContentValue(strSec, "Host"); } else { MessageBox.Show("INI文件不存在"); } } /// <summary> /// 自定义读取INI文件中的内容方法 /// </summary> /// <param name="Section">键</param> /// <param name="key">值</param> /// <returns></returns> private string ContentValue(string Section, string key) { StringBuilder temp = new StringBuilder(1024); GetPrivateProfileString(Section, key, "", temp, 1024, strFilePath); return EDncrypt.MD5Decrypt(temp.ToString()); }
4.Md5加密与解密类
public class EDncrypt { private static readonly string m_strKey = "dre34ASD"; ///MD5加密 public static string MD5Encrypt(string pToEncrypt) { DESCryptoServiceProvider des = new DESCryptoServiceProvider(); byte[] inputByteArray = Encoding.Default.GetBytes(pToEncrypt); des.Key = ASCIIEncoding.ASCII.GetBytes(m_strKey); des.IV = ASCIIEncoding.ASCII.GetBytes(m_strKey); MemoryStream ms = new MemoryStream(); CryptoStream cs = new CryptoStream(ms, des.CreateEncryptor(), CryptoStreamMode.Write); cs.Write(inputByteArray, 0, inputByteArray.Length); cs.FlushFinalBlock(); StringBuilder ret = new StringBuilder(); foreach (byte b in ms.ToArray()) { ret.AppendFormat("{0:X2}", b); } ret.ToString(); return ret.ToString(); } ///MD5解密 public static string MD5Decrypt(string pToDecrypt) { DESCryptoServiceProvider des = new DESCryptoServiceProvider(); byte[] inputByteArray = new byte[pToDecrypt.Length / 2]; for (int x = 0; x < pToDecrypt.Length / 2; x++) { int i = (System.Convert.ToInt32(pToDecrypt.Substring(x * 2, 2), 16)); inputByteArray[x] = (byte)i; } des.Key = ASCIIEncoding.ASCII.GetBytes(m_strKey); des.IV = ASCIIEncoding.ASCII.GetBytes(m_strKey); MemoryStream ms = new MemoryStream(); CryptoStream cs = new CryptoStream(ms, des.CreateDecryptor(), CryptoStreamMode.Write); cs.Write(inputByteArray, 0, inputByteArray.Length); cs.FlushFinalBlock(); StringBuilder ret = new StringBuilder(); return System.Text.Encoding.Default.GetString(ms.ToArray()); } }