文件及文件夹操作- File类、Directory 类、FileInfo 类、DirectoryInfo 类
文件及文件夹操作:
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 类的更多相关文章
- 64位Ubuntu运行32位程序时报文件不存在(No such file or Directory)的一种解决办法
尝试在64位Ubuntu下面运行32位程序时, 一直说 文件不存在(No such file or directory), 我只想说++. 你tm说个文件格式不正确不就好了? 非得说个文件不存在! 真 ...
- 无法打开包括文件:“windows.h”: No such file or directory
VS2012 出现如下错误: 无法打开包括文件:"windows.h": No such file or directory 解决办法,将 C:\Program Files ...
- 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): ...
- fatal error C1083: 无法打开包括文件: “SDKDDKVer.h”: No such file or directory(转)
fatal error C1083: 无法打开包括文件: “SDKDDKVer.h”: No such file or directory 解决办法:(Vs2013中) 项目--右键--属性--配置属 ...
- Linux执行.sh文件,提示No such file or directory的问题的解决方法
亲测有效:http://www.jb51.net/LINUXjishu/56395.html Linux执行.sh文件,提示No such file or directory的问题的解决方法 在win ...
- 【解决】 无法打开包括文件:“windows.h”: No such file or directory
vs编译时错误: 无法打开包括文件:“windows.h”: No such file or directory 出现这种错误什么都不用配置(环境变量),最好办法是将VS安装在C盘,让开发工具自动包含 ...
- 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 ...
- fatal error C1083: 无法打开包括文件:“qedit.h”: No such file or directory
VS2010编译 DirectShow一些项目时遇到 错误:fatal error C1083: 无法打开包括文件:“qedit.h”: No such file or directory 解决方法: ...
- 无法打开包括文件:“SDKDDKVer.h”: No such file or directory
在已经装有Visual Studio 2010的系统中,同时安装Visual Studio 2012,安装过程很顺利,但到使用VS2013时,却出问题了. 本文主要介绍:VS中新建工程编译时出现,“无 ...
- 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: 解决办法:这个问题很明显,是因为没有在工程属性里包 ...
随机推荐
- css--父元素塌陷
当父元素内都是漂浮元素时,会造成父高度塌陷的问题.(因为等同于父元素内容为空,所以长,宽都等于空) 我们想要的页面结构是: <!DOCTYPE html> <html lang=&q ...
- Mac中java实现自动打开软件问题
Runtime.getRuntime().exec("/Applications/NetEaseMusic.app/Contents/MacOS/NetEaseMusic");遗留 ...
- linux(ubuntu)GCC编译包含库函数的问题
GCC 编译命令通常为:gcc hello.c -o hello.out 注意:若hello.c中引用有库函数(比如math.h),直接编译会出错 "/tmp/ccalvMPY.o: In ...
- 利用phpredis实现PHP操作Redis
redis在PHP中的基本使用案例 利用phpredis实现PHP操作Redis,需要进行redis.so的下载,添加 //利用Redis类进行操作 //实例化Redis类 $redis = new ...
- 深入java----垃圾回收
Java和C++之间有一睹内存动态分配和垃圾收集技术所围成的“高墙”,墙外面的人想进去,墙里面的人想出来.-------<深入理解JVM虚拟机> 补充:在无用对象判断这两种方法中,都是靠对 ...
- Input标签_实现限制输入字符类型(只能输入特定类型字符)
... <input type="text" placeholder="密码" maxlength="20" onKeyPress=& ...
- CDN原理介绍(转)
内容分发网络(Content delivery network或Content distribution network,缩写:CDN)是指一种通过互联网互相连接的电脑网络系统,利用最靠近每位用户的服 ...
- 获取百度地图POI数据一(详解百度返回的POI数据)
POI是一切可以抽象为空间点的现实世界的实体,比如餐馆,酒店,车站,停车场等.POI数据具有空间坐标和各种属性,是各种地图查询软件的基础数据之一.百度地图作为国内顶尖的地图企业,其上具有丰富的POI数 ...
- sdl2在vs2012上的配置
网上关于sdl2的配置教程很多,我尽量将我遇到的问题分享给大家. 首先,打开VS2012: 2.点击新建项目:选择空项目,确定即可 (文件名,保存位置,解决方案名称,可以随便填,(我取名为sdlpla ...
- Java 实现ftp 文件上传、下载和删除
本文利用apache ftp工具实现文件的上传下载和删除.具体如下: 1.下载相应的jar包 commons-net-1.4.1.jar 2.实现代码如下: public class FtpUtils ...