File 文件
1.创建文件
File.Create(@"D:\Test\Debug1\测试.txt");
需要扩展名
2.打开文件
File.Open(@"D:\Test\Debug1\测试.txt",FileMode.Append);
3.追加文件内容
File.AppendAllText(@"D:\Test\Debug1\测试.txt","哈哈");
4.复制文件
File.Copy(@"D:\Test\Debug1\测试.txt", @"D:\Test\Debug2\测试1.txt", true);
true表示是否覆盖相同文件
5.移动文件
File.Move(@"D:\Test\Debug1\测试.txt", @"D:\Test\Debug3\测试2.txt");
6.删除文件
File.Delete(@"D:\Test\Debug1\测试.txt");
7.设置文件属性
File.SetAttributes(@"D:\Test\Debug1\测试.txt", FileAttributes.Hidden);
隐藏
Directory 目录
1.创建文件夹
Directory.CreateDirectory(@"C:\a");
2.删除文件夹
Directory.Delete(@"C:\a",true);
3.移动文件夹
Directory.Move(@"C:\a",@"C:\Users\Sp\Des\new);
4.获得指定文件夹下所有文件的全路径
string[] path=Directory.GetFiles(@"C:\Users\Sp\Des\Pic","*.jpg");
5.获得指定目录下所有文件夹的全路径
string[] path=Directory.GetDirectories(@"C:\Users\Sp\Des");
6.判断指定的文件夹是否存在
Directory.Exists(@"C:\a\b")
7.目录属性设置
设置目录为只读,隐藏。(与文件属性相同,目录属性也是使用FileAttributes来设置)
DirectoryInfo newDirInfo=new DirectoryInfo(@"C:\temp\NewDirectoty");
newDirInfo.Attributes=FileAttributes.ReadOnly|FileAttributes.Hidden;
PATH 路径
string filePath =@"E:/Randy0528/中文目录/JustTest.rar";
1.更改路径字符串的扩展名。
System.IO.Path.ChangeExtension(filePath, "txt");
输出:E:/Randy0528/中文目录/JustTest.txt
2.返回指定路径字符串的目录信息。
System.IO.Path.GetDirectoryName(filePath);
输出:E:/Randy0528/中文目录
3.返回指定的路径字符串的扩展名。
System.IO.Path.GetExtension(filePath);
输出:.rar
4.返回指定路径字符串的文件名和扩展名。
System.IO.Path.GetFileName(filePath);
输出:JustTest.rar
5.返回不具有扩展名的指定路径字符串的文件名。
System.IO.Path.GetFileNameWithoutExtension(filePath);
输出:JustTest