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 ...
随机推荐
- jquery 登录判断遇到的小问题
1.碰到的第一个问题是: 往body上加载check,用load不管用,可以用ready试试. 2.原来jquery里获取用的val(),我一直以为是value()... 尴尬 3.两个标志位是为了判 ...
- c#端口扫描器wpf+socket
布局如下 <Window x:Class="PortTest.MainWindow" xmlns="http://schemas.microsoft.com/win ...
- loadrunner 场景设计-手工场景方案(Schedule)设计 Part 1
参考:http://blog.sina.com.cn/s/articlelist_5314188213_1_1.html loadrunner 场景设计-手工场景方案(Schedule)设计 Part ...
- java:Review(J2ee)
1.oracle: 1.1 增:insert into 删:delete from 改:update tablename set 查:select * from 1.2 聚合函数 max,min,av ...
- const char* to char*(当函数传递参数时)
来自 https://blog.csdn.net/rongrongyaofeiqi/article/details/52442169 https://blog.csdn.net/hebbely/art ...
- Spring cloud 项目———酷派手机商城 (话术)1.0
酷派电商网站 描述: 随着电子商务的发展,网上购物正在趋于一种时尚,电子商务网站也逐渐成为企业顺应潮流的标配.大多数人知道可能在电子商务网站前端有查询,注册登录,购物车等等功能.可是您知道建设电子商 ...
- P4995 跳跳!
喵喵喵好久没做过贪心的题目了,刷一下免得忘了嘤嘤嘤 题面 虽然是黄题,但是我承认并不是很难,so看代码吧还是.. #include<set> #include<map> #in ...
- 【LeetCode】714、买卖股票的最佳时机含手续费
Best Time to Buy and Sell Stock with Transaction Fee 题目等级:Medium 题目描述: Your are given an array of in ...
- 【神经网络与深度学习】卷积神经网络-进化史:从LeNet到AlexNet
[卷积神经网络-进化史]从LeNet到AlexNet 本博客是[卷积神经网络-进化史]的第一部分<从LeNet到AlexNet> 如需转载,请附上本文链接:http://blog.csdn ...
- spring boot 数据源配置YAML格式
spring: datasource: url: jdbc:mysql://localhost:3306/test?useUnicode=true&characterEncoding=utf- ...