最好详细写下`有个大体框架

解决方案 »

  1.   

            #region DLL
            [DllImport("kernel32")]
            private static extern bool GetProfileString(string lpApplicationName, string lpKeyName, string lpDefault, StringBuilder lpReturnedString, int nSize);
            [DllImport("kernel32")]
            private static extern bool WriteProfileString(string lpApplicationName, string lpKeyName, string lpString);
            #endregion        private void button1_Click(object sender, EventArgs e)
            {
                //写入Win.ini文件
                try
                {
                    WriteProfileString("演示程序", "软件名称", this.textBox1.Text);
                    WriteProfileString("演示程序", "著作权人", this.textBox2.Text);
                    WriteProfileString("演示程序", "版本号", this.textBox3.Text);
                    MessageBox.Show("写入Win.ini文件操作成功!", "信息提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
                }
                catch (Exception Err)
                {
                    MessageBox.Show("写入Win.ini文件操作失败!" + Err.Message, "信息提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
                }
            }        private void button2_Click(object sender, EventArgs e)
            {
                //读取Win.ini文件
                StringBuilder MyString = new StringBuilder(256);
                GetProfileString("演示程序", "软件名称", "", MyString, 256);
                this.textBox1.Text = MyString.ToString();
                GetProfileString("演示程序", "著作权人", "", MyString, 256);
                this.textBox2.Text = MyString.ToString();
                GetProfileString("演示程序", "版本号", "", MyString, 256);
                this.textBox3.Text = MyString.ToString();
            }
      

  2.   

    [DllImport("kernel32")]
    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);
    private static void Wini(string Section, string Key, string Val, string FilePath)
    {
    WritePrivateProfileString(Section, Key, Val, FilePath);
    }
    private static string Rini(string Section, string Key, string Val, string FilePath)
    {
    int X = Convert.ToInt32(Val);
    StringBuilder Str = new StringBuilder(X);
    GetPrivateProfileString(Section, Key, "Error", Str, X, FilePath);
    return Str.ToString();
    }
      

  3.   

    using System;
    using System.Collections.Generic;
    using System.Text;
    using System.Runtime.InteropServices;
    using System.IO;
    using System.Security.Cryptography; namespace INI{
        public class INIHelper
        {
            private string strKey = "CSDN@#*&";
            public string inipath;
            [DllImport("kernel32")]
            private static extern long WritePrivateProfileString(string section, string key, string val, string filePath);
            [DllImport("kernel32")]
            private static extern int GetPrivateProfileString(string section, string key, string def, StringBuilder retVal, int size, string filePath);
            /// <summary> 
            /// 构造方法 
            /// </summary> 
            /// <param name="INIPath">文件路径</param> 
            public INIClass(string INIPath)
            {
                inipath = INIPath;
            }
            /// <summary> 
            /// 写入INI文件 
            /// </summary> 
            /// <param name="Section">项目名称(如 [TypeName] )</param> 
            /// <param name="Key">键</param> 
            /// <param name="Value">值</param> 
            public void IniWriteValue(string Section, string Key, string Value)
            {
                WritePrivateProfileString(Section, Key, Value, this.inipath);
            }        /// <summary> 
            /// Md5加密写入INI文件 
            /// </summary> 
            /// <param name="Section">项目名称(如 [TypeName] )</param> 
            /// <param name="Key">键</param> 
            /// <param name="Value">值</param> 
            public void IniMd5WriteValue(string Section, string Key, string Value)
            {
                string str = MD5Encrypt(Value, strKey);
                WritePrivateProfileString(Section, Key, str, this.inipath);
            }        /// <summary> 
            /// 读出INI文件 
            /// </summary> 
            /// <param name="Section">项目名称(如 [TypeName] )</param> 
            /// <param name="Key">键</param> 
            public string IniReadValue(string Section, string Key)
            {
                StringBuilder temp = new StringBuilder(500);
                int i = GetPrivateProfileString(Section, Key, "", temp, 500, this.inipath);
                return temp.ToString();
            }        /// <summary> 
            /// 读出INI文件 
            /// </summary> 
            /// <param name="Section">项目名称(如 [TypeName] )</param> 
            /// <param name="Key">键</param> 
            public string IniMd5ReadValue(string Section, string Key)
            {
                StringBuilder temp = new StringBuilder(500);
                int i = GetPrivateProfileString(Section, Key, "", temp, 500, this.inipath);
                string str = temp.ToString();            return MD5Decrypt(str, strKey);
            }        /// <summary> 
            /// 验证文件是否存在 
            /// </summary> 
            /// <returns>布尔值</returns> 
            public bool ExistINIFile()
            {
                return File.Exists(inipath);
            }        ///MD5加密
            public string MD5Encrypt(string pToEncrypt, string sKey)
            {
                DESCryptoServiceProvider des = new DESCryptoServiceProvider();
                byte[] inputByteArray = Encoding.Default.GetBytes(pToEncrypt);
                des.Key = ASCIIEncoding.ASCII.GetBytes(sKey);
                des.IV = ASCIIEncoding.ASCII.GetBytes(sKey);
                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 string MD5Decrypt(string pToDecrypt, string sKey)
            {
                DESCryptoServiceProvider des = new DESCryptoServiceProvider();            byte[] inputByteArray = new byte[pToDecrypt.Length / 2];
                for (int x = 0; x < pToDecrypt.Length / 2; x++)
                {
                    int i = (Convert.ToInt32(pToDecrypt.Substring(x * 2, 2), 16));
                    inputByteArray[x] = (byte)i;
                }            des.Key = ASCIIEncoding.ASCII.GetBytes(sKey);
                des.IV = ASCIIEncoding.ASCII.GetBytes(sKey);
                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());
            }
        }
    }
      

  4.   

     //写INI文件
      [ DllImport ( "kernel32" ) ]
      private static extern bool WritePrivateProfileString ( string section ,string key , string val , string filePath ) ; 
      //读ini文件(字符
      [ DllImport ( "kernel32" ) ]
      private static extern int GetPrivateProfileString ( string section ,string key , string def , StringBuilder retVal ,int size , string filePath ) ;   //读ini文件(数字
      [ DllImport ( "kernel32" ) ]
      private static extern int GetPrivateProfileInt 的( string section ,string key , int def , string filePath ) ;  //////////////////////////////////////////////////////////////
      using System; 
      using System.IO; 
      using System.Runtime.InteropServices; 
      using System.Text;  namespace EchonComponentLibrary
      {
      /// <summary>
      /// IniFile 的摘要说明。
      /// </summary>
      public class IniFile
      { 
      private string FFileName;   [DllImport("kernel32")] 
    private static extern int GetPrivateProfileInt( 
      string lpAppName, 
      string lpKeyName, 
      int nDefault, 
      string lpFileName 
      ); 
      [DllImport("kernel32")] 
      private static extern int GetPrivateProfileString( 
      string lpAppName, 
      string lpKeyName, 
      string lpDefault, 
      StringBuilder lpReturnedString, 
      int nSize, 
      string lpFileName 
      ); 
      [DllImport("kernel32")] 
      private static extern bool WritePrivateProfileString( 
      string lpAppName, 
      string lpKeyName, 
      string lpString, 
      string lpFileName 
      );   public IniFile(string filename) 
      { 
      FFileName = filename; 
      } 
      public int ReadInt(string section,string key,int def) 
      { 
      return GetPrivateProfileInt(section,key,def,FFileName); 
      } 
      public string ReadString(string section,string key,string def) 

      StringBuilder temp = new StringBuilder(1024); 
      GetPrivateProfileString(section,key,def,temp,1024,FFileName); 
      return temp.ToString(); 
      } 
      public void WriteInt(string section,string key,int iVal) 
      { 
      WritePrivateProfileString(section,key,iVal.ToString(),FFileName); 
      } 
      public void WriteString(string section,string key,string strVal) 
      { 
      WritePrivateProfileString(section,key,strVal,FFileName); 
      } 
      public void DelKey(string section,string key) 
      { 
      WritePrivateProfileString(section,key,null,FFileName); 
      } 
      public void DelSection(string section) 
      { 
      WritePrivateProfileString(section,null,null,FFileName); 
      }   } 
      }
      

  5.   

    现在流行的做法就是使用XML文件来保存配置信息,如果有加密需求的话,利用System.Data.Text名称空间下的加解密类配合文件流类可以实现。