初学C#不久,现在需要读取一个文档,并修改特定字段!尝试了半天,但是程序不工作。。
下面是一个按钮事件的处理,需要读取某字段,并修改!
下面是一个按钮事件的处理,需要读取某字段,并修改!
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
|
private
void
button5_Click(
object
sender, EventArgs e)
{
openFileDialog1.ShowDialog(
this
);
openFileDialog1.Filter =
"LUA文件|*.lua|所有文件|*.*"
;
//设置文件类型;
openFileDialog1.AutoUpgradeEnabled =
true
;
//是否随系统升级而升级外观;
if
( openFileDialog1.ShowDialog(
this
) == System.Windows.Forms.DialogResult.OK)
{
textBox1.Text = openFileDialog1.FileName;
Stream read_stream = File.OpenRead(openFileDialog1.FileName);
if
(File.Exists(openFileDialog1.FileName))
//看看文件是否存在;
{
/*
FileStream fs = File.OpenRead(openFileDialog1.FileName);//存在的话旧打开文件;
byte[] str = new byte[20000];//定义数组来读取数据;
while (fs.Read(str, 0, str.Length) > 0)//开始从文本中读取数据;
{
Console.WriteLine(System.Text.Encoding.Default.GetString(str));//将读取的数据写出来 到屏幕上;
textBox1.Text = System.Text.Encoding.Default.GetString(str);
}
fs.Close();//关闭文件;
*/
}
}
}
|