C#操作目录与文件的方法步骤
2023-06-13 09:15:00 时间
•创建目录和文件
1、通过Path类的Combine方法可以合并路径。
复制代码代码如下:
stringactiveDir=@"C:\myDir";
stringnewPath=System.IO.Path.Combine(activeDir,"mySubDirOne");
创建目录时如果目录已存在,则不会重新创建目录,且不会报错。创建目录时会自动创建路径中各级不存在的目录。
stringactiveDir=@"C:\myDir";
stringnewPath=System.IO.Path.Combine(activeDir,"mySubDirOne");
System.IO.Directory.CreateDirectory(newPath);
System.IO.DirectoryInfodi=newSystem.IO.DirectoryInfo(@"C:\myDirTwo\mySubDirThree");
di.Create();
通过Create方法创建文件,会覆盖同名的现有文件。创建文件时,该文件所在路径的目录必须存在,否则报错。
stringactiveDir=@"C:\myDir";
stringnewPath=System.IO.Path.Combine(activeDir,"mySubDirOne");
System.IO.Directory.CreateDirectory(newPath);
//创建一个空白文件
stringfileNameOne=DateTime.Now.ToString("yyyyMMddHHmmssffff")
+".txt";
stringfilePathOne=System.IO.Path.Combine(newPath,fileNameOne);
System.IO.File.Create(filePathOne);
//通过Combine合并目录
//然后创建目录
stringactiveDir=@"C:\myDir";
stringnewPath=System.IO.Path.Combine(activeDir,"mySubDirOne");
System.IO.Directory.CreateDirectory(newPath);
//创建一个空白文件
stringfileNameOne=DateTime.Now.ToString("yyyyMMddHHmmssffff")
+".txt";
stringfilePathOne=System.IO.Path.Combine(newPath,fileNameOne);
System.IO.FileInfofi=newSystem.IO.FileInfo(filePathOne);
fi.Create();
•复制目录文件
//复制单个文件到指定目录
stringfileName="test.txt";
stringsourcePath=@"C:\testDir\subTestDir";
stringtargetPath=@"C:\testDir\subTestDirTwo";
stringsourceFile=System.IO.Path.Combine(sourcePath,fileName);
stringdestFile=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(stringsinfiles)
{
//仅返回路径字符串的文件名及后缀
fileName=System.IO.Path.GetFileName(s);
destFile=System.IO.Path.Combine(targetPath,fileName);
System.IO.File.Copy(s,destFile,true);
}
}
}
•移动目录和文件
/*移动文件*/
stringsourceFile=@"C:\testDir\subTestDir\test.txt";
stringdestFile=@"C:\testDir\subTestDirTwo\test.txt";
//当目标文件存在时,抛出异常
System.IO.File.Move(sourceFile,destFile);
/*移动目录*/
//移动目录将移动改目录的子目录和文件
System.IO.Directory.Move(@"C:\testDir\subTestDirTwo\",@"C:\testDir\subTestDir");
•删除目录和文件
//删除可写空目录
//如果不为空抛出目录不为空异常
try
{
System.IO.Directory.Delete(@"C:\testDir\subTestDir");
}
catch(System.IO.IOExceptione)
{
Console.WriteLine(e.Message);
}
//第二参数为false时,只能删除空目录,否则抛出不为空异常
//第二参数为true时,删除目录,包括子目录和文件
try
{
System.IO.Directory.Delete(@"C:\testDir\subTestDir",true);
}
catch(System.IO.IOExceptione)
{
Console.WriteLine(e.Message);
}
System.IO.DirectoryInfodi=newSystem.IO.DirectoryInfo(@"C:\testDir\subTestDirTwo");
try
{
//无参数删除空目录
//当参数为false,可删除空目录;为true,删除目录,包括子目录和文件
di.Delete(true);
}
catch(System.IO.IOExceptione)
{
Console.WriteLine(e.Message);
}
try
{
System.IO.File.Delete(@"C:\testDir\subTestDir\test.txt");
}
catch(System.IO.IOExceptione)
{
Console.WriteLine(e.Message);
}
System.IO.FileInfofi=newSystem.IO.FileInfo(@"C:\testDir\subTestDir\test1.txt");
try
{
fi.Delete();
}
catch(System.IO.IOExceptione)
{
Console.WriteLine(e.Message);
}
相关文章
- C# 操作XML文件 XmlDocument和XElement
- C#中AssemblyInfo.cs文件详解
- c# mysql executenonquery_C#与数据库访问技术总结(八)之ExecuteNonQuery方法
- 应用c#读取带cookie的http数据详解编程语言
- C操作Oracle数据库的引用实践(c# 引用 oracle)
- 比较有效的使用C#读取文件的代码
- asp.net(c#)中取得文件物理路径
- C#文件上传默认最大为4M的解决方法
- C#中执行批处理文件(*.bat)的方法代码
- C#读写文件的方法汇总
- c#使用linq技术创建xml文件的小例子
- asp.netC#实现下载文件的六种方法实例
- c#解压文件的实例方法
- C#操作config文件的具体方法
- C#执行bat批处理文件的小例子
- C#获取全部目录和文件的简单实例
- C#正则表达式分解和转换IP地址实例(C#正则表达式大全c#正则表达式语法)
- c#使用简单工厂模式实现生成html文件的封装类分享
- c#文件下载示例的4种方法分享
- C#探秘系列(一)——ToDictionary,ToLookup
- C#实现HTTP上传文件的方法
- C#获取文件夹及文件的大小与占用空间的方法
- C#读取二进制文件方法分析
- C#中的文件路径获取函数和文件名字获取函数小结
- C#WinForm编程获取文件物理路径的方法
- asp.net(C#)禁止缓存文件不让文件缓存到客户端