码迷,mamicode.com
首页 > Windows程序 > 详细

c#文件操作

时间:2016-01-09 18:25:38      阅读:299      评论:0      收藏:0      [点我收藏+]

标签:

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Threading;
using System.IO;  

namespace WindowsFormsApplication1
{
    public partial class Form2 : Form
    {
        public Form2()
        {
            InitializeComponent();
        }

        private void 粘贴vToolStripMenuItem_Click(object sender, EventArgs e)
        {
            richTextBox1.Text = richTextBox1.Text + selecttext;

        }

        private void 返回主菜单ToolStripMenuItem_Click(object sender, EventArgs e)
        {
            this.Close();
            Form1 frm2=new Form1();

            frm2.Show();
        }


        /// <summary>
        /// 打开文件对话框
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
       private void toolStripButton1_Click(object sender, EventArgs e)
        {
            OpenFileDialog dlg = new OpenFileDialog();
            dlg.CheckFileExists = true;
            dlg.Filter = "文本文件|*.txt|RTF文件|*.rtf|AllFile|*.*";        //过滤器 
            dlg.InitialDirectory = @"C:\Users\Administrator\Documents\360js Files";               //默认打开路径
            if (dlg.ShowDialog() == DialogResult.Cancel)     //取消按钮按下
            {
                return;
            }
            

            //try
            //{
            //    FileStream fs;               //文件流
            //    fs = new FileStream(dlg.FileName, FileMode.Open);
            //    StreamReader sr = new StreamReader(fs);
            //    //使用下面这条语句,就不需要上面的流的申明了
            //    //StreamReader sr = new StreamReader(dlg.FileName);     
            //    richTextBox1.Tag = dlg.FileName;       //用tag传递文件名
            //    string strLine = sr.ReadLine();
            //    while (strLine != null)
            //    {
            //        richTextBox1.AppendText(strLine);
            //        strLine = sr.ReadLine();
            //    }
            //    sr.Close();
            //}
            //catch (IOException ex)
            //{
            //    MessageBox.Show("文件操作出错" + "\n" + ex.Message.ToString());
            //}
            richTextBox1.LoadFile(dlg.FileName); //直接把文件放在richtexbox中,通过filename访问路径
        }
        /// <summary>
        /// 另存为文件对话框
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void toolStripButton2_Click(object sender, EventArgs e)
        {
            SaveFileDialog fsd = new SaveFileDialog();
            fsd.Filter="文本文件|*.txt|RTF文件|*.rtf|所有文件|*.*";
            fsd.ShowDialog();
            string strPatn = fsd.FileName;
            if (strPatn == "")
            {
                return;
            }
            //错误处理讲解
            //try
            //{
            //    FileStream fs;               //文件流
            //    fs = new FileStream(fsd.FileName, FileMode.OpenOrCreate);
            //    StreamWriter sw = new StreamWriter(fs);
            //    sw.Write(richTextBox1.Text);
            //    sw.Close();
            //}
            //catch (IOException ex)
            //{
            //    MessageBox.Show("文件操作出错" + "\n" + ex.Message.ToString());
            //}
            richTextBox1.SaveFile(fsd.FileName);
            
            //对话框按键判断
            if (MessageBox.Show("保存后是否清空画面内容", "警告", MessageBoxButtons.YesNo) == DialogResult.Yes)
            {
                richTextBox1.Text = "";
            }


            //string filename;
            //SaveFileDialog filesave = new SaveFileDialog();
            //string dir = Environment.GetFolderPath(Environment.SpecialFolder.Templates);
            //filesave.FilterIndex = 1;
            //filesave.DefaultExt = ".txt";
            //if (filesave.ShowDialog() == DialogResult.Cancel)
            //    return;
            //else
            //    filename = filesave.FileName;
            //richTextBox1.SaveFile(filename);     另存为代码,自动加后缀
        }



