RT
解决方案 »
- (请教)根据textbox中的内容动态筛选listbox中的内容
- C# winfrom 连接MySQL 读取数据超时问题
- 问个关于datagrid打印问题,谁能提供个打印类!万分感谢!
- 初学者问关于ToSingle()的一个问题。
- 请问嵌套的在datalist里的radiobutton 怎么才能只选中一个啊
- 从远程数据库下载记录到本地的效率问题
- springnet+ibatisnet中的事务是如何在配置文件中实现
- FLASH后台截图,好像不能及时刷新,有知道这么回事的吗?
- 高分求正则表达式的资料!
- 解析柱状图
- 如何在C#图形界面中退出时自动启动浏览器并且访问某一个特定网址?
- 你都写过什么木马或者病毒?
[DllImport("kernel32")]
private static extern long WritePrivateProfileString(string section, string key, string val, string filePath);
//声明ini文件的读操作函数
[DllImport("kernel32")]
private static extern int GetPrivateProfileString(
string section, // ini文件段落的名称
string key, // 关键字
string def, // 无法读取关键字数值时的缺省数值
StringBuilder retval, // 读取数值
int size, // 数值大小
string filePath // ini文件的完整路径
);//加载文件
private void button1_Click(object sender, System.EventArgs e)
{
OpenFileDialog openFD = new OpenFileDialog();
openFD.Filter = "ini 文件(*.ini)|*.ini";
if(openFD.ShowDialog()==DialogResult.OK)
{
this.textBox1.Text = openFD.FileName;
this.richTextBox1.LoadFile(openFD.FileName,RichTextBoxStreamType.PlainText);
}
} //刷新
private void button2_Click(object sender, System.EventArgs e)
{
if(this.textBox1.Text!="")
{
try
{
this.richTextBox1.LoadFile(this.textBox1.Text,RichTextBoxStreamType.PlainText);
}
catch(Exception ex)
{
MessageBox.Show(ex.Message,"错误");
}
}
else
{
MessageBox.Show("请先选择一个ini文件!","提示");
}
} //写入
private void button3_Click(object sender, System.EventArgs e)
{
if(this.textBox1.Text.Trim()=="")
{
MessageBox.Show("请先选择一个ini文件!","提示");
return;
}
if(textBox2.Text==""||textBox3.Text==""||textBox4.Text=="")
{
MessageBox.Show("请完整输入 段落名、关键字和关键字数值!","提示");
return;
}
try
{
WritePrivateProfileString(textBox2.Text,textBox3.Text,textBox4.Text,textBox1.Text);
MessageBox.Show("成功写入ini文件!");
}
catch(Exception ex)
{
MessageBox.Show(ex.Message,"错误");
}
} private void button4_Click(object sender, System.EventArgs e)
{
if(this.textBox1.Text.Trim()=="")
{
MessageBox.Show("请先选择一个ini文件!","提示");
return;
}
if(textBox2.Text==""||textBox3.Text=="")
{
MessageBox.Show("请完整输入 段落名和关键字数值!","提示");
return;
}
StringBuilder temp = new StringBuilder(255);
try
{
int i = GetPrivateProfileString(textBox2.Text,textBox3.Text,"无法读取对应的数值",temp,255,textBox1.Text);
textBox4.Text = temp.ToString();
}
catch(Exception ex)
{
MessageBox.Show(ex.Message,"错误");
}
}
不知可能满足LZ需求
public class ConfigInfo
{
string FILE_NAME = ConstantClass.CONFIG_FILE_PATH;
public string host;
public string post;
public string adress;
public bool onlyUseAdress;
private int channelNum;
public List<ChannelInfo> channelList; public ConfigInfo()
{
channelNum = 0;
host = string.Empty;
post = string.Empty;
channelList = new List<ChannelInfo>(7);
} #region 添加读写INI文件方法的声明
[DllImport("kernel32")]
private static extern bool GetPrivateProfileString(
string lpApplicationName,
string lpKeyName,
string lpDefault,
StringBuilder lpReturnedString,
int nSize,
string lpFileName);
[DllImport("kernel32")]
private static extern bool WritePrivateProfileString(
string lpApplicationName,
string lpKeyName,
string lpString,
string lpFileName); #endregion //读取写入配置信息
public void SaveConfigInfo()
{
try
{
WritePrivateProfileString("ConfigInfo", "host", host, FILE_NAME);
WritePrivateProfileString("ConfigInfo", "post", post, FILE_NAME);
WritePrivateProfileString("ConfigInfo", "adress", adress, FILE_NAME);
WritePrivateProfileString("ConfigInfo", "onlyUseAdress", onlyUseAdress.ToString(), FILE_NAME);
WritePrivateProfileString("ConfigInfo", "channelNum", channelList.Count.ToString(), FILE_NAME); for (int i = 0; i < channelList.Count; ++i)
{
WritePrivateProfileString("channel" + i.ToString(), "dbName", channelList[i].dbName, FILE_NAME);
WritePrivateProfileString("channel" + i.ToString(), "icoIndex", channelList[i].icoIndex.ToString(), FILE_NAME);
WritePrivateProfileString("channel" + i.ToString(), "isShow", channelList[i].isShow.ToString(), FILE_NAME);
}
}
catch
{
}
} //读取配置信息
public void LoadConfigInfo()
{
int length = 255;
StringBuilder tempStr = new StringBuilder(length); if (!File.Exists(FILE_NAME))
{
File.Create(FILE_NAME);
} try
{
GetPrivateProfileString("ConfigInfo", "host", "", tempStr, tempStr.Capacity, FILE_NAME);
host = tempStr.ToString();
GetPrivateProfileString("ConfigInfo", "post", "", tempStr, tempStr.Capacity, FILE_NAME);
post = tempStr.ToString();
GetPrivateProfileString("ConfigInfo", "adress", "", tempStr, tempStr.Capacity, FILE_NAME);
adress = tempStr.ToString();
GetPrivateProfileString("ConfigInfo", "onlyUseAdress", "", tempStr, tempStr.Capacity, FILE_NAME);
onlyUseAdress = bool.Parse(tempStr.ToString());
GetPrivateProfileString("ConfigInfo", "channelNum", "0", tempStr, tempStr.Capacity, FILE_NAME);
channelNum = int.Parse(tempStr.ToString()); ChannelInfo item;
channelList.Clear();
string temp;
for (int i = 0; i < channelNum; ++i)
{
item = new ChannelInfo();
temp = "channel" + i.ToString();
GetPrivateProfileString(temp, "dbName", "", tempStr, tempStr.Capacity, FILE_NAME);
item.dbName = tempStr.ToString();
GetPrivateProfileString(temp, "icoIndex", "", tempStr, tempStr.Capacity, FILE_NAME);
item.icoIndex = int.Parse(tempStr.ToString());
GetPrivateProfileString(temp, "isShow", "", tempStr, tempStr.Capacity, FILE_NAME);
item.isShow = bool.Parse(tempStr.ToString()); channelList.Add(item);
}
}
catch
{
}
}