写入DLL文件

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using System.Runtime.InteropServices;

namespace WindowsApplication1
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}


[DllImport("kernel32")]
private static extern int GetPrivateProfileString
(string section, //欲在其中查找条目的小节名称。这个字串不区分大小写。如设为vbNullString,就在lpReturnedString缓冲区内装载这个ini文件所有小节的列表
string key, //欲获取的项名或条目名。这个字串不区分大小写。如设为vbNullString,就在lpReturnedString缓冲区内装载指定小节所有项的列表
string def, //指定的条目没有找到时返回的默认值。可设为空("")
StringBuilder retVal, //指定一个字串缓冲区,长度至少为nSize
int size, //指定装载到lpReturnedString缓冲区的最大字符数量
string filePath);//初始化文件的名字。如没有指定一个完整路径名,windows就在Windows目录中查找文件
/*
例子:
*
* section: [111]
* key: 123=huze
* huze是键入的值
*/
[DllImport("kernel32")]
private static extern long WritePrivateProfileString(
string section, //要在其中写入新字串的小节名称。这个字串不区分大小写
string key, //要设置的项名或条目名。这个字串不区分大小写。用vbNullString可删除这个小节的所有设置项
string val, //指定为这个项写入的字串值。用vbNullString表示删除这个项现有的字串
string filePath);//初始化文件的名字。如果没有指定完整路径名,则windows会在windows目录查找文件。如果文件没有找到,则函数会创建它
private void button1_Click(object sender, EventArgs e)
{
textBox1.Text = ReadIniReg("111", "123");
}
public static string ReadIniReg(string Section, string Key)
{
StringBuilder retVal = new StringBuilder(0xff);
GetPrivateProfileString(Section, Key, "", retVal, 0xff, Application.StartupPath + "\\123.dll");
return retVal.ToString();
}
public static void WriteintReg(string Section, string Key, string Value)
{
WritePrivateProfileString(Section, Key, Value, Application.StartupPath + "\\123.dll");
}

private void button2_Click(object sender, EventArgs e)
{
WriteintReg("111", "123", this.textBox2.Text);
WriteintReg("222", "234", this.textBox3.Text);
/* [111]
* 123=huze
* [222]
* 234=mengzi
*/
}
}
}

 

posted @ 2011-01-10 12:12  橘子西瓜  阅读(935)  评论(3编辑  收藏  举报