C# 文本文档操作——读取、保存、编辑

来源:互联网 发布:电子书软件 编辑:程序博客网 时间:2024/05/29 02:00
 

using System.IO;

//文本文档读入richtextbox中进行编辑
private void edit()
{
    this.richtb.Text = "";////新建
    this.richtb.Undo();////撤销
    this.richtb.SelectAll();////全选
    this.richtb.Paste();////粘贴
    this.richtb.Cut();////剪切
    this.richtb.Copy();////复制
    ////粘贴
    if (this.richtb.SelectedText != "")
    {
        this.richtb.SelectedText = "";
    }
    ////插入现在时间
    DateTime dt = DateTime.Now;
    this.richtb.AppendText(dt.ToString());
 
//读路径为string strFile的文本文档,或可用记事本正常打开的文档
////////方法一:一行一行读/////////
private void txtRead(string strFile)
{
    StreamReader myStreamReader;          
    try
    {
        string tline;
        myStreamReader = new StreamReader(strFile);
        while (myStreamReader.Peek() != -1)
        {
            tline = myStreamReader.ReadLine();
        }
        myStreamReader.Close();
    }
    catch (Exception ex)
                
        myStreamReader.Close();
        MessageBox.Show(ex.Message);
    }
}
方法二:整个读出来,作为一个字符串处理
private void txtRead()//索引数据:将机型文档导入到数组airfile中 idexclumrow()
{
    //若文档中有中文,就要用下面的格式
    StreamReader sr = new StreamReader(@"Airtype.txt", Encoding.GetEncoding("gb2312"));
    string sFile = sr.ReadToEnd();
    string[] vals = sFile.Split('\n');
    for (int i = 0; i < vals.Length; i++)
    {
        string sLine = vals[i];
        string[] sVal = sLine.Split('\r');       
    }
    sr.Dispose ();          
}
//将制定内容保存为文本或excel表格
 public void saveAs()
{
    //先拖一个saveFileDialog控件
    saveFileDialog1.Filter = "文本文档(*.txt)|*.txt|Excel文件(*.xls)|*.xls|所有文件(*.*)|*.*";
    if (saveFileDialog1.ShowDialog() == DialogResult.OK)
    {
        saveFileDialog1.RestoreDirectory = true;
        saveFileDialog1.CreatePrompt = true;
        Stream myStream=saveFileDialog1.OpenFile();
        StreamWriter sw = new StreamWriter(myStream, System.Text.Encoding.GetEncoding(-0));
        string str = "";
        try
        {
            //写入标题
            for (int i = 0; i < dayDataGridView.ColumnCount; i++)
            {
                if (i > 0)
                {
                    str += "\t";
                }
                str += dayDataGridView.Columns[i].HeaderText;
            }
            sw.WriteLine(str);
            //写内容
            for (int j = 0; j < dayDataGridView.Rows.Count - 1; j++)
            {
                string strTemp = "";
                for (int k = 0; k < dayDataGridView.Columns.Count; k++)
                {
                    if (k > 0)
                    {
                        strTemp += "\t";
                    }
                    strTemp += dayDataGridView.Rows[j].Cells[k].Value.ToString();
                }
                sw.WriteLine(strTemp);
            }
            sw.Close();
            myStream.Close();
        }
        catch (Exception ex)
                    
            myStream.Close();
            MessageBox.Show(ex.Message);
        }
        finally
        {
            sw.Close();
            myStream.Close();
        }
    }
}

原创粉丝点击