コード例 #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 = INIOperationHelper.GetPrivateProfileString(section, key, defaultValue, sb, SIZE, iniFile);

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

        return(value);
    }
コード例 #2
0
    /// <summary>
    /// 在INI文件中,删除指定节点中的所有内容。
    /// </summary>
    /// <param name="iniFile">INI文件</param>
    /// <param name="section">节点</param>
    /// <returns>操作是否成功</returns>
    public static bool INIEmptySection(string iniFile, string section)
    {
        if (string.IsNullOrEmpty(section))
        {
            throw new ArgumentException("必须指定节点名称", "section");
        }

        return(INIOperationHelper.WritePrivateProfileSection(section, string.Empty, iniFile));
    }
コード例 #3
0
    /// <summary>
    /// 在INI文件中,删除指定节点中的指定的键。
    /// </summary>
    /// <param name="iniFile">INI文件</param>
    /// <param name="section">节点</param>
    /// <param name="key">键</param>
    /// <returns>操作是否成功</returns>
    public static bool INIDeleteKey(string iniFile, string section, string key)
    {
        if (string.IsNullOrEmpty(section))
        {
            throw new ArgumentException("必须指定节点名称", "section");
        }

        if (string.IsNullOrEmpty(key))
        {
            throw new ArgumentException("必须指定键名称", "key");
        }

        return(INIOperationHelper.WritePrivateProfileString(section, key, null, iniFile));
    }
コード例 #4
0
    /// <summary>
    /// 在INI文件中,将指定的键值对写到指定的节点,如果已经存在则替换
    /// </summary>
    /// <param name="iniFile">INI文件</param>
    /// <param name="section">节点,如果不存在此节点,则创建此节点</param>
    /// <param name="items">键值对,多个用\0分隔,形如key1=value1\0key2=value2</param>
    /// <returns></returns>
    public static bool INIWriteItems(string iniFile, string section, string items)
    {
        if (string.IsNullOrEmpty(section))
        {
            throw new ArgumentException("必须指定节点名称", "section");
        }

        if (string.IsNullOrEmpty(items))
        {
            throw new ArgumentException("必须指定键值对", "items");
        }

        return(INIOperationHelper.WritePrivateProfileSection(section, items, iniFile));
    }
コード例 #5
0
ファイル: LoggerHelper.cs プロジェクト: thinhils/Nikita
        /// <summary>获取日志存储库连接字符串
        ///
        /// </summary>
        /// <returns></returns>
        public static string GetConn()
        {
            string conn = string.Empty;

            if (!string.IsNullOrEmpty(StaticInfoHelper.ConnString))
            {
                conn = StaticInfoHelper.ConnString;
            }
            else
            {
                string strFilePath = Application.StartupPath + "\\log.ini";
                if (File.Exists(strFilePath))
                {
                    conn = INIOperationHelper.INIGetStringValue(strFilePath, "LogConnection", "Connection", null);
                    conn = DESEncryptHelper.Decrypt(conn, "test332211");
                    StaticInfoHelper.ConnString = conn;
                }
            }

            return(conn);
        }
コード例 #6
0
ファイル: LoggerHelper.cs プロジェクト: thinhils/Nikita
        /// <summary>获取日志存储类型
        ///
        /// </summary>
        /// <returns></returns>
        public static string GetLogType()
        {
            //string val = ConfigurationManager.ConnectionStrings["LogConnection"].ProviderName;
            //return val;
            string val = string.Empty;

            if (!string.IsNullOrEmpty(StaticInfoHelper.LogType))
            {
                val = StaticInfoHelper.LogType;
            }
            else
            {
                string strFilePath = Application.StartupPath + "\\log.ini";
                if (File.Exists(strFilePath))
                {
                    val = INIOperationHelper.INIGetStringValue(strFilePath, "LogType", "Type", null);
                    StaticInfoHelper.LogType = val;
                }
            }
            return(val);
        }
