文件及文件夹操作:

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. 低版本IDE 打开 高版本 IDE 代码时 unit

    可以用单元别名 比如Vcl.Forms=Forms 来兼容.

  2. SpringBoot配置Swagger实例(POST接收json参数)

    工程目录结构: 首先,引入jar包,只需要以下两个即可 <dependency> <groupId>io.springfox</groupId> <artif ...

  3. 阿里云已买到域名价格统计js代码

    var sum = 0; $('.table-hover tr.ng-scope').each(function(){ sum = sum + parseInt($(this).children()[ ...

  4. input输入框失去焦点,软键盘关闭后,滚动的页面无法恢复到原来位置

    H5微信页面开发,软键盘弹起后,若原输入框被遮挡,页面整体将会上移,然而当输入框失焦,软键盘收起后,页面未恢复,导致弹框里的按钮响应区域错位. 解决方案:给输入框(或select选择框)添加失去焦点的 ...

  5. 【webpack学习笔记】a02-管理资源

    在webpack 中,各种资源要引入,要用到module配置,比如css/图片/字体等等. 例如: module.exports = { entry: './src/app.js', //这是入口文件 ...

  6. JS案例四:表格的隔行换色以及高亮显示

    <!DOCTYPE html> <html> <head> <meta charset="UTF-8"> <title> ...

  7. fastreport窗口重置(适用于属性、数据等窗口显示不出来)

    找到如下路径: C:/Users/账户名/AppData/Local/FastReport/FastReport.config 删除即可. 记得先退出使用FastReport的程序,再删除

  8. 进程pid理解

    PID(Process Identification)操作系统里指进程识别号,也就是进程标识符.操作系统里每打开一个程序都会创建一个进程ID,即PID.   PID(进程控制符)英文全称为Proces ...

  9. 运行python脚本时,报错InsecurePlatformWarning: A true SSLContext object is not available,解决方法

    今天,要在新环境里运行一个python脚本,遇到下面的报错: /usr/lib/python2.7/site-packages/urllib3/util/ssl_.py:160: InsecurePl ...

  10. 常见的操作系统及linux发展史

    目前我们常见的操作系统有: 1> 桌面操作系统 Windows 系列 用户群体大 macOS 适合于开发人员 Linux 应用软件少 2> 服务器操作系统 Linux 安全.稳定.免费 占 ...