C#操作目录和文件
- 创建目录和文件
1、通过Path类的Combine方法可以合并路径。
string activeDir = @"C:\myDir";
string newPath = System.IO.Path.Combine(activeDir, "mySubDirOne");
2、目录的创建。
创建目录时如果目录已存在,则不会重新创建目录,且不会报错。创建目录时会自动创建路径中各级不存在的目录。 (1)通过Directory类的CreateDirectory方法创建。
string activeDir = @"C:\myDir";
string newPath = System.IO.Path.Combine(activeDir, "mySubDirOne");
System.IO.Directory.CreateDirectory(newPath);
(1)通过DirectoryInfo的对象创建。
System.IO.DirectoryInfo di = new System.IO.DirectoryInfo(@"C:\myDirTwo\mySubDirThree");
di.Create();
3、文件的创建。
通过Create方法创建文件,会覆盖同名的现有文件。创建文件时,该文件所在路径的目录必须存在,否则报错。
(1)通过File类的Create方法创建。

string activeDir = @"C:\myDir";
string newPath = System.IO.Path.Combine(activeDir, "mySubDirOne");
System.IO.Directory.CreateDirectory(newPath); //创建一个空白文件
string fileNameOne = DateTime.Now.ToString("yyyyMMddHHmmssffff")
+ ".txt";
string filePathOne = System.IO.Path.Combine(newPath, fileNameOne);
System.IO.File.Create(filePathOne);

(2)通过FileInfo对象创建。

//通过Combine合并目录
//然后创建目录
string activeDir = @"C:\myDir";
string newPath = System.IO.Path.Combine(activeDir, "mySubDirOne");
System.IO.Directory.CreateDirectory(newPath); //创建一个空白文件
string fileNameOne = DateTime.Now.ToString("yyyyMMddHHmmssffff")
+ ".txt";
string filePathOne = System.IO.Path.Combine(newPath, fileNameOne);
System.IO.FileInfo fi = new System.IO.FileInfo(filePathOne);
fi.Create();

- 复制目录文件

//复制单个文件到指定目录
string fileName = "test.txt";
string sourcePath = @"C:\testDir\subTestDir";
string targetPath = @"C:\testDir\subTestDirTwo"; string sourceFile = System.IO.Path.Combine(sourcePath, fileName);
string destFile = System.IO.Path.Combine(targetPath, fileName); if (!System.IO.Directory.Exists(targetPath))
System.IO.Directory.CreateDirectory(targetPath); //如果已存在,参数为false时将报错,参数为true重写该文件
//当copy方法为两个参数时,默认重写为false。
System.IO.File.Copy(sourceFile, destFile, true); //以下为复制一个目录下所有文件到指定目录
//如果复制有子目录的目录的所有文件,可以用递归或堆栈算法实现
if (System.IO.Directory.Exists(sourcePath))
{
string[] files = System.IO.Directory.GetFiles(sourcePath); foreach (string s in files)
{
//仅返回路径字符串的文件名及后缀
fileName = System.IO.Path.GetFileName(s);
destFile = System.IO.Path.Combine(targetPath, fileName);
System.IO.File.Copy(s, destFile, true);
}
} }

- 移动目录和文件

