zoukankan      html  css  js  c++  java
  • C# 操作.ini文件

    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());
            }
        }
  • 相关阅读:
    mysql使用group by查询报错SELECT list is not in GROUP BY clause and contains nonaggregated column...解决方案
    CentOS7 使用minikube 搭建kubernetes 学习环境
    5
    4
    3
    2
    1
    8
    7
    Algorithm
  • 原文地址:https://www.cnblogs.com/zxbzl/p/3549717.html
Copyright © 2011-2022 走看看