C# - 读写INI文件

运行效果:

 

 

代码:

  1 using System;
  2 using System.Collections.Generic;
  3 using System.ComponentModel;
  4 using System.Data;
  5 using System.Drawing;
  6 using System.Linq;
  7 using System.Text;
  8 using System.Threading.Tasks;
  9 using System.Windows.Forms;
 10 
 11 //添加相应命名空间
 12 using System.Runtime.InteropServices;
 13 
 14 namespace InI
 15 {
 16     public partial class Form1 : Form
 17     {
 18         public Form1()
 19         {
 20             InitializeComponent();
 21         }
 22 
 23         [DllImport("Kernel32")]
 24         //创建INI文件
 25         //section:小节名程
 26         //key:关键字
 27         //val:值(可以是变量,字符串,整型,Bool型)
 28         //filepath:文件所在路径
 29         private static extern long WritePrivateProfileString(string section, string key, string val, string filepath);
 30 
 31         [DllImport("Kernel32")]
 32         //读取INI文件
 33         //section:查找条目的小节条数
 34         //key:关键字
 35         //def:制定的条目没有找到时返回的默认值,可以设置为空。
 36         //retVal:制定一个字符串缓冲区
 37         //size:retVal缓冲区最大的字节数
 38         //filepath:文件所在的路径
 39         private static extern long GetPrivateProfileString(string section, string key, string def, StringBuilder retVal, int size, string filepath);
 40 
 41         /// <summary>
 42         /// 获取路径名称,文件名称,生成新的带有文件名的路径
 43         /// </summary>
 44         /// <returns>新的路径</returns>
 45         public string FilePathName()
 46         {
 47             string FileName = this.txt_FileName.Text.Trim();
 48 
 49             string FilePath = this.txt_FilePath.Text.Trim();
 50 
 51             string NewFileName = FilePath + "\\" + FileName + ".ini";
 52 
 53             return NewFileName;
 54         }
 55 
 56         /// <summary>
 57         /// 创建INI文件
 58         /// 当INI文件不存在时WritePrivateProfileString会创建INI文件,而已经存在文件的时候会添加到已有的INI文件中。
 59         /// </summary>
 60         private void button1_Click_1(object sender, EventArgs e)
 61         {
 62             try
 63             {
 64                 //创建并写入INI文件
 65                 WritePrivateProfileString("MyQQ", "ID", "192.168.1.230", FilePathName());
 66 
 67                 //在已有的INI文件中追加信息
 68                 WritePrivateProfileString("MyQQ", "Port", "11000", FilePathName());
 69 
 70                 //用不同的小节名称,在INI文件中追加信息
 71                 WritePrivateProfileString("MyName", "Port", "11000", FilePathName());
 72 
 73                 this.txt_FilePath.Enabled = false;
 74                 MessageBox.Show("创建成功!", "提示");
 75             }
 76             catch (Exception ex)
 77             {
 78                 MessageBox.Show(ex.Message, "提示");
 79             }
 80         }
 81 
 82         /// <summary>
 83         /// 读取INI文件
 84         /// </summary>
 85         /// <param name="sender"></param>
 86         /// <param name="e"></param>
 87         private void button2_Click_1(object sender, EventArgs e)
 88         {
 89             try
 90             {
 91                 //创建一个StringBuilder对象
 92                 StringBuilder temp = new StringBuilder();
 93                 GetPrivateProfileString("MyQQ", "ID", "服务器地址读取错误..........", temp, 255, FilePathName());
 94                 string MyID = temp.ToString();
 95                 GetPrivateProfileString("MyName", "Port", "服务器地址读取错误..........", temp, 255, FilePathName());
 96                 string MyPort = temp.ToString();
 97 
 98                 this.label2.Text = "ID:" + MyID + "\n\n" + "Port:" + MyPort;
 99             }
100             catch (Exception ex)
101             {
102                 MessageBox.Show(ex.Message, "提示");
103             }
104         }
105     }
106 }

 

最终:

 

posted on 2015-05-19 15:05  ultrastrong  阅读(396)  评论(0编辑  收藏  举报