• c# 操作ini文件


     
    OperateIniFile.cs

    using System;
    using System.Collections.Generic;
    using System.IO;
    using System.Linq;
    using System.Runtime.InteropServices;
    using System.Text;
    using System.Threading.Tasks;
    
    namespace AutoS
    {
        class OperateIniFile
        {
            #region API函数声明
    
            [DllImport("kernel32")]//返回0表示失败,非0为成功
            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);
    
    
            #endregion
    
           #region 读Ini文件
    
            public static string ReadIniData(string Section, string Key, string NoText, string iniFilePath)
            {
                if (File.Exists(iniFilePath))
                {
                    StringBuilder temp = new StringBuilder(1024);
                    GetPrivateProfileString(Section, Key, NoText, temp, 1024, iniFilePath);
                    return temp.ToString();
                }
                else
                {
                    return String.Empty;
                }
            }
    
            #endregion
    
            #region 写Ini文件
    
            public static bool WriteIniData(string Section, string Key, string Value, string iniFilePath)
            {
                if (File.Exists(iniFilePath))
                {
                    long OpStation = WritePrivateProfileString(Section, Key, Value, iniFilePath);
                    if (OpStation == 0)
                    {
                        return false;
                    }
                    else
                    {
                        return true;
                    }
                }
                else
                {
                    return false;
                }
            }
    
            #endregion   
        }       
     

    config.ini

    [setting]
    curpath =E:\MyProject\Star_Lion\test001
    
    
    [database]
    server = "192.168.1.1"
    ports = [ 8001, 8001, 8002 ]
    connection_max = 5000
    enabled = true
  • 相关阅读:
    hadoop配置文件说明
    hadoop集群部署
    Linux iptables防火墙实用模板
    不容忽视的SQL注入式攻击
    字符串的加解密
    .NET程序集版本号的奥秘
    SQL Server 2005 数据库引擎的容量规范
    Microsoft Visual Studio Tools for the Microsoft Office system(3.0 版)
    How to: Display a Dialog Box for Selecting Entries from the Contacts Folder
    OpenID及其原理介绍
  • 原文地址:https://www.cnblogs.com/carl2380/p/15899971.html
Copyright © 2020-2023  润新知