File类,是一个静态类,主要是来提供一些函数库用的。静态实用类,提供了不少静态的方法,支持对文件的基本操做,包括建立,拷贝,移动,删除和 打开一个文件。html
File类方法的参量不少时候都是路径path。File的一些方法能够返回FileStream和StreamWriter的对象。能够 和他们配套使用。System.IO.File类和System.IO.FileInfo类数组
主要提供有关文件的各类操做,在使用时须要引用System.IO命名空间。app
1、File类经常使用的操做方法函数
一、建立文件方法布局
//参数1:要建立的文件路径测试
File.Create(@"D:\Test\Debug1\测试.txt")this
二、打开文件方法spa
//参数1:要打开的文件路径,参数2:打开的文件方式orm
File.Open(@"D:\Test\Debug1\测试.txt",FileMode.Append)htm
三、追加文件方法
//参数1:要追加的文件路径,参数2:追加的内容
File.AppendAllText(@"D:\Test\Debug1\测试.txt","哈哈");
四、复制文件方法
//参数1:要复制的源文件路径,参数2:复制后的目标文件路径,参数3:是否覆盖相同文件名
File.Copy(@"D:\Test\Debug1\测试.txt", @"D:\Test\Debug2\测试1.txt", true);
五、移动文件方法
//参数1:要移动的源文件路径,参数2:移动后的目标文件路径
File.Move(@"D:\Test\Debug1\测试.txt", @"D:\Test\Debug3\测试2.txt");
六、删除文件方法
//参数1:要删除的文件路径
File.Delete(@"D:\Test\Debug1\测试.txt");
七、设置文件属性方法
//参数1:要设置属性的文件路径,参数2:设置的属性类型(只读、隐藏等)
File.SetAttributes(@"D:\Test\Debug1\测试.txt", FileAttributes.Hidden);
2、界面和源码例子:
一、界面布局
二、源码例子:
using System; using System.Collections.Generic; using System.ComponentModel; using System.Data; using System.Drawing; using System.IO; using System.Linq; using System.Text; using System.Threading.Tasks; using System.Windows.Forms; namespace FileHandleTest { public partial class Form1 : Form { public Form1() { InitializeComponent(); } #region Files类的文件操做方法(建立、复制、删除、移动、追加、打开、设置属性等) static string path = @"D:\Test\Debug1\测试.txt"; //源文件路径 static string path1 = @"D:\Test\Debug2\测试1.txt"; //文件复制路径 static string path2 = @"D:\Test\Debug3\测试2.txt"; //文件移动路径 static string path3 = @"C:\测试3.txt"; //跨盘符存放路径(测试) /// <summary> /// 一、建立文件方法 /// </summary> /// <param name="sender"></param> /// <param name="e"></param> private void btncreate_Click(object sender, EventArgs e) { //参数1:指定要判断的文件路径 if (!File.Exists(path)) { //参数1:要建立的文件路径,包含文件名称、后缀等 FileStream fs = File.Create(path); fs.Close(); MessageBox.Show("文件建立成功!"); } else { MessageBox.Show("文件已经存在!"); } } /// <summary> ///二、 打开文件的方法 /// </summary> /// <param name="sender"></param> /// <param name="e"></param> private void btnopen_Click(object sender, EventArgs e) { if (File.Exists(path)) { //参数1:要打开的文件路径,参数2:打开的文件方式 FileStream fs = File.Open(path, FileMode.Append); //字节数组 byte[] bytes = { (byte)'h', (byte)'e', (byte)'l', (byte)'l', (byte)'o' }; //经过字符流写入文件 fs.Write(bytes, 0, bytes.Length); fs.Close(); MessageBox.Show("打开并追加Hello成功!"); } else { MessageBox.Show("文件不存在!"); } } /// <summary> /// 三、追加文件内容方法 /// </summary> /// <param name="sender"></param> /// <param name="e"></param> private void btnappend_Click(object sender, EventArgs e) { string appendtext = this.txtContent.Text; if (File.Exists(path)) { //参数1:要追加的文件路径,参数2:追加的内容 File.AppendAllText(path, appendtext); MessageBox.Show("文件追加内容成功!"); } else { MessageBox.Show("文件不存在!"); } } /// <summary> /// 四、复制文件方法(只能在同个盘符进行操做) /// </summary> /// <param name="sender"></param> /// <param name="e"></param> private void btncopy_Click(object sender, EventArgs e) { if (File.Exists(path)) { //参数1:要复制的源文件路径,参数2:复制后的目标文件路径,参数3:是否覆盖相同文件名 File.Copy(path, path1, true); MessageBox.Show("复制文件成功!"); } else { MessageBox.Show("文件不存在!"); } } /// <summary> /// 五、移动文件方法(只能在同个盘符进行操做) /// </summary> /// <param name="sender"></param> /// <param name="e"></param> private void btnmove_Click(object sender, EventArgs e) { if (File.Exists(path)) { //参数1:要移动的源文件路径,参数2:移动后的目标文件路径 File.Move(path, path2); MessageBox.Show("移动文件成功!"); } else { MessageBox.Show("文件不存在!"); } } /// <summary> /// 六、删除文件方法 /// </summary> /// <param name="sender"></param> /// <param name="e"></param> private void btndelete_Click(object sender, EventArgs e) { if (File.Exists(path)) { //参数1:要删除的文件路径 File.Delete(path); MessageBox.Show("文件删除成功!"); } else { MessageBox.Show("文件不存在!"); } } /// <summary> ////七、设置文件属性方法 /// </summary> /// <param name="sender"></param> /// <param name="e"></param> private void btnset_Click(object sender, EventArgs e) { if (File.Exists(path)) { //参数1:要设置属性的文件路径,参数2:设置的属性类型(只读、隐藏等) File.SetAttributes(path, FileAttributes.Hidden); MessageBox.Show("设置文件属性为隐藏成功!"); } else { MessageBox.Show("文件不存在!"); } } #endregion } }
参考来源:http://www.cnblogs.com/mfc-itblog/p/5771780.html