コード例 #7
0
    /// <summary>
    /// 在INI文件中,指定节点写入指定的键及值。如果已经存在,则替换。如果没有则创建。
    /// </summary>
    /// <param name="iniFile">INI文件</param>
    /// <param name="section">节点</param>
    /// <param name="key">键</param>
    /// <param name="value">值</param>
    /// <returns>操作是否成功</returns>
    public static bool INIWriteValue(string iniFile, string section, string key, string value)
    {
        if (string.IsNullOrEmpty(section))
        {
            throw new ArgumentException("必须指定节点名称", "section");
        }

        if (string.IsNullOrEmpty(key))
        {
            throw new ArgumentException("必须指定键名称", "key");
        }

        if (value == null)
        {
            throw new ArgumentException("值不能为null", "value");
        }

        bool flag = INIOperationHelper.WritePrivateProfileString(section, key, value, iniFile);

        return(flag);
    }
コード例 #8
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 = INIOperationHelper.GetPrivateProfileString(section, null, null, chars, SIZE, iniFile);

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

        return(value);
    }
コード例 #9
0
    /// <summary>
    /// 获取INI文件中指定节点(Section)中的所有条目(key=value形式)
    /// </summary>
    /// <param name="iniFile">Ini文件</param>
    /// <param name="section">节点名称</param>
    /// <returns>指定节点中的所有项目,没有内容返回string[0]</returns>
    public static string[] INIGetAllItems(string iniFile, string section)
    {
        //返回值形式为 key=value,例如 Color=Red
        uint MAX_BUFFER = 32767;        //默认为32767

        string[] items = new string[0]; //返回值

        //分配内存
        IntPtr pReturnedString = Marshal.AllocCoTaskMem((int)MAX_BUFFER * sizeof(char));

        uint bytesReturned = INIOperationHelper.GetPrivateProfileSection(section, pReturnedString, MAX_BUFFER, iniFile);

        if (!(bytesReturned == MAX_BUFFER - 2) || (bytesReturned == 0))
        {
            string returnedString = Marshal.PtrToStringAuto(pReturnedString, (int)bytesReturned);
            items = returnedString.Split(new char[] { '\0' }, StringSplitOptions.RemoveEmptyEntries);
        }

        Marshal.FreeCoTaskMem(pReturnedString);     //释放内存

        return(items);
    }
コード例 #10
0
        /// <summary>创建本地配置文件
        /// 创建本地配置文件
        /// </summary>
        private void CreateIniFile()
        {
            string filePath = Application.StartupPath + "\\" + "Log.ini";

            if (!File.Exists(filePath))
            {
                File.Create(filePath).Close();
            }
            else
            {
                File.Delete(filePath);
                File.Create(filePath).Close();
            }
            if (cboLogType.Text.Trim().ToLower() == "sqlite")
            {
                INIOperationHelper.INIWriteValue(filePath, "LogConnection", "Connection", DESEncryptHelper.Encrypt("LogLocalDB", "test332211"));
            }
            else
            {
                INIOperationHelper.INIWriteValue(filePath, "LogConnection", "Connection", txtConnection.Text.Trim());
            }
            INIOperationHelper.INIWriteValue(filePath, "LogType", "Type", cboLogType.Text.Trim().ToLower() + "log");
        }
コード例 #11
0
    /// <summary>
    /// 读取INI文件中指定INI文件中的所有节点名称(Section)
    /// </summary>
    /// <param name="iniFile">Ini文件</param>
    /// <returns>所有节点,没有内容返回string[0]</returns>
    public static string[] INIGetAllSectionNames(string iniFile)
    {
        uint MAX_BUFFER = 32767;           //默认为32767

        string[] sections = new string[0]; //返回值

        //申请内存
        IntPtr pReturnedString = Marshal.AllocCoTaskMem((int)MAX_BUFFER * sizeof(char));
        uint   bytesReturned   = INIOperationHelper.GetPrivateProfileSectionNames(pReturnedString, MAX_BUFFER, iniFile);

        if (bytesReturned != 0)
        {
            //读取指定内存的内容
            string local = Marshal.PtrToStringAuto(pReturnedString, (int)bytesReturned).ToString();

            //每个节点之间用\0分隔,末尾有一个\0
            sections = local.Split(new char[] { '\0' }, StringSplitOptions.RemoveEmptyEntries);
        }

        //释放内存
        Marshal.FreeCoTaskMem(pReturnedString);

        return(sections);
    }