各位高手大家好,小弟是C#初學者  请问如何用C#读写TXT檔案的配置文件然后修改参数和方法,能给小弟一个范例么!谢谢了!
       在线等哦!!麻烦各位了!

解决方案 »

  1.   

    格式如下:
    [Communication]
    COM=3
    Baudrate=19200
    Parity=e
    DataBit=7
    StopBit=1[Initial Command]
    Start PLC =sdjkhjfhowehor[Normal Command]
    Get X Data = jaksbdkajsbd
    Get Y Data = kjsndkjas
    Get M Data = kjqhdkhja[Direct Command]
    Start Process = kjasdkjs
    Push the Door = kjskhjas[Other]
    Timeout in ms = 1000
    Retry Time = 3
      

  2.   

    用 app.config 的 appSettings 配置节。然后用 ConfigurationManager.AppSettings["key"] 读取http://msdn.microsoft.com/zh-cn/library/ms228312.aspx
      

  3.   

    你这个文件明显是ini文件的格式。
      

  4.   

    private static bool WriteToFile(string strFile, string strData)
            {
                DateTime dtNow = DateTime.Now;
                string strFilePath = strFile + dtNow.ToString("yyyyMMdd") + ".txt";
                StreamWriter sw = new StreamWriter(strFilePath, true);
                sw.WriteLine(strData);
                sw.Close();
                return true;
            }
    可以输出TXT
      

  5.   


    string CurrentPath = Directory.GetCurrentDirectory(); 
    if(CurrentPath.Length!=3) 
    CurrentPath=CurrentPath+"\\"; 
    string inipath=CurrentPath+"ini.ini"; 
    Console.WriteLine("inipath={0}",inipath); 
    IniFile ini = new IniFile(inipath); 
    int firstrun,timer; 
    string appname; 
    firstrun=ini.IniReadValueInt("SYSTEM","FIRSTRUN",0); 
    appname=ini.IniReadValueString("SYSTEM","APPNAME","ini.exe"); 
    timer=ini.IniReadValueInt("SYSTEM","TIMER",0); 
    Console.WriteLine("firstrun={0},appname={1},timer={2}",firstrun,appname,timer); 
    string ORACLESERVICE,ORACLEUSERNAME,ORACLEPASSWORD; 
    ORACLESERVICE=ini.IniReadValueString("ORACLESERVER","ORACLESERVICENAME","YXZC"); 
    ORACLEUSERNAME=ini.IniReadValueString("ORACLESERVER","ORACLEUSERNAME","PMS"); 
    ORACLEPASSWORD=ini.IniReadValueString("ORACLESERVER","ORACLEPASSWORD","111111"); 
    int year,month,day,hour,minute,second; 
    year=ini.IniReadValueInt("TIME","YEAR",2005); 
    month=ini.IniReadValueInt("TIME","MONTH",7); 
    day=ini.IniReadValueInt("TIME","DAY",8); 
    hour=ini.IniReadValueInt("TIME","HOUR",9); 
    minute=ini.IniReadValueInt("TIME","MINUTE",10); 
    second=ini.IniReadValueInt("TIME","SECOND",11); 
    Console.WriteLine("{0}-{1}-{2} {3}:{4}:{5}",year,month,day,hour,minute,second); 
     
    int count=ini.IniReadValueInt("ACCMU","COUNT",0); 
    int numAccmu=5; 
    string[] Accmustr=new string[numAccmu+1]; 
    double[] Accmu=new double[numAccmu+1]; 
    for(int i=1;i=numAccmu;i++) 

    Accmustr[i]="A"+i.ToString(); 
    Accmu[i]=double.Parse(ini.IniReadValueString("ACCMU",Accmustr[i],"0.0")); 
    Console.WriteLine("{0}={1}",Accmustr[i],Accmu[i]); 

    for(int i=1;i=numAccmu;i++) 

    Accmu[i]=Accmu[i]+i; 
    ini.IniWriteValue("ACCMU",Accmustr[i],Accmu[i].ToString()); 

    C# 操作 INI 文件 
      

  6.   

    http://www.pudn.com/downloads152/sourcecode/windows/csharp/detail662020.html查看下,这个DEMO
      

  7.   


    public class IniFile 

    public string path;    //INI文件名 
    [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); 
    [DllImport("kernel32")] 
    private static extern int GetPrivateProfileInt(string section,string key,int def,string filePath ); 
     
    public IniFile(string INIPath) 

    path = INIPath; 

    //类的构造函数,传递INI文件名 
    public void IniWriteValue(string Section,string Key,string Value) 
    {  
    WritePrivateProfileString(Section,Key,Value,this.path);  

    //写INI文件 
    public string IniReadValueString(string Section,string Key,string def) 

    StringBuilder temp = new StringBuilder(255); 
    int i = GetPrivateProfileString(Section,Key,def,temp,255,this.path); 
    return temp.ToString(); 

    public int IniReadValueInt(string Section,string Key,int def) 

    StringBuilder temp = new StringBuilder(255); 
    int i = GetPrivateProfileInt(Section,Key,def,this.path); 
    return (i); 

    }  
      

  8.   

    在项目中鼠标右键,添加---新建项---->应用程序配置文件;确定1、好了,打开那个文件,输入尖括号,自动提示如:---  <appSettings>
        
      </appSettings>2、输入    <add key="conn" value ="database ......"/>
    3、在程序中读取。   string conn = System.Configuration.ConfigurationSettings.AppSettings["conn"] as string;
            
      

  9.   

    读ini可以做,但是现在基本不用了。多用xml,按我的步骤做一下。
      

  10.   

    哎~~謝謝各位說了這麼多!可是我的意思是1.我有一個INI文件 內容如下
    [Communication]
    COM=3
    Baudrate=19200
    Parity=e
    DataBit=7
    StopBit=1[Initial Command]
    Start PLC =sdjkhjfhowehor[Normal Command]
    Get X Data = jaksbdkajsbd
    Get Y Data = kjsndkjas
    Get M Data = kjqhdkhja[Direct Command]
    Start Process = kjasdkjs
    Push the Door = kjskhjas[Other]
    Timeout in ms = 1000
    Retry Time = 3其中
    [Communication]裏,定義的是要和通訊端達成協議(Shake Hand)所需要的參數。
    [Initial Command]裏,定義的是通訊完成後,一開始所需要執行的指令。
    [Normal Command]裏,定義的是[Initial Command]後,所需要常規性執行的指令,比如說是取得X值或是取得M值之類,這些命令會一直循環執行,以更新取得後的資料。
    [Direct Command]裏,定義的是可以即時下的指令,下了指令之後,可以即時性的更改通訊端的設定。
    [Other]裏,定義一些必要的參數
    [Communication]
    COM=3
    Baudrate=19200
    Parity=e
    DataBit=7
    StopBit=1[Initial Command]
    Start PLC =sdjkhjfhowehor[Normal Command]
    Get X Data = jaksbdkajsbd
    Get Y Data = kjsndkjas
    Get M Data = kjqhdkhja[Direct Command]
    Start Process = kjasdkjs
    Push the Door = kjskhjas[Other]
    Timeout in ms = 1000
    Retry Time = 3其中
    [Communication]裏,定義的是要和通訊端達成協議(Shake Hand)所需要的參數。
    [Initial Command]裏,定義的是通訊完成後,一開始所需要執行的指令。
    [Normal Command]裏,定義的是[Initial Command]後,所需要常規性執行的指令,比如說是取得X值或是取得M值之類,這些命令會一直循環執行,以更新取得後的資料。
    [Direct Command]裏,定義的是可以即時下的指令,下了指令之後,可以即時性的更改通訊端的設定。
    [Other]裏,定義一些必要的參數
    2.定義一個類COM Class
    PLC = new COMClass();命令
    開啟設定檔
    PLC.Open(“PLC.TXT”);建立通訊
    PLC.Connect();開始執行[Initial Command]及[Normal Command]指令
    PLC.Start();開始執行[Direct Command]指令
    PLC.Command(“指令前的文字“);結束通訊
    PLC.Clos();事件
    連線錯誤
    PLC.ConnectError();通訊錯誤
    PLC.CommError();
    我應該怎麼根據這個INI來和這些屬性,方法互動傳值呢?