        /// <summary>
        /// 保存文件对话框
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void toolStripButton3_Click(object sender, EventArgs e)
        {
            SaveFileDialog sfd = new SaveFileDialog();
            sfd.Filter = "文本文件|*.txt|rtf文件|*.rtf|所有文件|*.*";
            sfd.Title = "保存";
            sfd.ShowDialog();
            string path = sfd.FileName;
            if (path == "")
            {
                return;
            }
            using (FileStream fsw = new FileStream(path, FileMode.OpenOrCreate))
            {
                byte[] buffer = Encoding.Default.GetBytes(richTextBox1.Text);
                  fsw.Write(buffer,0,buffer.Length);
            }
            richTextBox1.SaveFile(sfd.FileName);
            //try
            //{
            //    FileStream fs;               //文件流
            //    fs = new FileStream(richTextBox1.Tag.ToString(), FileMode.OpenOrCreate);
            //    StreamWriter sw = new StreamWriter(fs);
            //    sw.Write(richTextBox1.Text);

            //    sw.Close();
            //}
            //catch (IOException ex)
            //{
            //    MessageBox.Show("文件操作出错" + "\n" + ex.Message.ToString());
            //}
            //对话框按键判断
            if (MessageBox.Show("保存后是否清空画面内容", "警告", MessageBoxButtons.YesNo) == DialogResult.Yes)
            {
                richTextBox1.Text = "";
            }


            //string fileContent = richTextBox1.Text;
            //string path = saveFileDialog1.FileName;
            //saveFileDialog1.Filter = "文本文件|*.*|C#文件|*.cs|所有文件|*.*";
            //saveFileDialog1.FilterIndex = 2;
            //saveFileDialog1.RestoreDirectory = true;
            //saveFileDialog1.FileName = path;
            //System.IO.StreamWriter streamWrite = new System.IO.StreamWriter(saveFileDialog1.FileName);
            //streamWrite.Write(this.richTextBox1.Text);
            //streamWrite.Flush();
            //streamWrite.Close();
            //MessageBox.Show("保存成功");
            
        }
           

        private void tslProgress_Click_1(object sender, EventArgs e)
        {
            tsProgress.Value = 0;
            for (int i = 0; i < 10; i++)     //循环
            {
                Thread.Sleep(100);          //暂停1秒
                tsProgress.Value += tsProgress.Step;//让进度条增加一次  
            }
        }

        private void toolStripButton4_Click(object sender, EventArgs e)
        {
            if (richTextBox1.SelectedText.Trim() == "")
            {
                MessageBox.Show("请选定设置字体的文字。");
                richTextBox1.Select();
                return;
            }
            FontDialog fdl = new FontDialog();
            fdl.ShowDialog();
            richTextBox1.SelectionFont = fdl.Font;
        }

        private void toolStripButton5_Click(object sender, EventArgs e)
        {
            if (richTextBox1.SelectedText.Trim() == "")
            {
                MessageBox.Show("请选定设置颜色的文字。");
                richTextBox1.Select();
                return;
            }
            ColorDialog cdl = new ColorDialog();
            cdl.AnyColor = true;
            cdl.ShowDialog();
            richTextBox1.SelectionColor = cdl.Color;
        }



        private void 删除DToolStripMenuItem_Click(object sender, EventArgs e)
        {
            if (richTextBox1.SelectedText.Trim() == "")
            {
                MessageBox.Show("请选定要删除的文字。");
                richTextBox1.Select();
                return;
            }
            richTextBox1.SelectedText = "";
            }

        private void 删除ToolStripMenuItem_Click(object sender, EventArgs e)
        {
             if (richTextBox1.SelectedText.Trim() == "")
            {
                MessageBox.Show("请选定要删除的文字。");
                richTextBox1.Select();
                return;
            }
            richTextBox1.SelectedText = "";
        }


        string selecttext;
        private void 复制CToolStripMenuItem_Click(object sender, EventArgs e)
        {
           
             selecttext = richTextBox1.SelectedText;
                 
        }


        private void toolStripButton6_Click(object sender, EventArgs e)
        {
            AboutBox1 ab = new AboutBox1();
            ab.Show();
        }

        private void toolStripButton7_Click(object sender, EventArgs e)
        {
            OpenFileDialog openImageDlg = new OpenFileDialog();
            openImageDlg.Filter = "所有图片(*.bmp,*.gif,*.jpg)|*.bmp;*.gif;*jpg";
            openImageDlg.Title = "选择图片";
            Bitmap bmp;
            if (openImageDlg.ShowDialog() == DialogResult.OK)
            {
                string fileName = openImageDlg.FileName;
                if (null == fileName || fileName.Trim().Length == 0)
                    return;
                try
                {
                    bmp = new Bitmap(fileName);
                    Clipboard.SetDataObject(bmp);       //利用剪贴板
                    DataFormats.Format dataFormat =
                        DataFormats.GetFormat(DataFormats.Bitmap);
                    if (richTextBox1.CanPaste(dataFormat))
                        richTextBox1.Paste(dataFormat);
                }
                catch (Exception exc)
                {
                    MessageBox.Show("图片插入失败。" + exc.Message, "提示",
                                    MessageBoxButtons.OK, MessageBoxIcon.Information);
                }
            }
        }

        
    }
}

 

c#文件操作

标签:

原文地址:http://www.cnblogs.com/-slient/p/5116808.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!