分享一篇关于C#对文件操作的日志,方法很全

Code:
  1. using System;    
  2. using System.Collections.Generic;    
  3. using System.ComponentModel;    
  4. using System.Data;    
  5. using System.Drawing;    
  6. using System.Linq;    
  7. using System.Text;    
  8. using System.Windows.Forms;    
  9. //引入命名空间    
  10. using System.IO;    
  11.   
  12. namespace NsFileOperate    
  13. {    
  14.     public partial class FileOperate : Form    
  15.     {    
  16.         public FileOperate()    
  17.         {    
  18.             InitializeComponent();    
  19.         }    
  20.         /// <summary>    
  21.         /// 方法名称:btnOpenFile_Click    
  22.         /// 方法作用:打开文件方法一    
  23.         /// 作者:心语    
  24.         /// 完成时间:2010年5月25日19:49:29    
  25.         /// </summary>    
  26.         /// <param name="sender"></param>    
  27.         /// <param name="e"></param>    
  28.         private void btnOpenFile_Click(object sender, EventArgs e)    
  29.         {    
  30.             //设置标题    
  31.             this.openFileDialog1.Title = "打开文件";    
  32.             //设置默认路径    
  33.             this.openFileDialog1.InitialDirectory = "d://";    
  34.             //设置文件类型    
  35.             this.openFileDialog1.Filter = "心语文件*.dxl|*.dxl|文本文件*.txt|*.txt|所有文件|*.*";    
  36.             //设置显示文件类型    
  37.             this.openFileDialog1.FilterIndex = 1;    
  38.             //关闭对话框时是否还原当前目录    
  39.             this.openFileDialog1.RestoreDirectory = true;    
  40.             //获取或设置默认文件扩展名    
  41.             this.openFileDialog1.DefaultExt = ".txt";    
  42.             //判断是否选择了打开按钮    
  43.             if (this.openFileDialog1.ShowDialog() == DialogResult.OK)    
  44.             {    
  45.                 //获得文件路径    
  46.                 string path = this.openFileDialog1.FileName;//注意位置    
  47.                 //创建文件流    
  48.                 FileStream file = new FileStream(path, FileMode.Open);    
  49.                 //创建读取器    
  50.                 StreamReader reader = new StreamReader(file);    
  51.                 //显示读取内容    
  52.                 this.txtFileName.Text = reader.ReadToEnd();    
  53.             }    
  54.         }    
  55.         /// <summary>    
  56.         /// 方法名称:btnOpenFile2_Click    
  57.         /// 方法作用:打开文件方法二    
  58.         /// 作者:心语    
  59.         /// 完成时间:2010年5月25日19:55:31    
  60.         /// </summary>    
  61.         /// <param name="sender"></param>    
  62.         /// <param name="e"></param>    
  63.         private void btnOpenFile2_Click(object sender, EventArgs e)    
  64.         {    
  65.             //创建打开对话框对象    
  66.             OpenFileDialog open = new OpenFileDialog();    
  67.             //默认路径    
  68.             open.InitialDirectory = "d://";    
  69.             //文本格式筛选    
  70.             open.Filter = "心语文件*.dxl|*.dxl|文本文件*.txt|*.txt|所有文件|*.*";    
  71.             //设置显示文件类型    
  72.             open.FilterIndex = 1;    
  73.             //关闭对话框时是否还原当前目录    
  74.             open.RestoreDirectory = true;    
  75.             //调用打开对话框方法    
  76.             if (open.ShowDialog() == DialogResult.OK)    
  77.             {    
  78.                 //取得文件路径    
  79.                 string path = open.FileName;    
  80.                 //创建文件流    
  81.                 FileStream filestream = new FileStream(path, FileMode.Open);    
  82.                 //创建byte数组    
  83.                 byte[] bt = new byte[filestream.Length];    
  84.                 //调用read读取方法    
  85.                 filestream.Read(bt, 0, bt.Length);    
  86.                 //以Unicode编码方式显示文本    
  87.                 this.txtFileName.Text = Encoding.Unicode.GetString(bt);    
  88.             }    
  89.         }    
  90.         /// <summary>    
  91.         /// 方法名称:btnSaveFile_Click    
  92.         /// 方法作用:保存文件    
  93.         /// 作者:心语    
  94.         /// 完成时间:2010年5月25日20:01:48    
  95.         /// </summary>    
  96.         /// <param name="sender"></param>    
  97.         /// <param name="e"></param>    
  98.         private void btnSaveFile_Click(object sender, EventArgs e)    
  99.         {    
  100.             //创建保存对话框对象    
  101.             SaveFileDialog save = new SaveFileDialog();    
  102.             //默认路径    
  103.             save.InitialDirectory = "d://";    
  104.             //文本格式筛选    
  105.             save.Filter = "心语文件*.dxl|*.dxl|文本文件*.txt|*.txt";    
  106.             //设置显示文件类型    
  107.             save.FilterIndex = 1;    
  108.             //关闭对话框时是否还原当前目录    
  109.             save.RestoreDirectory = true;    
  110.             //调用保存对话框方法    
  111.             if (save.ShowDialog() == DialogResult.OK)    
  112.             {    
  113.                 //取得文件路径    
  114.                 string path = save.FileName;    
  115.                 //设置默认文件扩展名    
  116.                 save.DefaultExt = ".txt";    
  117.                 //创建写入器对象    
  118.                 StreamWriter sw = new StreamWriter(path);    
  119.                 //调用写入方法    
  120.                 sw.WriteLine(txtFileName.Text);    
  121.                 //关闭写入器    
  122.                 sw.Close();    
  123.             }    
  124.         }    
  125.         /// <summary>    
  126.         /// 方法名称:btnCopy_Click    
  127.         /// 方法作用:复制文件    
  128.         /// 作者:心语    
  129.         /// 完成时间:2010年5月25日20:05:11    
  130.         /// </summary>    
  131.         /// <param name="sender"></param>    
  132.         /// <param name="e"></param>    
  133.         private void btnCopy_Click(object sender, EventArgs e)    
  134.         {    
  135.             //检查是否一存在文件    
  136.             if (File.Exists("D://CopyToFile//Copy.txt") == true)    
  137.             {    
  138.                 MessageBox.Show("目标文件夹已有此文件!""提示", MessageBoxButtons.OK, MessageBoxIcon.Asterisk);    
  139.             }    
  140.             else    
  141.             {    
  142.                 //复制    
  143.                 File.Copy("D://Copy.txt""D://CopyToFile//Copy.txt");    
  144.                 MessageBox.Show("复制成功!""提示", MessageBoxButtons.OK, MessageBoxIcon.Asterisk);    
  145.             }    
  146.         }    
  147.         /// <summary>    
  148.         /// 方法名称:btnMove_Click    
  149.         /// 方法作用:移动文件    
  150.         /// 作者:心语    
  151.         /// 完成时间:2010年5月25日20:11:22    
  152.         /// </summary>    
  153.         /// <param name="sender"></param>    
  154.         /// <param name="e"></param>    
  155.         private void btnMove_Click(object sender, EventArgs e)    
  156.         {    
  157.             if (File.Exists("D://MoveToFile//Move.txt") == true)    
  158.             {    
  159.                 MessageBox.Show("目标文件夹已有此文件!""提示", MessageBoxButtons.OK, MessageBoxIcon.Asterisk);    
  160.             }    
  161.             else    
  162.             {    
  163.                 //移动    
  164.                 File.Move("D://Move.txt""D://MoveToFile//Move.txt");    
  165.                 MessageBox.Show("移动成功!""提示", MessageBoxButtons.OK, MessageBoxIcon.Asterisk);    
  166.             }    
  167.         }    
  168.         /// <summary>    
  169.         /// 方法名称:btnDelete_Click    
  170.         /// 方法作用:删除文件    
  171.         /// 作者:心语    
  172.         /// 完成时间:2010年5月25日20:14:46    
  173.         /// </summary>    
  174.         /// <param name="sender"></param>    
  175.         /// <param name="e"></param>    
  176.         private void btnDelete_Click(object sender, EventArgs e)    
  177.         {    
  178.             if (Directory.Exists("D://Move.txt") == false)    
  179.             {    
  180.                 MessageBox.Show("目标文件夹不存在此文件!""提示", MessageBoxButtons.OK, MessageBoxIcon.Asterisk);    
  181.             }    
  182.             else    
  183.             {    
  184.                 //删除    
  185.                 Directory.Delete("D://Move.txt");    
  186.                 MessageBox.Show("删除成功!""提示", MessageBoxButtons.OK, MessageBoxIcon.Asterisk);    
  187.             }    
  188.         }    
  189.     }    
  190. }    
  191.   
 
原文地址:https://www.cnblogs.com/JerryWang1991/p/3936397.html