c# 如何写入 ini 20
我的conn.ini如下:没有标准的语法就这句,我在连接时已经可以调用DataSource=(local);Database=设备管理系统;Userid=sa;PassW...
我的 conn.ini 如下: 没有标准的语法 就这句,我在连接时已经可以调用
Data Source=(local);Database=设备管理系统;Userid=sa ;PassWord=yd20002000
我想在登陆界面的textBox 对内容进行Data Source=,Database=,等进行更换
在 private void button4_Click(object sender, EventArgs e)里面怎么写啊~ ,共四个TEXT
textBox5.Text;
textBox2.Text;
textBox3.Text;
textBox4.Text; 展开
Data Source=(local);Database=设备管理系统;Userid=sa ;PassWord=yd20002000
我想在登陆界面的textBox 对内容进行Data Source=,Database=,等进行更换
在 private void button4_Click(object sender, EventArgs e)里面怎么写啊~ ,共四个TEXT
textBox5.Text;
textBox2.Text;
textBox3.Text;
textBox4.Text; 展开
3个回答
展开全部
慢慢看,你会看明白的..
using System.Data;
using System.Runtime.InteropServices;
using System.Text;
[DllImport("kernel32")]
private static extern long WritePrivateProfileString(string section,
string key,string val,string filePath);
[DllImport("kernel32")]
private static extern int GetPrivateProfileString(string section,
string key,string def, StringBuilder retVal,
int size,string filePath);
public void IniWriteValue(string Section,string Key,string Value,string filepath)//对ini文件进行写操作的函数
{
WritePrivateProfileString(Section,Key,Value,filepath);
}
public string IniReadValue(string Section,string Key,string filepath)//对ini文件进行读操作的函数
{
StringBuilder temp = new StringBuilder(255);
int i = GetPrivateProfileString(Section,Key,"",temp,
255, filepath);
return temp.ToString();
}
private void button1_Click(object sender, System.EventArgs e)
{
this.txtHost.Text= IniReadValue("Connection Configure","Host","c:/demo.ini");
this.txtPort.Text= IniReadValue("Connection Configure","Port","c:/demo.ini");
this.txtDN.Text= IniReadValue("Connection Configure","DN","c:/demo.ini");
}
private void button2_Click(object sender, System.EventArgs e)
{
IniWriteValue("Connection Configure","Host",this.txtHost.Text,"c:/demo.ini");
IniWriteValue("Connection Configure","Port",this.txtPort.Text,"c:/demo.ini");
IniWriteValue("Connection Configure","DN",this.txtDN.Text,"c:/demo.ini");
}
/*可以通过调用kernel32.dll中的两个api:WritePrivateProfileString,GetPrivateProfileString
来实现对ini 文件的读些。*/
using System.Data;
using System.Runtime.InteropServices;
using System.Text;
[DllImport("kernel32")]
private static extern long WritePrivateProfileString(string section,
string key,string val,string filePath);
[DllImport("kernel32")]
private static extern int GetPrivateProfileString(string section,
string key,string def, StringBuilder retVal,
int size,string filePath);
public void IniWriteValue(string Section,string Key,string Value,string filepath)//对ini文件进行写操作的函数
{
WritePrivateProfileString(Section,Key,Value,filepath);
}
public string IniReadValue(string Section,string Key,string filepath)//对ini文件进行读操作的函数
{
StringBuilder temp = new StringBuilder(255);
int i = GetPrivateProfileString(Section,Key,"",temp,
255, filepath);
return temp.ToString();
}
private void button1_Click(object sender, System.EventArgs e)
{
this.txtHost.Text= IniReadValue("Connection Configure","Host","c:/demo.ini");
this.txtPort.Text= IniReadValue("Connection Configure","Port","c:/demo.ini");
this.txtDN.Text= IniReadValue("Connection Configure","DN","c:/demo.ini");
}
private void button2_Click(object sender, System.EventArgs e)
{
IniWriteValue("Connection Configure","Host",this.txtHost.Text,"c:/demo.ini");
IniWriteValue("Connection Configure","Port",this.txtPort.Text,"c:/demo.ini");
IniWriteValue("Connection Configure","DN",this.txtDN.Text,"c:/demo.ini");
}
/*可以通过调用kernel32.dll中的两个api:WritePrivateProfileString,GetPrivateProfileString
来实现对ini 文件的读些。*/
展开全部
using System;
using System.Drawing;
using System.Collections;
using System.ComponentModel;
using System.Windows.Forms;
using System.Data;
using System.Runtime.InteropServices;
using System.Text;
class RwIni
{
[DllImport("kernel32")]
private static extern long WritePrivateProfileString(string section,string key, string val, string filePath);
[DllImport("kernel32")]
private static extern int GetPrivateProfileString(string section,string key, string def, StringBuilder retVal,int size, string filePath);
/// <summary>
/// 写入INI文件
/// </summary>
/// <param name="Section">节点名称</param>
/// <param name="Key">关键字</param>
/// <param name="Value">值</param>
/// <param name="filepath">INI文件路径</param>
static public void IniWriteValue(string Section, string Key, string Value, string filepath)
{
WritePrivateProfileString(Section, Key, Value, filepath);
}
/// <summary>
/// 读取INI文件
/// </summary>
/// <param name="Section">节点名称</param>
/// <param name="Key">关键字</param>
/// <param name="filepath">INI文件路径</param>
/// <returns>值</returns>
static public string IniReadValue(string Section, string Key, string filepath)
{
StringBuilder temp = new StringBuilder(255);
int i = GetPrivateProfileString(Section, Key, "", temp,
255, filepath);
return temp.ToString();
}
}
送你个INI读写类啦!随便写什么都可以!
using System.Drawing;
using System.Collections;
using System.ComponentModel;
using System.Windows.Forms;
using System.Data;
using System.Runtime.InteropServices;
using System.Text;
class RwIni
{
[DllImport("kernel32")]
private static extern long WritePrivateProfileString(string section,string key, string val, string filePath);
[DllImport("kernel32")]
private static extern int GetPrivateProfileString(string section,string key, string def, StringBuilder retVal,int size, string filePath);
/// <summary>
/// 写入INI文件
/// </summary>
/// <param name="Section">节点名称</param>
/// <param name="Key">关键字</param>
/// <param name="Value">值</param>
/// <param name="filepath">INI文件路径</param>
static public void IniWriteValue(string Section, string Key, string Value, string filepath)
{
WritePrivateProfileString(Section, Key, Value, filepath);
}
/// <summary>
/// 读取INI文件
/// </summary>
/// <param name="Section">节点名称</param>
/// <param name="Key">关键字</param>
/// <param name="filepath">INI文件路径</param>
/// <returns>值</returns>
static public string IniReadValue(string Section, string Key, string filepath)
{
StringBuilder temp = new StringBuilder(255);
int i = GetPrivateProfileString(Section, Key, "", temp,
255, filepath);
return temp.ToString();
}
}
送你个INI读写类啦!随便写什么都可以!
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
展开全部
在做项目过程中,有时需要保存一些简单的配置信息,可以使用xml,也可以使用INI文件。下面是C#中读取INI的方法,相信大部分朋友都使用过这种方式。
INI文件的存储方式如下,
[section]
key=value
key=value
[DllImport("kernel32")]
private static extern long WritePrivateProfileString(string section, string key, string val, string filePath);
[DllImport("kernel32")]
private static extern int GetPrivateProfileString(string lpAppName, string lpKeyName, string lpDefault, StringBuilder lpReturnedString, int nSize, string lpFileName);
[DllImport("kernel32.dll", CharSet = CharSet.Auto)]
private static extern uint GetPrivateProfileSection(string lpAppName, IntPtr lpReturnedString, uint nSize, string lpFileName);
private static string ReadString(string section, string key, string def, string filePath)
{
StringBuilder temp = new StringBuilder(1024);
try
{
GetPrivateProfileString(section, key, def, temp, 1024, filePath);
}
catch
{ }
return temp.ToString();
}
/// <summary>
/// 根据section取所有key
/// </summary>
/// <param name="section"></param>
/// <param name="filePath"></param>
/// <returns></returns>
public static string[] ReadIniAllKeys(string section,string filePath)
{
UInt32 MAX_BUFFER = 32767;
string[] items = new string[0];
IntPtr pReturnedString = Marshal.AllocCoTaskMem((int)MAX_BUFFER * sizeof(char));
UInt32 bytesReturned = GetPrivateProfileSection(section, pReturnedString, MAX_BUFFER, filePath);
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;
}
/// <summary>
/// 根据section,key取值
/// </summary>
/// <param name="section"></param>
/// <param name="keys"></param>
/// <param name="filePath">ini文件路径</param>
/// <returns></returns>
public static string ReadIniKeys(string section, string keys, string filePath)
{
return ReadString(section, keys, "", filePath);
}
/// <summary>
/// 保存ini
/// </summary>
/// <param name="section"></param>
/// <param name="key"></param>
/// <param name="value"></param>
/// <param name="filePath">ini文件路径</param>
public static void WriteIniKeys(string section, string key, string value, string filePath)
{
WritePrivateProfileString(section, key, value, filePath);
}
如果要删除某一项:
WriteIniKeys(section, key, null, recordIniPath);
INI文件如何创建:
[DllImport("kernel32")]
public static extern long WritePrivateProfileString(string section, string key, string value, string iniPath);
调用该方法,即可创建你的ini文件和想要保存的值。
INI文件的存储方式如下,
[section]
key=value
key=value
[DllImport("kernel32")]
private static extern long WritePrivateProfileString(string section, string key, string val, string filePath);
[DllImport("kernel32")]
private static extern int GetPrivateProfileString(string lpAppName, string lpKeyName, string lpDefault, StringBuilder lpReturnedString, int nSize, string lpFileName);
[DllImport("kernel32.dll", CharSet = CharSet.Auto)]
private static extern uint GetPrivateProfileSection(string lpAppName, IntPtr lpReturnedString, uint nSize, string lpFileName);
private static string ReadString(string section, string key, string def, string filePath)
{
StringBuilder temp = new StringBuilder(1024);
try
{
GetPrivateProfileString(section, key, def, temp, 1024, filePath);
}
catch
{ }
return temp.ToString();
}
/// <summary>
/// 根据section取所有key
/// </summary>
/// <param name="section"></param>
/// <param name="filePath"></param>
/// <returns></returns>
public static string[] ReadIniAllKeys(string section,string filePath)
{
UInt32 MAX_BUFFER = 32767;
string[] items = new string[0];
IntPtr pReturnedString = Marshal.AllocCoTaskMem((int)MAX_BUFFER * sizeof(char));
UInt32 bytesReturned = GetPrivateProfileSection(section, pReturnedString, MAX_BUFFER, filePath);
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;
}
/// <summary>
/// 根据section,key取值
/// </summary>
/// <param name="section"></param>
/// <param name="keys"></param>
/// <param name="filePath">ini文件路径</param>
/// <returns></returns>
public static string ReadIniKeys(string section, string keys, string filePath)
{
return ReadString(section, keys, "", filePath);
}
/// <summary>
/// 保存ini
/// </summary>
/// <param name="section"></param>
/// <param name="key"></param>
/// <param name="value"></param>
/// <param name="filePath">ini文件路径</param>
public static void WriteIniKeys(string section, string key, string value, string filePath)
{
WritePrivateProfileString(section, key, value, filePath);
}
如果要删除某一项:
WriteIniKeys(section, key, null, recordIniPath);
INI文件如何创建:
[DllImport("kernel32")]
public static extern long WritePrivateProfileString(string section, string key, string value, string iniPath);
调用该方法,即可创建你的ini文件和想要保存的值。
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询