RT

解决方案 »

  1.   

    //声明ini文件的写操作函数
    [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需求
      

  2.   

    //配置信息结构
        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
                {                
                }
            }
      

  3.   

    http://www.mentalis.org/soft/class.qpx?id=6
      

  4.   

    xml年代还用ini,ini基本都快被淘汰了,建议楼主用XML吧