文件及文件夹操作:
一、File类
1.创建:
FileStream fs = File.Create(文件路径);
记住,创建完成后,需要关闭fs,否则别人无法使用这个文件.
FileStream fs = File.Create(@"E:aaa.txt");
fs.Close();
//using (FileStream fs = File.Create(@"E:aaa.txt"))
//{
// StreamWriter sw = new StreamWriter(fs, Encoding.Default);
// sw.WriteLine("Hello World");
// sw.Close();
//}
2、删除
File.Delete(@"E:aaa.txt");
3、复制文件
File.Copy(@"e: est.txt", @"d: ew.txt");
4、判断文件是否存在
bool isExisted = File.Exists(@"e: est.txt");
if (isExisted == true)
{
MessageBox.Show("文件已存在");
}
else
{
MessageBox.Show("OK");
}
5、文件移动
File.Move(@"E: est.txt", @"d:aaa.txt");
6、获取创建时间
DateTime dt = File.GetCreationTime(@"D:aaa.txt");
this.Text = dt.ToString("yyyy-MM-dd hh:mm:ss");
7、获取访问时间
DateTime dt = File.GetLastAccessTime(@"D:aaa.txt");
this.Text = dt.ToString("yyyy-MM-dd hh:mm:ss");
8、获取最后修改时间
DateTime dt = File.GetLastWriteTime(@"D:aaa.txt");
this.Text = dt.ToString("yyyy-MM-dd hh:mm:ss");
9、修改创建时间
DateTime time = DateTime.Now.AddSeconds(10000);
File.SetCreationTime(@"D:aaa.txt",time);
10、修改访问时间
DateTime time = DateTime.Now.AddSeconds(-1000);
File.SetLastAccessTime(@"D:aaa.txt", time);
11、修改最后修改时间
DateTime time = DateTime.Now.AddSeconds(-20000);
File.SetLastWriteTime(@"D:aaa.txt", time);
12、文件改名
File.Move(@"d:aaa.txt", @"d: est.txt");
13、创建目录
Directory.CreateDirectory(@"d:mydir");
14、删除目录
Directory.Delete(@"d:mydir");
15、目录是否存在
MessageBox.Show(Directory.Exists(@"d:mydir").ToString());
16、获取子目录
string[] dirs = Directory.GetDirectories(@"c:windows");
foreach (string s in dirs)
{
label1.Text += " " + s;
}
17、获取子文件
string[] files = Directory.GetFiles(@"c:windows");
foreach (string s in files)
{
label1.Text += " " + s;
}
18、获取根目录
this.Text = Directory.GetDirectoryRoot(@"C:WIndows");
19、获取上一级
DirectoryInfo di = Directory.GetParent(@"C:windowsmicrosoft.net");
this.Text = di.Name;
二、FileInfo类
1、创建文件
FileInfo file = new FileInfo(@"E: est.txt");
FileStream stream = file.Create();
stream.Close();
2、删除文件
FileInfo info = new FileInfo(@"E: est.txt");
info.Delete();
3、复制文件
FileInfo info = new FileInfo(@"E: est.txt");
info.CopyTo(@"D:aaa.txt",true);
4、移动文件
FileInfo info = new FileInfo(@"E: est.txt");
info.MoveTo(@"D: est1.txt");
5、文件是否存在
FileInfo info = new FileInfo(@"E: est.txt");
bool b = info.Exists;
MessageBox.Show(b.ToString());
6、获取文件名
FileInfo info = new FileInfo(@"E: est.txt");
string s = info.FullName;
MessageBox.Show(s);
7、获取创建时间
FileInfo info = new FileInfo(@"E: est.txt");
DateTime dt = info.CreationTime;
MessageBox.Show(dt.ToString());
8、获取访问时间
FileInfo info = new FileInfo(@"E: est.txt");
DateTime dt = info.LastAccessTime;
//DateTime dt = File.GetLastAccessTime(@"E: est.txt");
MessageBox.Show(dt.ToString());
9、获取修改时间
FileInfo info = new FileInfo(@"E: est.txt");
DateTime dt = info.LastWriteTime;
MessageBox.Show(dt.ToString());
10、设置创建时间
FileInfo info = new FileInfo(@"E: est.txt");
info.CreationTime = DateTime.Now.AddDays(1000);
11、设置修改时间
FileInfo info = new FileInfo(@"E: est.txt");
info.LastWriteTime = DateTime.Now.AddDays(1000);
12、设置访问时间
FileInfo info = new FileInfo(@"E: est.txt");
info.LastAccessTime = DateTime.Now.AddDays(1000);
13、创建目录
DirectoryInfo info = new DirectoryInfo(@"E:MyDir");
info.Create();
14、删除目录
DirectoryInfo info = new DirectoryInfo(@"E:MyDir");
info.Delete();
15、移动目录
DirectoryInfo info = new DirectoryInfo(@"E:MyDir");
info.MoveTo(@"D:YourDir");
16、目录是否存在
DirectoryInfo info = new DirectoryInfo(@"E:MyDir");
bool b = info.Exists;
this.Text = b.ToString();
17、获取目标全名
DirectoryInfo info = new DirectoryInfo(@"E:MyDir");
//this.Text = info.FullName;
this.Text = info.Name;
18、获取子文件
DirectoryInfo info = new DirectoryInfo(@"C:Windows");
//FileInfo[] files = info.GetFiles();
FileInfo[] files = info.GetFiles("*.exe");
foreach (FileInfo data in files)
{
string s = "<文件>"+ data.FullName + " " + data.Extension + " " + data.CreationTime.ToString();
listBox1.Items.Add(s);
}
//string[] files = Directory.GetFiles();
19、获取子目录
DirectoryInfo info = new DirectoryInfo(@"C:Windows");
DirectoryInfo[] dirs = info.GetDirectories();
foreach (DirectoryInfo data in dirs)
{
string s = "<目录>" + data.FullName + " " + data.CreationTime.ToString();
listBox1.Items.Add(s);
}
20、获取驱动信息
DriveInfo[] drivers = DriveInfo.GetDrives();
foreach (DriveInfo dr in drivers)
{
if (dr.DriveType != DriveType.CDRom)
{
listBox1.Items.Add(dr.Name + " " + dr.DriveType + " " + dr.VolumeLabel + " " + dr.TotalSize + " " + dr.TotalFreeSpace + " " + dr.DriveFormat);
}
else
{
listBox1.Items.Add(dr.Name + " " + dr.DriveType );
}
}