/*移动文件*/
string sourceFile = @"C:\testDir\subTestDir\test.txt";
string destFile = @"C:\testDir\subTestDirTwo\test.txt";
//当目标文件存在时,抛出异常
System.IO.File.Move(sourceFile, destFile); /*移动目录*/
//移动目录将移动改目录的子目录和文件
System.IO.Directory.Move(@"C:\testDir\subTestDirTwo\", @"C:\testDir\subTestDir");

- 删除目录和文件
1、删除目录
删除目录,如果该目录不存在,会抛出异常。可以通过File类的Delete方法删除目录,也可以通过FileInfo对象方法删除目录。
(1)通过 File类的Delete方法删除目录

//删除可写空目录
//如果不为空抛出目录不为空异常
try
{
System.IO.Directory.Delete(@"C:\testDir\subTestDir");
}
catch (System.IO.IOException e)
{
Console.WriteLine(e.Message);
} //第二参数为false时,只能删除空目录,否则抛出不为空异常
//第二参数为true时,删除目录,包括子目录和文件
try
{
System.IO.Directory.Delete(@"C:\testDir\subTestDir", true);
}
catch(System.IO.IOException e)
{
Console.WriteLine(e.Message);
}

(2)通过FileInfo对象方法删除目录

System.IO.DirectoryInfo di = new System.IO.DirectoryInfo(@"C:\testDir\subTestDirTwo");
try
{
//无参数删除空目录
//当参数为false,可删除空目录;为true,删除目录,包括子目录和文件
di.Delete(true);
}
catch (System.IO.IOException e)
{
Console.WriteLine(e.Message);
}

2、删除文件
删除文件时如果指定文件的目录存在,而文件不存在,则不会抛出异常,如果指定文件的目录不存在,则会抛出异常。
(1)通过File类Delete方法删除文件

try
{
System.IO.File.Delete(@"C:\testDir\subTestDir\test.txt");
}
catch(System.IO.IOException e)
{
Console.WriteLine(e.Message);
}

(2)通过FileInfo对象Delete方法删除文件

System.IO.FileInfo fi = new System.IO.FileInfo(@"C:\testDir\subTestDir\test1.txt");
try
{
fi.Delete();
}
catch(System.IO.IOException e)
{
Console.WriteLine(e.Message);
}

C#操作目录和文件的更多相关文章
- (转载)Linux入门:操作目录和文件的命令
PATH 每个用户的PATH都是不一样的: PATH中不包含“当前目录”: (1)echo $PATH:显示PATH环境变量: (2)PATH = "$PATH":/home/ ...
- SVNKit学习——使用低级别的API(ISVNEditor接口)直接操作Repository的目录和文件(五)
本文是参考官方文档的实现,官方wiki:https://wiki.svnkit.com/Committing_To_A_Repository 本文核心使用的是ISVNEditor这个接口直接对Re ...
- Python获取目录、文件的注意事项
Python获取指定路径下的子目录和文件有两种方法: os.listdir(dir)和os.walk(dir),前者列出dir目录下的所有直接子目录和文件的名称(均不包含完整路径),如 >> ...
- Python --判断路径是否为目录或文件
os.path.isdir( ), os.path.isfile(),os.listdir( ), os.walk( ) 参考网址:https://blog.csdn.net/xxn_723911/a ...
- Linux学习笔记 -- 目录与文件的管理
目录结构 Linux的目录结构为树状结构,最顶级的目录为根目录 “/”. 其他目录通过挂载可以将它们添加到树中,通过解除挂载可以移除它们. 在开始本教程前我们需要先知道什么是. 绝对路径与相对路径 绝 ...
- [Python] 目录和文件操作
在Linux系统下用Python写脚本,肯定不能避免各种与目录和文件夹有关的操作.为了以后方便查阅,简单地针对Python中与目录和文件夹有关的操作进行汇总. 需要实现导入的模块为: import o ...
- python目录和文件操作
一.python中对文件.文件夹操作时经常用到的os模块和shutil模块常用方法. 要操作目录,必须引入os模块 import os1.得到当前工作目录,即当前Python脚本工作的目录路径: os ...
- AIR文件操作(二):使用文件对象操作文件和目录
转载于:http://www.flashj.cn/wp/air-file-operation2.html 文件对象是啥?文件对象(File对象)是在文件系统中指向文件或目录的指针.由于安全原因,只在A ...
- (第三周)wc.exe—命令行实现对指定目录下文件的操作
一.用户需求 程序处理用户需求的模式为: wc.exe [parameter][filename] 在[parameter]中,用户通过输入参数与程序交互,需实现的功能如下: 1.基本功能 支持 -c ...
随机推荐
- How to solve the error "Field service in com.xx.xxx.xxxx required a bean of type 'com.aa.bb.cc' that could not be found."
When runung a SpringBoot demo, I got a error as following: *************************** APPLICATION ...
- shell 并发进程的例子
linux shell 实现后台多进程运行的,开始和终止 原创 2014年11月21日 12:04:51 9953 linux shell 实现后台开始,和停止多进程 知识点: xxxxcmd.sh ...
- 什么是 ANR 如何避免它?
在 Android 上,如果你的应用程序有一段时间响应不够灵敏,系统会向用户显示一个对话框,这个对话框称作应用程序无响应(ANR:Application Not Responding)对话框.用户可以 ...
- 【8】ie css hack
1. "\9" IE6/IE7/IE8/IE9/IE10都生效 "\0" IE8/IE9/IE10都生效,是IE8/9/10的hack "\9 ...
- pip Fatal error in launcher: Unable to create process using '""'
如果你装了python2.7, python3.5, 在两个版本的兼容问题上折腾很久了, 通过修改环境变量, 能够出现下面的界面, 恭喜你, 暂时解决了一些问题, 哈哈
- 无界面上(linux)运行jmeter(2)
无界面上(linux)运行jmeter 1.先在bin目录下面创建一个文件夹testplan用来存放脚本(.jmx文件),然后在创建一个文件夹testresult用来存放脚本执行后的结果(.jtl文件 ...
- 浅谈Excel开发:二 Excel 菜单系统(转)
编辑器加载中...http://www.cnblogs.com/yangecnu/p/Excel-Menu-System-Introduction.html 在开始Excel开发之前,需要把架子搭起来 ...
- mingw 编译 libopus 1.3.1 时 注意事项
OPUS_STACK_PROTECTOR 默认是使用的, 在 windows 上编译时一定要去掉选项不然 -lopus 链接时出现错误undefined reference to `__stack_c ...
- VIM常用操作手册
VIM常用操作手册 1.多行操作,多行注释,多行取消注释 https://jingyan.baidu.com/article/9c69d48f43ed6d13c8024e7b.html 2.常用操作 ...
- 报错:java.lang.NoClassDefFoundError: com/google/inject/Injector
使用testng report,导入jar包:reportng.jar和velocity-dep-1.4.jar后,执行脚本,报错如下: 缺少依赖的jar包:guice-4.0.jar 导入依赖的ja ...