应急跪求~!!!!!!!!!!!!!!!!!!!!!!!~!!!!!!!!!!!!!!~ C#中读取文本文件的内容,不能用STRING类的方法和正则表达式还有IO流,请大家帮帮忙,谢谢了跪求跪求 解决方案 » 免费领取超大流量手机卡,每月29元包185G流量+100分钟通话, 中国电信官方发货 日本人说不能用IO流,不能用STRING,不能用正则表达式,只能用微软读INI配置文件的方法读TXT文件 可以用windwos API 去读取我不知道你这个文件有没有什么格式要求如果有纯C#来做,不能用STRING类的方法和正则表达式还有IO流:我看是做不到的你可以去查一下windwos api 是可以读取的 废话,你读出来的数据放到什么地方,不会是放到二进制数组吧,最后还是要放到string里。 读进来的东西放在哪里?难不成用转换成BYTE数组,然后用ASCII码显示啊!! 在API中有两个方法是操作INI文件的:WritePrivateProfileString,GetPrivateProfileString在DOTNET中使用比较麻烦,因为其中的参数类型在DOTNET中没有。 双击打开文本文件,CTRL+A,CTRL+C,然后切换到C#程序中,CTRL+V你会发现文本文件中的内容已经在C#程序中了没有用STRING类,没有用正则表达式,也没有用IO流,完全符合要求,更重要的不用写一行代码。 双击打开文本文件,CTRL+A,CTRL+C,然后切换到C#程序中,CTRL+V你会发现文本文件中的内容已经在C#程序中了没有用STRING类,没有用正则表达式,也没有用IO流,完全符合要求,更重要的不用写一行代码。 RxitRose(天恒) ( ) 信誉:99 2005-10-19 16:22:24 得分: 0 可以用windwos API 去读取我不知道你这个文件有没有什么格式要求如果有纯C#来做,不能用STRING类的方法和正则表达式还有IO流:我看是做不到的你可以去查一下windwos api 是可以读取的 好像看过一篇文章,讲怎么用C#去读INI的,是个技巧,可惜不知道在哪了. 如果是读取 ini文件,可以用[DllImport("kernel32.dll")]private static extern int GetPrivateProfileString ( string section ,string key , string def , StringBuilder retVal ,int size , string filePath );但楼主似乎不是要读ini文件,而且你读出来放在什么地方?string不能用,那还用什么C#?string 本来就是C#的一部分 兄弟!还是不要为小日本干活了吧!!下面是你要的答案C#中读写INI文件的关键步骤和解决方法C#对INI文件进行写操作:对INI文件进行写操作,是通过组件button2的"Click"事件来实现的。这里有一点应该注意,当在调用 WritePrivateProfileString()对INI文件进行写操作的时候,如果此时在INI文件中存在和要写入的信息相同的段落名称和关键字,则将覆盖此INI信息。下面是button2组件的"Click"事件对应的代码清单:private void button2_Click ( object sender , System.EventArgs e ) { string FileName = textBox1.Text ; string section = textBox2.Text ; string key = textBox3.Text ; string keyValue = textBox4.Text ; WritePrivateProfileString ( section , key , keyValue , FileName ) ; MessageBox.Show ( "成功写入INI文件!" , "信息" ) ; }C#对INI文件进行读操作:正确读取INI的必须满足三个前提:INI文件的全路径、段落名称和关键字名称。否则就无法正确读取。你可以设定读取不成功后的缺省数值,在下面的程序中,为了直观设定的是“无法读取对应数值!”字符串,读取INI文件是通过button3组件的“Click”事件来实现的,下面是其对应的代码清单:private void button3_Click ( object sender , System.EventArgs e ) { StringBuilder temp = new StringBuilder ( 255 ) ; string FileName = textBox1.Text ; string section = textBox2.Text ; string key = textBox3.Text ; int i = GetPrivateProfileString ( section , key ,"无法读取对应数值!", temp , 255 , FileName ) ; //显示读取的数值 textBox4.Text = temp.ToString ( ) ; }C#操作INI文件的完整源代码(ini.cs)和运行界面通过上面的这些介绍,我们不难得到用C#操作INI文件的完整代码清单(ini.cs),具体如下:using System ;using System.Drawing ;using System.Collections ;using System.ComponentModel ;using System.Windows.Forms ;using System.Data ;using System.Runtime.InteropServices ;using System.Text ;namespace C_操作INI文件__写操作{ public class Form1 : System.Windows.Forms.Form { private System.Windows.Forms.Button button1 ; private System.Windows.Forms.TextBox textBox1 ; private System.Windows.Forms.Button button2 ; private System.Windows.Forms.TextBox textBox2 ; private System.Windows.Forms.TextBox textBox3 ; private System.Windows.Forms.TextBox textBox4 ; private System.Windows.Forms.Label label1 ; private System.Windows.Forms.Label label2 ; private System.Windows.Forms.Label label3 ; private System.Windows.Forms.Button button3 ; private System.Windows.Forms.OpenFileDialog openFileDialog1 ; private System.ComponentModel.Container components = null ; public Form1 ( ) { InitializeComponent ( ) ; } protected override void Dispose ( bool disposing ) { if ( disposing ) { if ( components != null ) { components.Dispose ( ) ; } } base.Dispose ( disposing ) ; } [ 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 ) ; private void InitializeComponent ( ) { this.button1 = new System.Windows.Forms.Button ( ) ; this.textBox1 = new System.Windows.Forms.TextBox ( ) ; this.button2 = new System.Windows.Forms.Button ( ) ; this.textBox2 = new System.Windows.Forms.TextBox ( ) ; this.textBox3 = new System.Windows.Forms.TextBox ( ) ; this.textBox4 = new System.Windows.Forms.TextBox ( ) ; this.label1 = new System.Windows.Forms.Label ( ) ; this.label2 = new System.Windows.Forms.Label ( ) ; this.label3 = new System.Windows.Forms.Label ( ) ; this.button3 = new System.Windows.Forms.Button ( ) ; this.openFileDialog1 = new System.Windows.Forms.OpenFileDialog ( ) ; this.SuspendLayout ( ) ; this.button1.FlatStyle = System.Windows.Forms.FlatStyle.Flat ; this.button1.Location = new System.Drawing.Point ( 238 , 20 ) ; this.button1.Name = "button1" ; this.button1.Size = new System.Drawing.Size ( 100 , 32 ) ; this.button1.TabIndex = 0 ; this.button1.Text = "选择INI文件" ; this.button1.Click += new System.EventHandler ( this.button1_Click ) ; this.textBox1.Location = new System.Drawing.Point ( 58 , 22 ) ; this.textBox1.Name = "textBox1" ; this.textBox1.Size = new System.Drawing.Size ( 162 , 21 ) ; this.textBox1.TabIndex = 1 ; this.textBox1.Text = "" ; this.button2.FlatStyle = System.Windows.Forms.FlatStyle.Flat ; this.button2.Location = new System.Drawing.Point ( 86 , 168 ) ; this.button2.Name = "button2" ; this.button2.Size = new System.Drawing.Size ( 98 , 30 ) ; this.button2.TabIndex = 3 ; this.button2.Text = "写入INI文件" ; this.button2.Click += new System.EventHandler ( this.button2_Click ) ; this.textBox2.Location = new System.Drawing.Point ( 160 , 62 ) ; this.textBox2.Name = "textBox2" ; this.textBox2.Size = new System.Drawing.Size ( 176 , 21 ) ; this.textBox2.TabIndex = 5 ; this.textBox2.Text = "" ; this.textBox3.Location = new System.Drawing.Point ( 160 , 94 ) ; this.textBox3.Name = "textBox3" ; this.textBox3.Size = new System.Drawing.Size ( 176 , 21 ) ; this.textBox3.TabIndex = 6 ; this.textBox3.Text = "" ; this.textBox4.Location = new System.Drawing.Point ( 160 , 128 ) ; this.textBox4.Name = "textBox4" ; this.textBox4.Size = new System.Drawing.Size ( 176 , 21 ) ; this.textBox4.TabIndex = 7 ; this.textBox4.Text = "" ; this.label1.Location = new System.Drawing.Point ( 56 , 62 ) ; this.label1.Name = "label1" ; this.label1.TabIndex = 8 ; this.label1.Text = "段落名称:" ; this.label2.Location = new System.Drawing.Point ( 66 , 96 ) ; this.label2.Name = "label2" ; this.label2.TabIndex = 9 ; this.label2.Text = "关键字:" ; this.label3.Location = new System.Drawing.Point ( 42 , 130 ) ; this.label3.Name = "label3" ; this.label3.TabIndex = 10 ; this.label3.Text = "关键字数值:" ; this.button3.FlatStyle = System.Windows.Forms.FlatStyle.Flat ; this.button3.Location = new System.Drawing.Point ( 208 , 168 ) ; this.button3.Name = "button3" ; this.button3.Size = new System.Drawing.Size ( 98 , 30 ) ; this.button3.TabIndex = 11 ; this.button3.Text = "读取INI数值" ; this.button3.Click += new System.EventHandler ( this.button3_Click ) ; this.openFileDialog1.Filter = "INI 文件|*.ini" ; this.AutoScaleBaseSize = new System.Drawing.Size ( 6 , 14 ) ; this.ClientSize = new System.Drawing.Size ( 366 , 217 ) ; this.Controls.AddRange ( new System.Windows.Forms.Control [ ] { this.button3 , this.textBox4 , this.textBox3 , this.textBox2 , this.button2 , this.textBox1 , this.button1 , this.label3 , this.label2 , this.label1 } ) ; this.MaximizeBox = false ; this.Name = "Form1" ; this.Text = "C#操作INI文件--写操作" ; this.ResumeLayout ( false ) ; } [STAThread] static void Main ( ) { Application.Run ( new Form1 ( ) ) ; } private void button1_Click ( object sender , System.EventArgs e ) { openFileDialog1.ShowDialog ( ) ; textBox1.Text = openFileDialog1.FileName ; } //写入INI文件 private void button2_Click ( object sender , System.EventArgs e ) { string FileName = textBox1.Text ; string section = textBox2.Text ; string key = textBox3.Text ; string keyValue = textBox4.Text ; WritePrivateProfileString ( section , key , keyValue , FileName ) ; MessageBox.Show ( "成功写入INI文件!" , "信息" ) ; } //读取指定INI文件的特定段落中的关键字的数值 private void button3_Click ( object sender , System.EventArgs e ) { StringBuilder temp = new StringBuilder ( 255 ) ; string FileName = textBox1.Text ; string section = textBox2.Text ; string key = textBox3.Text ; int i = GetPrivateProfileString ( section , key , "无法读取对应数值!" , emp , 255 , FileName ) ; //显示读取的数值 textBox4.Text = temp.ToString ( ) ; } }} 未实现IXmlSerializable接口的问题 资源文件! winform中contextmenustript如何动态添加子菜单 求个思路~! 接收存储过程返回值 关于dataset的持久性 如何在GridData控件中添加其他控件? 有关存储过程的问题,进入观看详情!万分火急! winform判断文件是否完成上传(新手,请大家指教) 如何开发"顽固"的WindowService? 想写一个正则表达式的自动生成程序,大家帮忙。可以加分,谢谢! vs2005和vs2003数据库控件
我不知道你这个文件有没有什么格式要求
如果有纯C#来做,不能用STRING类的方法和正则表达式还有IO流:我看是做不到的你可以去查一下windwos api 是可以读取的
可以用windwos API 去读取
我不知道你这个文件有没有什么格式要求
如果有纯C#来做,不能用STRING类的方法和正则表达式还有IO流:我看是做不到的你可以去查一下windwos api 是可以读取的
[DllImport("kernel32.dll")]
private static extern int GetPrivateProfileString (
string section ,string key , string def , StringBuilder retVal ,int size , string filePath );但楼主似乎不是要读ini文件,而且你读出来放在什么地方?
string不能用,那还用什么C#?string 本来就是C#的一部分
C#对INI文件进行写操作:对INI文件进行写操作,是通过组件button2的"Click"事件来实现的。这里有一点应该注意,当在调用 WritePrivateProfileString()对INI文件进行写操作的时候,如果此时在INI文件中存在和要写入的信息相同的段落名称和关键字,则将覆盖此INI信息。下面是button2组件的"Click"事件对应的代码清单:private void button2_Click ( object sender , System.EventArgs e )
{
string FileName = textBox1.Text ;
string section = textBox2.Text ;
string key = textBox3.Text ;
string keyValue = textBox4.Text ;
WritePrivateProfileString ( section , key , keyValue , FileName ) ;
MessageBox.Show ( "成功写入INI文件!" , "信息" ) ;
}C#对INI文件进行读操作:正确读取INI的必须满足三个前提:INI文件的全路径、段落名称和关键字名称。否则就无法正确读取。你可以设定读取不成功后的缺省数值,在下面的程序中,为了直观设定的是“无法读取对应数值!”字符串,读取INI文件是通过button3组件的“Click”事件来实现的,下面是其对应的代码清单:private void button3_Click ( object sender , System.EventArgs e )
{
StringBuilder temp = new StringBuilder ( 255 ) ;
string FileName = textBox1.Text ;
string section = textBox2.Text ;
string key = textBox3.Text ;
int i = GetPrivateProfileString ( section , key ,"无法读取对应数值!",
temp , 255 , FileName ) ;
//显示读取的数值
textBox4.Text = temp.ToString ( ) ;
}C#操作INI文件的完整源代码(ini.cs)和运行界面
通过上面的这些介绍,我们不难得到用C#操作INI文件的完整代码清单(ini.cs),具体如下:using System ;
using System.Drawing ;
using System.Collections ;
using System.ComponentModel ;
using System.Windows.Forms ;
using System.Data ;
using System.Runtime.InteropServices ;
using System.Text ;
namespace C_操作INI文件__写操作
{
public class Form1 : System.Windows.Forms.Form
{
private System.Windows.Forms.Button button1 ;
private System.Windows.Forms.TextBox textBox1 ;
private System.Windows.Forms.Button button2 ;
private System.Windows.Forms.TextBox textBox2 ;
private System.Windows.Forms.TextBox textBox3 ;
private System.Windows.Forms.TextBox textBox4 ;
private System.Windows.Forms.Label label1 ;
private System.Windows.Forms.Label label2 ;
private System.Windows.Forms.Label label3 ;
private System.Windows.Forms.Button button3 ;
private System.Windows.Forms.OpenFileDialog openFileDialog1 ;
private System.ComponentModel.Container components = null ;
public Form1 ( )
{
InitializeComponent ( ) ;
}
protected override void Dispose ( bool disposing )
{
if ( disposing )
{
if ( components != null )
{
components.Dispose ( ) ;
}
}
base.Dispose ( disposing ) ;
} [ 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 ) ;
private void InitializeComponent ( )
{
this.button1 = new System.Windows.Forms.Button ( ) ;
this.textBox1 = new System.Windows.Forms.TextBox ( ) ;
this.button2 = new System.Windows.Forms.Button ( ) ;
this.textBox2 = new System.Windows.Forms.TextBox ( ) ;
this.textBox3 = new System.Windows.Forms.TextBox ( ) ;
this.textBox4 = new System.Windows.Forms.TextBox ( ) ;
this.label1 = new System.Windows.Forms.Label ( ) ;
this.label2 = new System.Windows.Forms.Label ( ) ;
this.label3 = new System.Windows.Forms.Label ( ) ;
this.button3 = new System.Windows.Forms.Button ( ) ;
this.openFileDialog1 = new
System.Windows.Forms.OpenFileDialog ( ) ;
this.SuspendLayout ( ) ;
this.button1.FlatStyle = System.Windows.Forms.FlatStyle.Flat ;
this.button1.Location = new System.Drawing.Point ( 238 , 20 ) ;
this.button1.Name = "button1" ;
this.button1.Size = new System.Drawing.Size ( 100 , 32 ) ;
this.button1.TabIndex = 0 ;
this.button1.Text = "选择INI文件" ;
this.button1.Click += new System.EventHandler ( this.button1_Click ) ;
this.textBox1.Location = new System.Drawing.Point ( 58 , 22 ) ;
this.textBox1.Name = "textBox1" ;
this.textBox1.Size = new System.Drawing.Size ( 162 , 21 ) ;
this.textBox1.TabIndex = 1 ;
this.textBox1.Text = "" ;
this.button2.FlatStyle = System.Windows.Forms.FlatStyle.Flat ;
this.button2.Location = new System.Drawing.Point ( 86 , 168 ) ;
this.button2.Name = "button2" ;
this.button2.Size = new System.Drawing.Size ( 98 , 30 ) ;
this.button2.TabIndex = 3 ;
this.button2.Text = "写入INI文件" ;
this.button2.Click += new System.EventHandler ( this.button2_Click ) ;
this.textBox2.Location = new System.Drawing.Point ( 160 , 62 ) ;
this.textBox2.Name = "textBox2" ;
this.textBox2.Size = new System.Drawing.Size ( 176 , 21 ) ;
this.textBox2.TabIndex = 5 ;
this.textBox2.Text = "" ;
this.textBox3.Location = new System.Drawing.Point ( 160 , 94 ) ;
this.textBox3.Name = "textBox3" ;
this.textBox3.Size = new System.Drawing.Size ( 176 , 21 ) ;
this.textBox3.TabIndex = 6 ;
this.textBox3.Text = "" ;
this.textBox4.Location = new System.Drawing.Point ( 160 , 128 ) ;
this.textBox4.Name = "textBox4" ;
this.textBox4.Size = new System.Drawing.Size ( 176 , 21 ) ;
this.textBox4.TabIndex = 7 ;
this.textBox4.Text = "" ;
this.label1.Location = new System.Drawing.Point ( 56 , 62 ) ;
this.label1.Name = "label1" ;
this.label1.TabIndex = 8 ;
this.label1.Text = "段落名称:" ;
this.label2.Location = new System.Drawing.Point ( 66 , 96 ) ;
this.label2.Name = "label2" ;
this.label2.TabIndex = 9 ;
this.label2.Text = "关键字:" ;
this.label3.Location = new System.Drawing.Point ( 42 , 130 ) ;
this.label3.Name = "label3" ;
this.label3.TabIndex = 10 ;
this.label3.Text = "关键字数值:" ;
this.button3.FlatStyle = System.Windows.Forms.FlatStyle.Flat ;
this.button3.Location = new System.Drawing.Point ( 208 , 168 ) ;
this.button3.Name = "button3" ;
this.button3.Size = new System.Drawing.Size ( 98 , 30 ) ;
this.button3.TabIndex = 11 ;
this.button3.Text = "读取INI数值" ;
this.button3.Click += new System.EventHandler ( this.button3_Click ) ;
this.openFileDialog1.Filter = "INI 文件|*.ini" ;
this.AutoScaleBaseSize = new System.Drawing.Size ( 6 , 14 ) ;
this.ClientSize = new System.Drawing.Size ( 366 , 217 ) ;
this.Controls.AddRange ( new System.Windows.Forms.Control [ ] {
this.button3 ,
this.textBox4 ,
this.textBox3 ,
this.textBox2 ,
this.button2 ,
this.textBox1 ,
this.button1 ,
this.label3 ,
this.label2 ,
this.label1 } ) ;
this.MaximizeBox = false ;
this.Name = "Form1" ;
this.Text = "C#操作INI文件--写操作" ;
this.ResumeLayout ( false ) ;
}
[STAThread]
static void Main ( )
{
Application.Run ( new Form1 ( ) ) ;
}
private void button1_Click ( object sender , System.EventArgs e )
{
openFileDialog1.ShowDialog ( ) ;
textBox1.Text = openFileDialog1.FileName ;
}
//写入INI文件
private void button2_Click ( object sender , System.EventArgs e )
{
string FileName = textBox1.Text ;
string section = textBox2.Text ;
string key = textBox3.Text ;
string keyValue = textBox4.Text ;
WritePrivateProfileString ( section , key , keyValue , FileName ) ;
MessageBox.Show ( "成功写入INI文件!" , "信息" ) ;
}
//读取指定INI文件的特定段落中的关键字的数值
private void button3_Click ( object sender , System.EventArgs e )
{
StringBuilder temp = new StringBuilder ( 255 ) ;
string FileName = textBox1.Text ;
string section = textBox2.Text ;
string key = textBox3.Text ;
int i = GetPrivateProfileString ( section , key ,
"无法读取对应数值!" , emp , 255 , FileName ) ;
//显示读取的数值
textBox4.Text = temp.ToString ( ) ;
}
}
}