文件及文件夹操作:

C/S:WinForm可以操作客户端文件 Client Server
B/S:Brower Server

命名空间:using system .IO;

1. File类:

创建:File.Create(路径);创建文件,返回FileStream

FileStream fs = File.Create(路径);之后需要关闭否则打不开,fs.close();

删除:File.Delete(路径);无返回值

复制文件:File.Copy(源文件,目标文件);

剪切文件:File.Move(源文件路径,目标路径);

判断文件是否存在:File.Exists(路径);返回布尔型,true代表已存在

文件加密:File.Encrypt();File.Decrypt();解密

File.GetCreationTime(路径);获取创建时间,返回DateTime类型 SetCreationTime(路径,DateTime类型);修改创建时间

File.GetLastAccessTime(路径);最后访问时间,返回DateTime类型 SetLastAccessTime(路径,DateTime类型);修改访问时间

File.GetLastWriteTime(路径);最后修改时间,返回DateTime类型 SetLastWriteTime(路径,DateTime类型);修改修改时间

2. Directory 类,目录(文件夹)

Directory .CreateDirectory(路径);创建目录

Directory .Delete(路径);删除目录

Directory .Exists(路径);目录是否存在

三个时间的get和set

Directory .GetDirectories(路径); 获取子目录,返回string数组

Directory .GetFiles(路径); 获取子文件!名!,返回string数组,string[] s = Directory .GetFiles(路径);

Directory .GetDirectoryRoot(路径); 获取根目录

Directory .GetParent(路径); 获取上一级目录

------------------------------------------------------------------------------------------------------

FileInfo 类

是实例方法,需要造对象new出来才能用,上面的都是File的静态方法

创建文件:FileInfo f = new FileInfo(路径); FileStream s = f.Create(); s.Close();

删除文件:FileInfo f = new FileInfo(路径); f.Delete();

复制文件:FileInfo f = new FileInfo(路径); f.CopyTo(目标路径,是否覆盖(true是覆盖));

移动文件:FileInfo f = new FileInfo(路径); f.MoveTo(目标路径);

文件是否存在:FileInfo f = new FileInfo(路径); bool b = f.Exists;布尔型,是个属性

获取文件名:FileInfo f = new FileInfo(路径);string s = f.FullName;属性,带路径的文件名

获得创建时间:DateTime d = f.CreationTime,三个时间都一样,都是属性

设置创建时间:f.CreationTime = DateTime.Now.AddDays(100); 三个都一样

获取文件大小:f.Length

DirectoryInfo 类

创建目录:DirectoryInfo d = new DirectoryInfo(路径); d.Create();

删除目录:d.Delete();

移动目录:d.MoveTo(目标路径);

目录是否存在:bool b = d.Exists;

获得目录全名:d.FullName;

获得子文件!对象信息!: FileInfo[] f = d.GetFiles(); 返回的是文件对象数组,内容更详细,d.GetFiles("*.exe")只获取exe的文件

获得子目录:DirectoryInfo[] dr = d.GetDirectories();

实例展示:读取目录大小

private long size = 0;
private long DirSize(string path)
{
DirectoryInfo d = new DirectoryInfo(path);
FileInfo[] f = d.GetFiles();
foreach (FileInfo wj in f)
{
size += wj.Length;
}

DirectoryInfo[] dr = d.GetDirectories();
if (dr.Count() > 0)
{
foreach (DirectoryInfo wjj in dr)
{
DirSize(wjj.FullName);
}
}

return size;
}

读取目录下面文件夹数量:

private int dcount = 0;
private int DirCount(string path)
{
DirectoryInfo d = new DirectoryInfo(path);

DirectoryInfo[] dr = d.GetDirectories();

if (dr.Count() > 0)
{
foreach (DirectoryInfo wjj in dr)
{
DirCount(wjj.FullName);
}
}

dcount += dr.Count();

return dcount;
}

读取所有文件数量:

private int count=0;
private int FileCount(string path)
{
DirectoryInfo d = new DirectoryInfo(path);
FileInfo[] f = d.GetFiles();

DirectoryInfo[] dr = d.GetDirectories();
if (dr.Count() > 0)
{
foreach (DirectoryInfo wjj in dr)
{
FileCount(wjj.FullName);
}
}

count += f.Count();

return count;
}

利用遍历集合查询文件夹下所有文件数量,文件夹数量:

private int fcount = 0;
private int FileCount(string path)
{
//造文件夹信息对象
DirectoryInfo dwjj = new DirectoryInfo(path);

//取当前文件夹下文件数量
fcount += dwjj.GetFiles().Length;

//取当前目录下所有文件夹
foreach (DirectoryInfo d in dwjj.GetDirectories())
{
FileCount(d.FullName);
}

return fcount;

}

private int dcount = 0;
private int DirCount(string path)
{
//造一个文件夹信息对象
DirectoryInfo d = new DirectoryInfo(path);

//取该目录下所有文件夹
DirectoryInfo[] df = d.GetDirectories();

//累加文件夹数量
dcount += df.Length;

//遍历所有文件夹
foreach (DirectoryInfo w in df)
{
DirCount(w.FullName);
}

return dcount;
}

