C#操作目录与文件的方法步骤

所属分类: 软件编程 / C#教程 阅读数: 120
收藏 0 赞 0 分享
• 创建目录和文件
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);

(2)通过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#开发word批量转pdf源码分享

已经安装有Office环境,借助一些简单的代码即可实现批量Word转PDF,看下面的实例源码吧
收藏 0 赞 0 分享

c# xml API操作的小例子

这篇文章主要介绍了c# xml API操作的小例子,有需要的朋友可以参考一下
收藏 0 赞 0 分享

c#唯一值渲染实例代码

这篇文章主要介绍了c#唯一值渲染实例代码,有需要的朋友可以参考一下
收藏 0 赞 0 分享

淘宝IP地址库采集器c#代码

这篇文章主要介绍了淘宝IP地址库采集器c#代码,有需要的朋友可以参考一下
收藏 0 赞 0 分享

C#在后台运行操作(BackgroundWorker用法)示例分享

BackgroundWorker类允许在单独的专用线程上运行操作。如果需要能进行响应的用户界面,而且面临与这类操作相关的长时间延迟,则可以使用BackgroundWorker类方便地解决问题,下面看示例
收藏 0 赞 0 分享

c#文本加密程序代码示例

这是一个加密软件,但只限于文本加密,加了窗口控件的滑动效果,详细看下面的代码
收藏 0 赞 0 分享

c#生成站点地图(SiteMapPath)文件示例程序

这篇文章主要介绍了c#生成站点地图(SiteMapPath)文件的示例,大家参考使用
收藏 0 赞 0 分享

C# 键盘Enter键取代Tab键实现代码

这篇文章主要介绍了C# 键盘Enter键取代Tab键实现代码,有需要的朋友可以参考一下
收藏 0 赞 0 分享

C# WinForm导出Excel方法介绍

在.NET应用中,导出Excel是很常见的需求,导出Excel报表大致有以下三种方式:Office PIA,文件流和NPOI开源库,本文只介绍前两种方式
收藏 0 赞 0 分享

C#串口通信程序实例详解

在.NET平台下创建C#串口通信程序,.NET 2.0提供了串口通信的功能,其命名空间是System.IO.Ports,创建C#串口通信程序的具体实现是如何的呢?让我们开始吧
收藏 0 赞 0 分享
查看更多