File类,是一个静态类,主要是来提供一些函数库用的。静态实用类,提供了很多静态的方法,支持对文件的基本操作,包括创建,拷贝,移动,删除和 打开一个文件。
File类方法的参量很多时候都是路径path。File的一些方法可以返回FileStream和StreamWriter的对象。可以 和他们配套使用。System.IO.File类和System.IO.FileInfo类
主要提供有关文件的各种操作,在使用时需要引用System.IO命名空间。
一、File类常用的操作方法
1、创建文件方法
//参数1:要创建的文件路径
File.Create(@"D:\Test\Debug1\测试.txt")
2、打开文件方法
//参数1:要打开的文件路径,参数2:打开的文件方式
File.Open(@"D:\Test\Debug1\测试.txt",FileMode.Append)
3、追加文件方法
//参数1:要追加的文件路径,参数2:追加的内容
File.AppendAllText(@"D:\Test\Debug1\测试.txt","哈哈");
4、复制文件方法
//参数1:要复制的源文件路径,参数2:复制后的目标文件路径,参数3:是否覆盖相同文件名
File.Copy(@"D:\Test\Debug1\测试.txt", @"D:\Test\Debug2\测试1.txt", true);
5、移动文件方法
//参数1:要移动的源文件路径,参数2:移动后的目标文件路径
File.Move(@"D:\Test\Debug1\测试.txt", @"D:\Test\Debug3\测试2.txt");
6、删除文件方法
//参数1:要删除的文件路径
File.Delete(@"D:\Test\Debug1\测试.txt");
7、设置文件属性方法
//参数1:要设置属性的文件路径,参数2:设置的属性类型(只读、隐藏等)
File.SetAttributes(@"D:\Test\Debug1\测试.txt", FileAttributes.Hidden);
二、界面和源码例子:
1、界面布局
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>
/// 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
}
}