文件及文件夹操作- File类、Directory 类、FileInfo 类、DirectoryInfo 类的更多相关文章

  1. 64位Ubuntu运行32位程序时报文件不存在(No such file or Directory)的一种解决办法

    尝试在64位Ubuntu下面运行32位程序时, 一直说 文件不存在(No such file or directory), 我只想说++. 你tm说个文件格式不正确不就好了? 非得说个文件不存在! 真 ...

  2. 无法打开包括文件:“windows.h”: No such file or directory

      VS2012 出现如下错误: 无法打开包括文件:"windows.h": No such file or directory   解决办法,将 C:\Program Files ...

  3. afx.h(78): fatal error C1083: 无法打开包括文件: “new.h”: No such file or directory

    vs2015新建mfc工程,编译错误: D:\program files (x86)\microsoft visual studio 14.0\vc\atlmfc\include\afx.h(78): ...

  4. fatal error C1083: 无法打开包括文件: “SDKDDKVer.h”: No such file or directory(转)

    fatal error C1083: 无法打开包括文件: “SDKDDKVer.h”: No such file or directory 解决办法:(Vs2013中) 项目--右键--属性--配置属 ...

  5. Linux执行.sh文件,提示No such file or directory的问题的解决方法

    亲测有效:http://www.jb51.net/LINUXjishu/56395.html Linux执行.sh文件,提示No such file or directory的问题的解决方法 在win ...

  6. 【解决】 无法打开包括文件:“windows.h”: No such file or directory

    vs编译时错误: 无法打开包括文件:“windows.h”: No such file or directory 出现这种错误什么都不用配置(环境变量),最好办法是将VS安装在C盘,让开发工具自动包含 ...

  7. VS2012与VS2015同时安装用VS2012创建MFC程序时弹出编译错误”fatal error C1083: 无法打开包括文件:“mprapidef.h”: No such file or directory”的解决办法

    在WIndows 7操作系统上同时安装VS2012与VS2015并用VS2012创建MFC程序时弹出编译错误”fatal error C1083: 无法打开包括文件:“mprapidef.h”: No ...

  8. fatal error C1083: 无法打开包括文件:“qedit.h”: No such file or directory

    VS2010编译 DirectShow一些项目时遇到 错误:fatal error C1083: 无法打开包括文件:“qedit.h”: No such file or directory 解决方法: ...

  9. 无法打开包括文件:“SDKDDKVer.h”: No such file or directory

    在已经装有Visual Studio 2010的系统中,同时安装Visual Studio 2012,安装过程很顺利,但到使用VS2013时,却出问题了. 本文主要介绍:VS中新建工程编译时出现,“无 ...

  10. ObjectARX2012错误1 fatal error C1083: 无法打开包括文件:“arxHeaders.h”: No such file or directory; fatal error C1083: 无法打开包括文件:“map”: No such file or directory

    问题1:fatal error C1083: 无法打开包括文件:“arxHeaders.h”: No such file or directory: 解决办法:这个问题很明显,是因为没有在工程属性里包 ...

随机推荐

  1. 开发一个简单的chrome插件-解析本地markdown文件

    准备软件环境 1. 软件环境 首先,需要使用到的软件和工具环境如下: 一个最新的chrome浏览器 编辑器vscode 2. 使用的js库 代码高亮库:prismjs https://prismjs. ...

  2. C#中枚举的使用

    一.什么是枚举类型 枚举类型(也称为枚举):该类型可以是除 char以外的任何整型(重点). 枚举元素的默认基础类型为 int.准许使用的枚举类型有 byte.sbyte.short.ushort.i ...

  3. CAT部署安装文档

    多数软件都在/root/project/codebase/3rdpart redhat7用firewalld取代了iptables,遇到问题请添加redhat7关键字搜索,详情请参见Common ad ...

  4. 关于STL的map的注意事项

    关于map是什么,这里就不多叙述了. 直接正题,常用的map插入操作有三种方法:通过pair<key_type,value_type>.通过value_type插入数据.还有一种类似于数组 ...

  5. 最短路径HDU3790(Dijkstra)

    准备考研,荒废了好多东西,希望做了正确的决定 /********************************************************* *author:chen xin * ...

  6. 魅族pro 7详细打开Usb调试模式的方法

    经常我们使用安卓手机链上Pc的时候,或者使用的有些APP比如我们公司营销小组经常使用的APP引号精灵,之前老版本就需要开启usb开发者调试模式下使用,现经常新版本不需要了,如果手机没有开启usb开发者 ...

  7. LeetCode 57 插入区间

    题目: 给出一个无重叠的 ,按照区间起始端点排序的区间列表. 在列表中插入一个新的区间,你需要确保列表中的区间仍然有序且不重叠(如果有必要的话,可以合并区间). 示例 1: 输入: intervals ...

  8. Redis 分布式锁及缓存注释的使用方法

    使用工具:Apache an 测压命令: ab -n 100 -c 100 http://www.baidu.com -n代表模拟100个请求,-c代表模拟100个并发,相当于100个人同时访问 ab ...

  9. (2018 Multi-University Training Contest 2)Problem G - Naive Operations

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=6315 题目大意:告诉你a,b两个数组,a数组初始化为0,b数组告诉你长度和具体值,接下来有q次操作,a ...

  10. Annotation(注解)介绍

    Annotation(注解)是什么: Annotation(注解) 官方的定义:    An annotation is a form of metadata, that can be added t ...