在C#中怎么修改记事本中的内容?比如我想修改记事本中的某一行?

吴青峰 2008-06-25 09:58:51
在C#中怎么修改记事本中的内容?比如我想修改记事本中的某一行?
...全文
511 13 打赏 收藏 转发到动态 举报
写回复
用AI写文章
13 条回复
切换为时间正序
请发表友善的回复…
发表回复
OICQshenshi 2011-08-12
  • 打赏
  • 举报
回复
学习学习!
dadongdong511 2008-06-26
  • 打赏
  • 举报
回复
路过,学习学习!
bagegejin 2008-06-26
  • 打赏
  • 举报
回复
学习学习
rainxy2000 2008-06-26
  • 打赏
  • 举报
回复
刚查到,notepad其实也是一个COM组件, 你可以直接在用NotepadOM.Application这个名字来调用它.

详细地你要查查这个COM的资料了.

比如:
npad = CreateObject("NotepadOM.Application")
npad.InvokeMenu 10 //NOTEPAD_FILE_OPEN
rainxy2000 2008-06-26
  • 打赏
  • 举报
回复
没办法,只能用WinAPI了.

1. FindWindow找到你要的记事本窗口句柄Hwnd.
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;
}

我姓区不姓区 2008-06-26
  • 打赏
  • 举报
回复

//读取文本 
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();

needanothercoder 2008-06-26
  • 打赏
  • 举报
回复
mark
wzy_love_sly 2008-06-25
  • 打赏
  • 举报
回复 1
/*当文档状态为新建文档状态时,提示用户输入保存路径.*/
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;
}


保存后重新读取
wzy_love_sly 2008-06-25
  • 打赏
  • 举报
回复
string fname;                                              //局部变量,保存文件名;下同.
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;
}


打开文件
wzy_love_sly 2008-06-25
  • 打赏
  • 举报
回复
加我qq,给你个例子,260831750
wzy_love_sly 2008-06-25
  • 打赏
  • 举报
回复
报告楼主,俺是sql版的
--------------------------
看看读取全部,然后修改,保存后覆盖原文件
amandag 2008-06-25
  • 打赏
  • 举报
回复
记事本只能逐行读取
吴青峰 2008-06-25
  • 打赏
  • 举报
回复

110,570

社区成员

发帖
与我相关
我的任务
社区描述
.NET技术 C#
社区管理员
  • C#
  • Web++
  • by_封爱
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告

让您成为最强悍的C#开发者

试试用AI创作助手写篇文章吧