C#中File类文件操作详解
File类
File类是一个静态类,提供了很多对文件操作的静态方法,包括创建,复制,移动,删除,打开文件。和文件相关的参数基本都是路径path。其返回值有的是FileStream 和 StreamWriter 的对象。
1.打开文件:public static FileStream Open(sting path,FileMode mode)
private void OpenFile() { FileStream.TextFile=File.Open(@"c:\tempuploads\newFile.txt",FileMode.Append); byte [] Info = {(byte)'h',(byte)'e',(byte)'l',(byte)'l',(byte)'o'}; TextFile.Write(Info,0,Info.Length); TextFile.Close(); }
2.文件创建:public static FileStream Create(string path)
private void MakeFile() { FileStream NewText=File.Create(@"c:\tempuploads\newFile.txt"); NewText.Close();//由于File.Create方法默认向所有用户授予对新文件的完全读/写访问权限,所以文件是用读/写访问权限打开的,必须关闭后才能由其他应用程序打开。为此,所以需要使用FileStream类的Close方法将所创建的文件关闭。 }
3.删除文件:public static void Delete(string path)
private void DeleteFile() { File.Delete(@"c:\tempuploads\newFile.txt"); }
4.复制文件:public static void Copy(string sourceFile,string destFile.bool overwrite)
private void CopyFile() { File.Copy(@"c:\tempuploads\newFile.txt",@"c:\tempuploads\BackUp.txt",true); //true 时,如果文件存在,就直接覆盖 }
5.移动文件(仅限同一个磁盘):public static void Move(string sourceFile,string destFile)
private void MoveFile() { File.Move(@"c:\tempuploads\BackUp.txt",@"c:\BackUp.txt"); }
6.设置文件属性(只读和隐藏等) public static void SetAttributes(string path,FileAttributes fileAttributes)
private void SetFile() { File.SetAttributes(@"c:\tempuploads\newFile.txt", FileAttributes.ReadOnly|FileAttributes.Hidden);
//Archive(文件存档状态) System(系统文件) Temporary(临时文件) }
7.文件是否存在:public static bool Exists(string path)
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> /// 1、创建文件方法 /// </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> ///2、 打开文件的方法 /// </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> /// 3、追加文件内容方法 /// </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> /// 4、复制文件方法(只能在同个盘符进行操作) /// </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> /// 5、移动文件方法(只能在同个盘符进行操作) /// </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> /// 6、删除文件方法 /// </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> ////7、设置文件属性方法 /// </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 } }

浙公网安备 33010602011771号