用C#读取,写入ini文件

       [DllImport("kernel32.dll")]
        private static extern bool WritePrivateProfileString(string section, string key, string val, string filePath);
        [DllImport("kernel32.dll")]
        private static extern int GetPrivateProfileString(string section, string key, string def, StringBuilder retVal, int size, string filePath);
        private string sPath_ = "";
        /// <summary>
        /// 绝对路径
        /// </summary>
        public string sPath
        {
            get { return sPath_; }
            set { sPath_ = value; }
        }

        /// <summary>
        /// 向ini文件进行写入
        /// </summary>
        /// <param name="section">文件域</param>
        /// <param name="key">key值</param>
        /// <param name="value">value值</param>
        /// <returns></returns>
        public bool WriteToIni(string section, string key, string value)
        {
            bool result = false;
            if (System.IO.File.Exists(sPath))
            {
                result = WritePrivateProfileString(section, key, value, sPath);
            }
            return result;
        }

        /// <summary>
        /// 从ini文件进行读取
        /// </summary>
        /// <param name="section">文件域</param>
        /// <param name="key">key值</param>
        /// <param name="defaultValue">默认值</param>
        /// <returns></returns>
        public string ReadFromIni(string section, string key, string defaultValue = "")
        {
            string result = defaultValue;
            if (System.IO.File.Exists(sPath))
            {
                StringBuilder sTemp = new StringBuilder(1024);
                int i = GetPrivateProfileString(section, key, defaultValue, sTemp, 1024, sPath);
                result = sTemp.ToString();
            }
            return result;
        }

强烈注意:

ini文件必须为ANSI、Unicode 格式,否则会出现异常,至于原因暂时没有找到。请大侠帮忙解释。

时间: 2024-07-30 11:38:17

用C#读取,写入ini文件的相关文章

C语言实现 读取写入ini文件实现(转)

#include <stdio.h> #include <string.h> /* * 函数名: GetIniKeyString * 入口参数: title * 配置文件中一组数据的标识 * key * 这组数据中要读出的值的标识 * filename * 要读取的文件路径 * 返回值: 找到需要查的值则返回正确结果 * 否则返回NULL */ char *GetIniKeyString(char *title,char *key,char *filename) { FILE *f

C++ 中使用boost::property_tree读取解析ini文件

boost 官网 http://www.boost.org/ 下载页面 http://sourceforge.net/projects/boost/files/boost/1.53.0/ 我下载的是 boost_1_53_0.tar.gz 使用系统  ubuntu 12.10 一.解压 [plain] view plaincopy tar -zxvf  boost_1_53_0.tar.gz 得到一个文件夹 boost_1_53_0,  拷贝其子目录 boost 到以下路径 [plain] vi

boost::property_tree读取解析ini文件--推荐

boost::property_tree读取解析ini文件 [cpp] view plaincopy #include "stdafx.h" #include <iostream> #include <boost/property_tree/ptree.hpp> #include <boost/property_tree/ini_parser.hpp> int main() { boost::property_tree::ptree pt; boos

C++简单使用Jsoncpp来读取写入json文件

一.源码编译 C++操作json字符串最好的库应该就是jsoncpp了,开源并且跨平台.它可以从这里下载. 下载后将其解压到任意目录,它默认提供VS2003和VS2010的工程文件,使用VS2010可以直接打开makefiles\msvc2010目录下的sln文件. 工程文件提供Jsoncpp的win32和win64静态库生成.点击生成--批生成选择需要生成的配置后即可生成jsoncpp静态库.生成的文件在makefiles\msvc2010\(x64\)Debug(Release)\目录下.

CSVFileUtil 读取写入CSV文件简单工具类

参考github大神源码 总结一下最简单的工具类记录一下 /** * @description CSV文件读取和输出 工具类.<br/> * @author michael * @date 2019/05/16 * @version Copyright (c) 2019, [email protected] All Rights Reserved. */ public class CSVFileUtil { private static final Logger LOGGER = Logger

node.js同步及异步读取写入删除文件1

node.js初学中,在文件中同步及异步读取文档的过程: 1.同步读取: var fs=require("fs") //直接读取文档,并将同步返回值,赋值给变量 var data=fs.readFileSync("input.txt"); console.log(data.toString()); 2.异步读取: var fs=require("fs"); //通过回调函数返回获得的data值: fs.readFile("input.t

009-Go 读取写入CSV文件

package main import( "encoding/csv" "fmt" "os" "strconv" ) type Post struct{ Id int Content string Author string } func main(){ csvFile, err := os.Create("posts.csv") if err!= nil{ panic(err) } defer csvFi

利用GetPrivateProfileString读取ini文件的字段

//INIClass读取类 using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Runtime.InteropServices; using System.IO; using UnityEngine; namespace cReadConfigFile { public class INIClass { public string inipath; [

【转】ini文件写入 WritePrivateProfileString、GetPrivateProfileString等

转自 http://www.cnblogs.com/rosesmall/archive/2012/09/12/2681897.html ::WritePrivateProfileString( _T("Options"), _T("Name"), m_strName, pApp->m_szIni ); INI文件编程,WINAPI函数WritePrivateProfileString,GetPrivateProfileString 收藏    在我们写的程序当