在C#中怎么修改记事本中的内容?比如我想修改记事本中的某一行?
解决方案 »
- WPF中的Grid控件Height和Width设置不支持百分比?
- 求助,vs2008安装部署安装了windows服务,卸载的时候卸载不掉,在线等。
- 有关vs2008创建的office项目的问题
- 如何在线程中使用窗体控件的数据
- 用javascript返回的数组如何赋给.net中的datatable
- 请问,那为高手可以提供POP的BASE64解码和 提取附件的方法
- 云计算,到底什么是云计算。现在发现太多的企业在吹嘘这个词。
- 如何修改Explorer的默认连接设置
- winfrom 数据绑定的一个问题,求大师解答
- WPF,如何让单击CheckBox不变成选中状态
- 请问如何删除txt文本中的一条数据
- 请问,如何利用伪图片增加网站流量呢?
--------------------------
看看读取全部,然后修改,保存后覆盖原文件
openFileDialog1.FileName = "";
openFileDialog1.Filter = "文本文件(*.txt)|*.txt|所有文件(*.*)|*.*"; //设定文档过滤器,下同.
openFileDialog1.ShowDialog();
fname = openFileDialog1.FileName;
if (fname != "")
{
richTextBox1.LoadFile(fname, RichTextBoxStreamType.PlainText);
this.Text = fname;
changed = false;
saveable = true;
}打开文件
if (saveable == false)
{ string fname;
saveFileDialog1.Filter = "文本文件(*.txt)|*.txt|所有文件(*.*)|*.*";
saveFileDialog1.FilterIndex = 1;
saveFileDialog1.Title = "保存";
saveFileDialog1.InitialDirectory = Application.StartupPath; ;
saveFileDialog1.RestoreDirectory = true;
saveFileDialog1.ShowDialog();
fname = saveFileDialog1.FileName;
if (fname != "") //文件名不为空时,执行保存;否则放弃操作.
{
richTextBox1.SaveFile(fname, RichTextBoxStreamType.PlainText);
this.Text = this.Text.Substring(1, (this.Text.Length - 1));
/*下面一段代码为:保存文件后打开,下同.*/
richTextBox1.LoadFile(fname, RichTextBoxStreamType.PlainText);
this.Text = fname;
changed=false;
saveable=false;
}
}
/*否则在原文件进行保存*/
else
{
richTextBox1.SaveFile(this.Text.Substring(1, (this.Text.Length - 1)), RichTextBoxStreamType.PlainText);
this.Text = this.Text.Substring(1, (this.Text.Length - 1));
richTextBox1.LoadFile(this.Text, RichTextBoxStreamType.PlainText);
this.Text = this.Text;
changed = false;
}保存后重新读取
//读取文本
StreamReader sr = new StreamReader(文本文件的路径);
string str = sr.ReadToEnd();
sr.Close();
//替换文本
string [] result = str.Split("\n".ToCharArray());//数组中一个元素代表一行
result[index]="***";//想改哪行自己决定
//更改保存文本
StreamWriter sw = new StreamWriter(文本文件的路径,false);
foreach(string s in result)
sw.WriteLine(s);
sw.Close();
2. 再PostMessage 送出Ctrl + G键 定位到你要的那一行.
3. 选中你要的那一行. bool SelectText(int nFrom, int nTo) {
SendMessage(m_hwndEdit, EM_SETSEL, nFrom-1, nTo-1);
return S_OK;
} 4. 取得Text, 修改后再写回.
bool put_Text(BSTR newVal){
USES_CONVERSION;
SendMessage(m_hwndEdit, WM_SETTEXT, 0, (LPARAM) OLE2T(newVal));
return S_OK;
}
npad = CreateObject("NotepadOM.Application")
npad.InvokeMenu 10 //NOTEPAD_FILE_OPEN