Esempio n. 1
0
    /// <summary>
    /// 讀取INI文件中指定KEY的字符串型值
    /// </summary>
    /// <param name="iniFile">Ini文件</param>
    /// <param name="section">節點名稱</param>
    /// <param name="key">鍵名稱</param>
    /// <param name="defaultValue">如果沒此KEY所使用的默認值</param>
    /// <returns>讀取到的值</returns>
    public static string INIGetStringValue(string iniFile, string section, string key, string defaultValue)
    {
        string    value = defaultValue;
        const int SIZE  = 1024 * 10;

        if (string.IsNullOrEmpty(section))
        {
            throw new ArgumentException("必須指定節點名稱", "section");
        }

        if (string.IsNullOrEmpty(key))
        {
            throw new ArgumentException("必須指定鍵名稱(key)", "key");
        }

        StringBuilder sb            = new StringBuilder(SIZE);
        uint          bytesReturned = INIOperationClass.GetPrivateProfileString(section, key, defaultValue, sb, SIZE, iniFile);

        if (bytesReturned != 0)
        {
            value = sb.ToString();
        }
        sb = null;

        return(value);
    }
Esempio n. 2
0
    /// <summary>
    /// 獲取INI文件中指定節點(Section)中的所有條目的Key列表
    /// </summary>
    /// <param name="iniFile">Ini文件</param>
    /// <param name="section">節點名稱</param>
    /// <returns>如果沒有內容,反回string[0]</returns>
    public static string[] INIGetAllItemKeys(string iniFile, string section)
    {
        string[]  value = new string[0];
        const int SIZE  = 1024 * 10;

        if (string.IsNullOrEmpty(section))
        {
            throw new ArgumentException("必須指定節點名稱", "section");
        }

        char[] chars         = new char[SIZE];
        uint   bytesReturned = INIOperationClass.GetPrivateProfileString(section, null, null, chars, SIZE, iniFile);

        if (bytesReturned != 0)
        {
            value = new string(chars).Split(new char[] { '\0' }, StringSplitOptions.RemoveEmptyEntries);
        }
        chars = null;

        return(value);
    }