C#的目录与文件操作

 

  C#中对文件和目录的操作均可以分为两种方式,即静态调用和基于对象调用。静态调用主要是Directory和File两类,而基于对象的调用则是基于,DirectoryInfo和FileInfo两类。两种调用方式主要有两种不同,静态调用之前会进行较多的检查,如身份检查等等,而ji'y基于对象的调用则需要创建对象来调用相关的方法。

  目录的静态方法调用相对十分简单,只需要设置好文件夹路径,而后将其作为参数传入Directory静态类的相应方法中即可调用,一般对文件夹的操作可分我查询、复制、移动、删除等等。

代码较为简单,如下图所示:(说明一下,本博客中的各类均在引用System.IO中)

//Display subdirectorty information(在同一类下面引发的方法必须为静态)
        static void DisplayFolder(string folderPath)
        {
            DirectoryInfo theFolder = new DirectoryInfo(folderPath);
            string folderName = folderPath.Substring(22);
            //Console.WriteLine(folderName);
            if (!theFolder.Exists)
                throw new DirectoryNotFoundException("Folder is not found" + folderName);
            //list all subdirectory in folder
            foreach (DirectoryInfo subFolder in theFolder.GetDirectories())
            {
                Console.WriteLine(subFolder.Name);
            }
            //list all files in folder
            Console.WriteLine("\nfiles:");
            foreach (FileInfo file in theFolder.GetFiles())
            {
                Console.WriteLine(file.Name);
            }
        }
 //Move folder
        static void MoveDirectory(string sourceFolderName, string destFolderName)
        {
            if(Directory.Exists(sourceFolderName))
            {
                Directory.Move(sourceFolderName, destFolderName);
                Console.WriteLine("File has been moved");
            }   
              
        }
 //Copy file
        static void CopyFile(string sourceFileName, string destFileName)
        {
            if (File.Exists(sourceFileName))
            {
                File.Copy(sourceFileName, destFileName);
                Console.WriteLine("File has been copyed");
            }

        }
//Delete folder
        static void DeleteDirectory(string folderPath)
        {
            DirectoryInfo folderInfo = new DirectoryInfo(folderPath);
            if (folderInfo.Exists)
            {
                folderInfo.Delete();//该目录下必须为空,若有文件需要先删除文件
                Console.WriteLine("Folder has been deleted");
            }
            else
                Console.WriteLine("Folder is not found");
        }

  对文件的操作的逻辑相似,需要设置好文件路径,而后调用相应方法即可,基于对象的方式需要先创建相应的实例,相应代码

如下所示:(由于对文件的读写操作相对来说比较复杂,因此本文介绍对文件的简单操作如查询、删除等等,其他的诸如复制、创建的操作依葫芦画瓢,十分简单,便不再赘述)

//Display file information
        static void DisplayFileInfo(string folderPath, string fileName)
        {
            string fileFullName = Path.Combine(folderPath, fileName);
            FileInfo fileInfo = new FileInfo(fileFullName);
            if (!fileInfo.Exists)
                throw new FileNotFoundException("File is not found" + fileName);
            Console.WriteLine(string.Format("Creation time: {0}", fileInfo.CreationTime.ToString()));
            Console.WriteLine(string.Format("size: {0} bytes", fileInfo.Length.ToString()));
        }
 //Delete file
        static void DeleteFile(string folderPath, string fileName)
        {
            string fileFullName = Path.Combine(folderPath, fileName);
            FileInfo fileInfo = new FileInfo(fileFullName);
            if (fileInfo.Exists)
            {
                fileInfo.Delete();
                Console.WriteLine("File has been deleted");
            }
                
            else
                Console.WriteLine("File is not found");
        }

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

lemon_tttea

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值