笔记: C#文件操作方法学习
2018-02-01 15:16
190 查看
学习笔记:文件目录创建、文件拷贝、文件删除、文件移动、文件夹移动、文件夹删除、获取文件信息
获取app所在资源根路径:
public static string get_app_path()
{
string app_name = Process.GetCurrentProcess().MainModule.FileName;
string path = Path.GetDirectoryName(app_name);
return path;
}
创建文件目录:
public void create_dir(string path)
{
DirectoryInfo folder = new DirectoryInfo(path);
if (!folder.Exists)
Directory.CreateDirectory(path);
}
文件拷贝:
public void copy_file(string path)
{
string[] path_segs = path.Split('\\');
string name = path_segs[path_segs.Length - 1];
string save_path = Path.Combine(get_app_path(), name);
FileInfo file = new FileInfo(path);
if (file.Exists)
{
file.CopyTo(save_path, true);
}
}
文件删除:
public void delete_file(string path)
{
FileInfo file = new FileInfo(path);
if (file.Exists)
{
file.Delete();
}
}
文件夹删除:
public void delete_folder(string path)
{
DirectoryInfo folder = new DirectoryInfo(path);
if (folder.Exists)
{
folder.Delete(true);
}
}
文件移动:
public void move_file(string file_path, string dst_path)
{
FileInfo file = new FileInfo(file_path);
if (file.Exists)
{
file.MoveTo(dst_path);
}
}
文件夹移动:
public void move_folder(string folder_path, string dst_path)
{
DirectoryInfo folder = new DirectoryInfo(folder_path);
if (folder.Exists)
{
folder.MoveTo(dst_path);
}
}
获取文件的信息:
public FileInfo get_file_info(string path)
{
FileInfo file = new FileInfo(path);
if (!file.Exists)
throw new FileNotFoundException("File not found: " + path);
Console.WriteLine("创建时间:" + file.CreationTime.ToString());
Console.WriteLine("文件大小:" + file.Length.ToString());
return file;
}
打印指定目录下所有文件名称、及所有子文件夹测试:
public void test(path)
{
DirectoryInfo folder = new DirectoryInfo(path);
if (!folder.Exists)
throw new DirectoryNotFoundException("Folder not found: " + path);
foreach (DirectoryInfo sub_folder in folder.GetDirectories())
{
Console.WriteLine(sub_folder.Name);
}
foreach (FileInfo file in folder.GetFiles())
{
Console.WriteLine(file.Name);
}
}
获取app所在资源根路径:
public static string get_app_path()
{
string app_name = Process.GetCurrentProcess().MainModule.FileName;
string path = Path.GetDirectoryName(app_name);
return path;
}
创建文件目录:
public void create_dir(string path)
{
DirectoryInfo folder = new DirectoryInfo(path);
if (!folder.Exists)
Directory.CreateDirectory(path);
}
文件拷贝:
public void copy_file(string path)
{
string[] path_segs = path.Split('\\');
string name = path_segs[path_segs.Length - 1];
string save_path = Path.Combine(get_app_path(), name);
FileInfo file = new FileInfo(path);
if (file.Exists)
{
file.CopyTo(save_path, true);
}
}
文件删除:
public void delete_file(string path)
{
FileInfo file = new FileInfo(path);
if (file.Exists)
{
file.Delete();
}
}
文件夹删除:
public void delete_folder(string path)
{
DirectoryInfo folder = new DirectoryInfo(path);
if (folder.Exists)
{
folder.Delete(true);
}
}
文件移动:
public void move_file(string file_path, string dst_path)
{
FileInfo file = new FileInfo(file_path);
if (file.Exists)
{
file.MoveTo(dst_path);
}
}
文件夹移动:
public void move_folder(string folder_path, string dst_path)
{
DirectoryInfo folder = new DirectoryInfo(folder_path);
if (folder.Exists)
{
folder.MoveTo(dst_path);
}
}
获取文件的信息:
public FileInfo get_file_info(string path)
{
FileInfo file = new FileInfo(path);
if (!file.Exists)
throw new FileNotFoundException("File not found: " + path);
Console.WriteLine("创建时间:" + file.CreationTime.ToString());
Console.WriteLine("文件大小:" + file.Length.ToString());
return file;
}
打印指定目录下所有文件名称、及所有子文件夹测试:
public void test(path)
{
DirectoryInfo folder = new DirectoryInfo(path);
if (!folder.Exists)
throw new DirectoryNotFoundException("Folder not found: " + path);
foreach (DirectoryInfo sub_folder in folder.GetDirectories())
{
Console.WriteLine(sub_folder.Name);
}
foreach (FileInfo file in folder.GetFiles())
{
Console.WriteLine(file.Name);
}
}
相关文章推荐
- C#学习笔记15:字符串、文件、目录的操作方法
- C#学习笔记(十六):使用打开保存文件对话框
- C#学习笔记(二十一):使用文件基础
- C#中的文件流学习笔记第一篇
- C#复制文件和文件夹(学习笔记)
- (C#)学习笔记1:文件和注册表操作
- C#学习笔记(二十一):使用文件基础
- 【C#学习笔记】播放wma/mp3文件
- log4net按日期生成日志文件-C#学习笔记
- 黑马程序员之C#学习笔记: xml文件读取
- [C#学习笔记]判断文件文件夹是否存在
- C#学习笔记(十六):使用打开保存文件对话框
- 【C#学习笔记】打开对话框并返回打开文件所在路径
- 【C#学习笔记】写文件
- C#学习笔记(十六):使用打开保存文件对话框
- 【C#学习笔记】保存文件
- C#学习笔记:使用文件基础
- 【C#学习笔记】读文件
- C#学习笔记(二十一):使用文件基础
- 黑马程序员之C#学习笔记:C#读取txt文件的方法