一, 文件夹
1. 选择文件夹
- var sfd = new System.Windows.Forms.FolderBrowserDialog();
- var rfn = sfd.ShowDialog();
- if (rfn == System.Windows.Forms.DialogResult.OK)
- {
- folderPath = sfd.SelectedPath; // 文件夹路径
- }
2. 文件夹相关操作
C# 文件夹相关操作主要使用 Directory 类和 DirectoryInfo 类.
Directory 类中的常用方法有:
Exists: 判断文件夹是否存在
CreateDirectory: 创建新文件夹
Delete: 删除文件夹
Move(oldPath,newPath): 移动文件夹到指定路径(不可以跨磁盘)
GetFiles: 获得文件夹下的文件列表
GetDirectories: 获得子文件夹列表
GetDirectorieyRoot: 获取文件夹根目录
GetParent: 获取父文件夹名称
DirectoryInfo 类的常用属性及方法有:
Name: 文件夹名
Parent: 父文件夹名
Root: 根目录
Create: 创建新文件夹
CreateSubdirectory: 创建子文件夹
Delete: 删除文件夹
Exists: 判断文件夹是否存在
MoveTo(newPath): 移动文件夹到指定路径(不可以跨磁盘)
GetFiles: 获得文件夹下的文件列表
GetDirectories: 获得子文件夹列表
- string folderPath = @"e:\CodeSource\C#\FileOperations";
- if (Directory.Exists(folderPath))
- {
- Directory.Delete(folderPath);
- if (!Directory.Exists(folderPath))
- {
- Console.WriteLine($"删除 {folderPath} 成功");
- }
- }
- if (!Directory.Exists(folderPath))
- {
- Directory.CreateDirectory(folderPath);
- if (Directory.Exists(folderPath))
- {
- Console.WriteLine($"创建 {folderPath} 成功");
- }
- }
- string newPath = $@"{Directory.GetParent(folderPath).Parent.FullName}\FileOperations";
- Directory.Move(folderPath,newPath);
- DirectoryInfo dir = new DirectoryInfo(newPath);
- if (!dir.Exists)
- {
- Console.WriteLine($"移动 {folderPath} 到{newPath}失败");
- dir.Create();
- }
- else
- {
- Console.WriteLine($"移动 {folderPath} 到{newPath}成功");
- }
- if (dir.Exists)
- {
- dir.CreateSubdirectory("NewFolder");
- dir.MoveTo(folderPath);
- }
二, 文件
1. 选择文件
- var ofd = new OpenFileDialog
- {
- Title = "选择配置文件",
- Filter = "Xml 文件(*.xml)|*.xml"
- };
- if (ofd.ShowDialog() == true){
- filePath= ofd.FileName; // 文件路径
- }
2. 文件相关操作
C# 文件相关操作主要使用 File 类和 FileInfo 类.
File 类中的常用方法有:
Exists: 判断文件是否存在
Open: 打开文件
Create: 创建文件
Copy: 复制文件(不可以跨磁盘)
Delete: 删除文件
Move: 移动文件(不可以跨磁盘)
Replace: 替换文件
AppendAllText: 文件不存在新建文件并添加文本, 存在则追加文本
ReadAllText: 打开并读取文本内容
FileInfo 类的常用属性及方法有:
Name: 文件名
Directory: 文件所在目录
Exists: 判断文件是否存在
Extension: 文件扩展名
Length: 文件长度
IsReadOnly: 判断文件是否为只读状态
Open: 打开文件
Create: 创建新文件
Delete: 删除文件
MoveTo: 移动文件
Replace: 替换文件
CopyTo: 复制到新文件
EnCrypt: 加密文件
Decrypt: 解密文件
SetAttributes: 设置文件属性
- string FilePath1 = @"E:\CodeSource\C#\FileOperations\File1.txt"; // 源文件路径
- string FilePath2 = @"E:\CodeSource\C#\FileOperations\File2.txt"; // 文件复制路径 1
- string FilePath3 = @"E:\CodeSource\C#\FileOperationsCopy\File2.txt"; // 文件复制路径 2
- string FilePath4 = @"E:\CodeSource\C#\FileOperationsCopy\File1.txt"; // 文件移动路径
- if (!File.Exists(FilePath1))
- {
- FileStream fs = File.Create(FilePath1);
- fs.Close();
- }
- if (File.Exists(FilePath1))
- {
- Console.WriteLine("文件创建成功!");
- FileStream fs = File.Open(FilePath1, FileMode.Append); //FileMode 为枚举类型, 表示文件打开方式
- string text = "FileAppendTextByByte";
- byte[] bytes = Convert.FromBase64String(text);//string 转字节数组
- fs.Write(bytes, 0, bytes.Length); // 以字节流方式向文件尾写入内容
- fs.Close();
- Console.WriteLine("打开并写入内容成功!");
- string appendtext = "FileAppendTextByText";
- File.AppendAllText(FilePath1, appendtext); // 向文件尾添加内容
- Console.WriteLine("文件尾添加内容成功!");
- File.Copy(FilePath1, FilePath2, false); // 复制文件不可以跨盘操作, 如果覆盖同名目标文件, 则为 true, 否则为 false
- File.Copy(FilePath1, FilePath3, true);
- Console.WriteLine("复制文件成功!");
- File.Move(FilePath1, FilePath4);
- Console.WriteLine("移动文件成功!");
- File.Delete(FilePath4);
- Console.WriteLine("删除文件成功!");
- File.SetAttributes(FilePath2, FileAttributes.Hidden);
- Console.WriteLine("设置文件属性为隐藏成功!");
来源: http://www.jianshu.com/p/934b3caf0a33