首页 > 开发 > 综合 > 正文

C#读写INI文件

2024-07-21 02:29:46
字体:
来源:转载
供稿:网友

用ini文件感觉还是挺方便的

using system;
using system.drawing;
using system.collections;
using system.componentmodel;
using system.windows.forms;
using system.io;
using system.runtime.interopservices;
using system.text;
namespace ini
{
 /// <summary>
 /// form1 的摘要说明。
 /// </summary>
 public class form1 : system.windows.forms.form
 { 
  private system.windows.forms.button button1;
  private system.windows.forms.textbox textbox1;
  private system.windows.forms.textbox textbox2;
  private system.windows.forms.textbox textbox3;
  private system.windows.forms.button button3;
  public string database,userid,server;
  //申明ini文件的写操作函数writeprivateprofilestring()
  [ dllimport ( "kernel32" ) ]
  private static extern long writeprivateprofilestring ( string section , string key , string val , string filepath ) ;
  //申明ini文件的读操作函数getprivateprofilestring()
  [ dllimport ( "kernel32" ) ]
  private static extern int getprivateprofilestring ( string section ,string key , string def , stringbuilder retval , int size , string filepath ) ;

  /// <summary>
  /// 必需的设计器变量。
  /// </summary>
  private system.componentmodel.container components = null;

  public form1()
  {
   //
   // windows 窗体设计器支持所必需的
   //
   initializecomponent();

   //
   // todo: 在 initializecomponent 调用后添加任何构造函数代码
   //
  }

  /// <summary>
  /// 清理所有正在使用的资源。
  /// </summary>
  protected override void dispose( bool disposing )
  {
   if( disposing )
   {
    if (components != null)
    {
     components.dispose();
    }
   }
   base.dispose( disposing );
  }

  #region windows 窗体设计器生成的代码
  /// <summary>
  /// 设计器支持所需的方法 - 不要使用代码编辑器修改
  /// 此方法的内容。
  /// </summary>
  private void initializecomponent()
  {
   this.button1 = new system.windows.forms.button();
   this.textbox1 = new system.windows.forms.textbox();
   this.textbox2 = new system.windows.forms.textbox();
   this.textbox3 = new system.windows.forms.textbox();
   this.button3 = new system.windows.forms.button();
   this.suspendlayout();
   //
   // button1
   //
   this.button1.location = new system.drawing.point(176, 16);
   this.button1.name = "button1";
   this.button1.size = new system.drawing.size(88, 24);
   this.button1.tabindex = 0;
   this.button1.text = "写ini文件";
   this.button1.click += new system.eventhandler(this.button1_click);
   //
   // textbox1
   //
   this.textbox1.location = new system.drawing.point(40, 8);
   this.textbox1.name = "textbox1";
   this.textbox1.size = new system.drawing.size(96, 21);
   this.textbox1.tabindex = 1;
   this.textbox1.text = "";
   //
   // textbox2
   //
   this.textbox2.location = new system.drawing.point(40, 40);
   this.textbox2.name = "textbox2";
   this.textbox2.size = new system.drawing.size(96, 21);
   this.textbox2.tabindex = 2;
   this.textbox2.text = "";
   //
   // textbox3
   //
   this.textbox3.location = new system.drawing.point(40, 72);
   this.textbox3.name = "textbox3";
   this.textbox3.size = new system.drawing.size(96, 21);
   this.textbox3.tabindex = 3;
   this.textbox3.text = "";
   //
   // button3
   //
   this.button3.location = new system.drawing.point(176, 56);
   this.button3.name = "button3";
   this.button3.size = new system.drawing.size(88, 24);
   this.button3.tabindex = 6;
   this.button3.text = "读ini文件";
   this.button3.click += new system.eventhandler(this.button3_click);
   //
   // form1
   //
   this.autoscalebasesize = new system.drawing.size(6, 14);
   this.clientsize = new system.drawing.size(320, 117);
   this.controls.add(this.button3);
   this.controls.add(this.textbox3);
   this.controls.add(this.textbox2);
   this.controls.add(this.textbox1);
   this.controls.add(this.button1);
   this.name = "form1";
   this.text = "ini";
   this.load += new system.eventhandler(this.form1_load);
   this.resumelayout(false);

  }
  #endregion

  /// <summary>
  /// 应用程序的主入口点。
  /// </summary>
  [stathread]
  static void main()
  {
   application.run(new form1());
  }
  private void server_info()
  {
   string s =  application.executablepath;
   string s1;
   s1 = s.replace("ini.exe","config.ini");
   //写入ini 文件
   string filename = "fiatdatabase" ;
   string path = s1;
   string key1 = "database";
   string keyvalue1 = textbox1.text ;
   string key2 = "server";
   string keyvalue2 = textbox2.text ;
   string key3 = "userid";
   string keyvalue3 = textbox3.text ;
   writeprivateprofilestring(filename,key1,keyvalue1,path) ;
   writeprivateprofilestring(filename,key2,keyvalue2,path) ;
   writeprivateprofilestring(filename,key3,keyvalue3,path) ;
   messagebox.show("完毕!");
  }
  //读取服务器信息
  

  private void button1_click(object sender, system.eventargs e)
  {
   server_info();
  
  }

  private void button2_click(object sender, system.eventargs e)
  {
   
   
  }

  //读取服务器信息
  private void read_systeminfo()
  {
   try
   {
    //获取配置信息
    string s =  application.executablepath;
    string s1;
    s1 = s.replace("ini.exe","config.ini");
    stringbuilder temp1 = new stringbuilder ( 255 ) ;
    int i1 = getprivateprofilestring ("fiatdatabase" ,"database" , "" , temp1 , 255 , s1 ) ;   
    database = temp1.tostring();   
    stringbuilder temp2 = new stringbuilder ( 255 ) ;
    int i2 = getprivateprofilestring ("fiatdatabase" ,"server" , "" , temp2 , 255 , s1 ) ;
    server = temp2.tostring();
    stringbuilder temp3 = new stringbuilder ( 255 ) ;
    int i3 = getprivateprofilestring ("fiatdatabase" ,"userid" , "" , temp3 , 255 , s1 ) ;
    userid = temp3.tostring();
   }
   catch(exception e1)
   {
    messagebox.show("请先配置服务器信息!");
   }
  }

  private void button3_click(object sender, system.eventargs e)
  {
 
   read_systeminfo();
   
  }

  private void form1_load(object sender, system.eventargs e)
  {
  
  }
 }
}

菜鸟学堂:
发表评论 共有条评论
用户名: 密码:
验证码: 匿名发表