专注于互联网--专注于架构

最新标签
网站地图
文章索引
Rss订阅

首页 »DotNet » 读写ini文件:C#读写ini文件 »正文

读写ini文件:C#读写ini文件

来源: 发布时间:星期五, 2009年1月9日 浏览:18次 评论:0
  主要思路是Win32 API

  1.引入命名空间

  using .Runtime.InteropServices;

  2.声明(把个Win32 API转成C#)

    //声明INI文件写操作 WritePrivateProfileString
    [DllImport("kernel32")]
    private extern long WritePrivateProfileString( section, key, val, filePath);
    //声明INI文件读操作 GetPrivateProfileString
    [DllImport("kernel32")]
    private extern GetPrivateProfileString( section, key, def, StringBuilder retVal, size, filePath);


  3.

//写个config.ini文件
private void Save_Ini
    {
       s = .Windows.Forms.Application.ExecutablePath;
      //在当前目录下个config.ini文件
       path = s.ToLower.Replace("mediaconvert.exe", "config.ini");
       configureNode = "DataBaseConfigure";//配置节
       key1 = "DataBase";//键名
       key1_Value = "DataBaseName";//键值
       key2 = "Server";
       key2_Value = "ServerName";
       key3 = "UserId";
       key3_Value = "1";
      WritePrivateProfileString(configureNode, key1, key1_Value, path);
      WritePrivateProfileString(configureNode, key2, key2_Value, path);
      WritePrivateProfileString(configureNode, key3, key3_Value, path);
      /*最后在exe文件同目录下生成个config.ini文件内容应如下:
       * [DataBaseConfigure]
       * DataBase=DataBaseName
       * Server=ServerName
       * UserId=1
       */
    }
//读取config.ini文件中配置
private void Read_Ini
    {
       s = .Windows.Forms.Application.ExecutablePath;
      //取得config.ini路径
       path = s.ToLower.Replace("mediaconvert.exe", "config.ini");
      StringBuilder str = StringBuilder(255);
      //取得配置节[DataBaseConfigure]DataBase键
      GetPrivateProfileString("DataBaseConfigure", "DataBase", "", str, 255, path);
      //对话框中结果应该为 DataBase:DataBaseName
      .Windows.Forms.MessageBox.Show("DataBase:" + str.);
    }


  C#使用系统Api最头疼问题就是Api中数据类型在C#中如何对应问题

  这个网站WebSitehttp://www.pinvoke.net/列出了大多系统Api在此C#或VB.Net中对应声明很详细

相关文章

读者评论

  • 共0条 分0页

发表评论

  • 昵称:
  • 内容: