这节讲两个实例类,FileInfo和DirectoryInfo两个类,用于操作某个具体的文件或者目录。...DirectoryInfo: 它的用法和属性,跟FileInfo差不多,但它有两个专有方法: DirectoryInfo di=new DirectoryInfo("./"); DirectoryInfo...parent = di.Parent; DirectoryInfo root = di.Root; Console.WriteLine(di.FullName); Console.WriteLine...那么,File,Directory和FileInfo,DirectoryInfo作用都是差不多的,具体在什么时候用呢,静态类的话,因为不需要实例化,所以在单次使用的情景下,可以使用File,Directory...,当需要多次使用的话,就可以使用FileInfo,DirectoryInfo。
(目录信息类) // 1、实例化新的DirectoryInfo对象并指定文件夹路径 DirectoryInfo directoryInfo = new DirectoryInfo("d:\\test")...; // 2、判断目录是否存在 bool isDirectoryInfoExist = directoryInfo.Exists; // 3、创建目录 directoryInfo.Create(); /...) { // 6、移动文件,将一个DirectoryInfo实例及其内容移动到一个新的路径;源目录或指定路径不存在则抛出异常 directoryInfo.MoveTo("d:\\test1..."); // 7、返回当前目录的子目录;目录或指定路径不存在则抛出异常 DirectoryInfo[] directoryInfos = directoryInfo.GetDirectories...:" + directoryInfo.Root); // d:\ Console.WriteLine("获取或设置当前文件或目录的属性:" + directoryInfo.Attributes)
directoryInfo = new DirectoryInfo(zipFilePath); if (directoryInfo.Parent !...= null) { directoryInfo = directoryInfo.Parent; }...directoryInfo.Exists) { directoryInfo.Create(); }...directoryInfo = new DirectoryInfo(zipFilePath); if (directoryInfo.Parent !...= null) { directoryInfo = directoryInfo.Parent; }
dir = new DirectoryInfo(path); if (!...dir = new DirectoryInfo(CreatePath); if (!...DirectoryInfo[] dis = d.GetDirectories();//获取目录子目录列表 foreach (DirectoryInfo di in dis)...diSource = new DirectoryInfo(sourceDirectory); DirectoryInfo diTarget = new DirectoryInfo...foreach (DirectoryInfo diSourceSubDir in source.GetDirectories()) { DirectoryInfo
root = new DirectoryInfo(path); FileInfo[] files=root.GetFiles(); 3、获得该文件夹下的子目录,返回类型为DirectoryInfo...string path=@"X:\XXX\XX"; DirectoryInfo root = new DirectoryInfo(path); DirctoryInfo[] dics=root.GetDirectories...(); 4、获得文件夹名 string path=@"X:\XXX\XX"; DirectoryInfo root = new DirectoryInfo(path); string dicName...=root.Name; 5、获得文件夹完整的路径名 string path=@"X:\XXX\XX"; DirectoryInfo root = new DirectoryInfo(path)...root = new DirectoryInfo(path); 59 foreach (DirectoryInfo d in root.GetDirectories()) 60
本文主要讨论FileInfo,DirectoryInfo和FileSystemInfo三个类。 FileInfo和DirectoryInfo都继承于FileSystemInfo类。...DirectoryInfo类的构造函数: //在指定路径上初始化DirectoryInfo新实例 public DirectoryInfo(string path); 它的属性: override bool...DirectoryInfo[] GetDirectories(); DirectoryInfo[] GetDirectories(string searchPattern); DirectoryInfo...进行动手练习: DirectoryInfo directoryInfo = new DirectoryInfo(@"C:\Temp"); bool exists = directoryInfo.Exists...C:\\ DirectoryInfo parent = directoryInfo.Parent; // C:\\ DirectoryInfo root = directoryInfo.Root; //
param> /// 是否包含隐藏目录 public static void GetEndDirectories(DirectoryInfo...dir,List dirList,bool system=false,bool hidden=false) { try...{ //返回当前目录的子目录集合 DirectoryInfo[] dirSub = dir.GetDirectories();...; return; } DirectoryInfo dir = new DirectoryInfo(dirPath);...List lastDirNameList = new List(); Tools.FileToolsHelper.GetEndDirectories
1.2 Directory和DirectoryInfo 1.2.1 Directory d....view=netcore-3.1 1.2.2 DirectoryInfo 之前的篇幅我们介绍了Directory的工具类所支持的方法,接下来我们看一下 DirectoryInfo有哪些属性和方法吧。...public DirectoryInfo (string path); 初始化的方式很简单,直接传递一个目录的路径字符串,就可以获取一个目录信息类了。...System.IO.DirectoryInfo Root { get; }//获取目录的根目录 我们路过了DirectoryInfo的属性,看到了它一部分特点,那么我们该怎么使用呢?...public System.IO.DirectoryInfo[] GetDirectories (); public System.IO.DirectoryInfo[] GetDirectories (
dir = new DirectoryInfo(path); FileInfo[] fil = dir.GetFiles(); DirectoryInfo...directoryInfo = new DirectoryInfo(directorySource); FileInfo[] files = directoryInfo.GetFiles...[] directoryInfoArray = directoryInfo.GetDirectories(); foreach (DirectoryInfo dir in directoryInfoArray...directoryInfo = new DirectoryInfo(directorySource); FileInfo[] files = directoryInfo.GetFiles...[] directoryInfoArray = directoryInfo.GetDirectories(); foreach (DirectoryInfo dir in directoryInfoArray
directoryInfo = new(zipPath); if (directoryInfo.Parent !...= null) { directoryInfo = directoryInfo.Parent; } if (!...directoryInfo.Exists) { directoryInfo.Create(); } ZipFile.CreateFromDirectory(folderPath...directoryInfo = new(folderPath); if (!...directoryInfo.Exists) { directoryInfo.Create(); } ZipFile.ExtractToDirectory(zipPath
g.获取上级目录 public static System.IO.DirectoryInfo GetParent (string path); 获取传入目录的上级目录信息。...view=netcore-3.1 1.2.2 DirectoryInfo 之前的篇幅我们介绍了Directory的工具类所支持的方法,接下来我们看一下 DirectoryInfo有哪些属性和方法吧。...public DirectoryInfo (string path); 初始化的方式很简单,直接传递一个目录的路径字符串,就可以获取一个目录信息类了。...System.IO.DirectoryInfo Root { get; }//获取目录的根目录 我们路过了DirectoryInfo的属性,看到了它一部分特点,那么我们该怎么使用呢?...public System.IO.DirectoryInfo[] GetDirectories (); public System.IO.DirectoryInfo[] GetDirectories (
递归遍历如下:将已知路径和列表数组作为参数传递, public void Director(string dir,List list) { DirectoryInfo d...= new DirectoryInfo(dir); FileInfo[] files = d.GetFiles();//文件 DirectoryInfo[] directs =...{ list.Add(f.Name);//添加文件名到列表中 } //获取子文件夹内的文件列表,递归遍历 foreach (DirectoryInfo
direc in new DirectoryInfo(FilePath).GetDirectories()) { TreeNode...direc, TreeNode tn,ImageList imglist) { foreach (DirectoryInfo dir in new DirectoryInfo...= dir.Name; tn.Nodes.Add(temptn); foreach (FileInfo fileinfo in new DirectoryInfo...direc in new DirectoryInfo(FilePath).GetDirectories()) { TreeNode...direc, TreeNode tn) { foreach (DirectoryInfo dir in new DirectoryInfo(direc.FullName
用LINQ遍历一个指定目录并输出为XML 写出的代码如下: public static XElement ToXml(this DirectoryInfo dir) { XElement root...dir = new DirectoryInfo(path); XElement dirXml = dir.ToXml(); OK,搞定。...比如说我要找出目录里的所有的mp3 于是可以写出这样的代码 public static XElement ToXml(this DirectoryInfo dir, string searchPattern...dir = new DirectoryInfo(path); XElement dirXml = dir.DirToXml("*.mp3"); 轻松搞定 更进一步,如果我要找多个格式的文件呢?...比如说要找出目录里面的图片文件(jpg, bmp, gif…) Ok,没问题,可以写成这样: public static XElement ToXml(this DirectoryInfo dir,
{ textBox1.Text = folderBrowserDialog1.SelectedPath; //实例化DirectoryInfo...对象 DirectoryInfo dinfo = new DirectoryInfo(textBox1.Text); //获取指定目录下的全部子目录及文件类型...foreach (FileSystemInfo fsinfo in fsinfos) { if (fsinfo is DirectoryInfo...) //推断是否目录 { //使用获取的目录名称实例化DirectoryInfo对象...DirectoryInfo dirinfo = new DirectoryInfo(fsinfo.FullName); //为ListView控件加入目录信息
param name="toDir">复制到的目录 /// public static bool CopyDir(DirectoryInfo...name="rootDir">被复制的根目录 /// private static bool CopyDir(DirectoryInfo... /// public static bool FindFile(DirectoryInfo...var listFolderName = new List(); try { var info = new DirectoryInfo...var listFileName = new List(); try { var info = new DirectoryInfo
Directory.Exists(LogPath))//检测文件夹是否存在 { } DirectoryInfo...Directory.Exists(LogPath)) { DirectoryInfo directoryInfo = Directory.CreateDirectory...Directory.Exists(rootPath)) return new List(); List directoryList = new List();//容器 DirectoryInfo directory = new DirectoryInfo...> GetChild(List directoryList, DirectoryInfo directoryCurrent) { var
1.实现把指定文件夹下的所有文件复制到指定路径下 1 public static void copyFiles(string path) { 2 DirectoryInfo dir = new...DirectoryInfo(path); 3 if (!...dir.Exists) 4 return; //获得指定文件夹内的所有的子目录信息 5 DirectoryInfo[] dirs = dir.GetDirectories...desPath = "~/DownLoad-TargetFiles/" + fileName; file.CopyTo(desPath); 10 } 11 foreach (DirectoryInfo
Directory.CreateDirectory(@"D:\wps\WPS Office\123"); 4.删除目录 //要求目录必须为空 Directory.Delete(@"D:\wps\WPS Office\123"); //使用DirectoryInfo...对象,可以删除不为空的目录 DirectoryInfo dir = new DirectoryInfo(@"D:\wps\WPS Office\123"); dir.Delete(true);//可以删除不为空的目录
领取专属 10元无门槛券
手把手带您无忧上云