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

解决方案 »

  1.   

    报告楼主,俺是sql版的
    --------------------------
    看看读取全部,然后修改,保存后覆盖原文件
      

  2.   

    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;
                }打开文件
      

  3.   

    /*当文档状态为新建文档状态时,提示用户输入保存路径.*/
                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;
                }保存后重新读取
      

  4.   


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

  5.   

    没办法,只能用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; 
       }    
      

  6.   

    刚查到,notepad其实也是一个COM组件, 你可以直接在用NotepadOM.Application这个名字来调用它.详细地你要查查这个COM的资料了.比如:
    npad = CreateObject("NotepadOM.Application") 
    npad.InvokeMenu 10 //NOTEPAD_FILE_OPEN