C# 處理INI文個類 INIManager

來源:互聯網
上載者:User

using System;
using System.Collections.Generic;
using System.Runtime.InteropServices;
using System.Text;

namespace MeiDuShaV1.Business
{
 
    public class INIManager
    {

        /*原型
         * BOOL WritePrivateProfileString(
                    LPCTSTR lpAppName, //是INI檔案中的一個欄位名.
                    LPCTSTR lpKeyName,  //是lpAppName下的一個鍵名,通俗講就是變數名.
                    LPCTSTR lpString,   //是索引值,也就是變數的值,不過必須為LPCTSTR型或CString型的.
                    LPCTSTR lpFileName  //檔案路徑
         * );

         */
        [DllImport("kernel32")]
        private static extern long WritePrivateProfileString(string section, string key, string val, string filePath);

        /* DWORD GetPrivateProfileString(
                     LPCTSTR lpAppName,
                     LPCTSTR lpKeyName,
                     LPCTSTR lpDefault, //如果INI檔案中沒有前兩個參數指定的欄位名或鍵名,則將此值賦給變數.
                     LPTSTR lpReturnedString,
                     DWORD nSize,
                     LPCTSTR lpFileName
         * ); */

        [DllImport("kernel32")]
        private static extern int GetPrivateProfileString(string section, string key, string def, StringBuilder retVal, int size, string filePath);

        private string filePath;

        public INIManager(string filePath)
        {
            this.filePath = filePath;
        }

        public void Write(string section ,string key,string value)
        {
            try
            {
                WritePrivateProfileString(section, key, value, filePath);
            }
            catch(Exception ex)
            {
                throw new Exception(ex.ToString());
            }
        }

        public string Read(string section ,string key)
        {
            try
            {
                StringBuilder temp = new StringBuilder(255);
                int i = GetPrivateProfileString(section, key, string.Empty, temp, 255, filePath);
                return temp.ToString();
            }
            catch(Exception ex)
            {
               
                throw new Exception(ex.ToString());
            }
        }
    }

 


}

Code
1

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.