星空网 > 软件开发 > ASP.net

C#文件目录操作完全手册

需要 using System.IO;

 

1) 相对路径转绝对路径
string fullfolder = HttpContext.Current.Server.MapPath(folder);

 

2) 文件移动(改名)
File.Move(Server.MapPath("/a.txt"), Server.MapPath("/b.txt"));

 

3) 文件复制
File.Copy(Server.MapPath("/a.txt"), Server.MapPath("/b.txt"), true);

 

4) 文件是否存在
File.Exists(filefullname)

 

5) 目录是否存在
Directory.Exists(fullfolder))

 

6) 创建目录
Directory.CreateDirectory(fullfolder);

 

7) 目录移动
Directory.Move

 

8) 读取文本文件
StreamReader srd = File.OpenText(fullfilename);
srd.ReadToEnd();
srd.Close();
srd.Dispose();

 

9) 写文件
StreamWriter swr = File.CreateText(Server.MapPath("test.txt"));
swr.Write("message");
swr.Close();
swr.Dispose();

 

10)删除文件
// 删除硬盘上的文件
if (File.Exists(filefullname))
{
File.Delete(filefullname);
}

 

11)目录遍历
public void ListFiles(string pathname)
{
// 所有目录与文件
string[] subDirs = Directory.GetDirectories(pathname);
string[] subFiles = Directory.GetFiles(pathname);

foreach (string subDir in subDirs)
{
ListFiles(subDir);
}

// 所有文件
foreach (string subFile in subFiles)
{
string filename = Path.GetFileName(subFile);
}
}

 

12)文件修改时间
FileInfo fi = new FileInfo(@"c:\test.txt");
DateTime writetime = fi.LastWriteTime;

 

13)从含路径的文件名中提取文件名
System.IO.Path.GetFileName(fullPath);//文件名




原标题:C#文件目录操作完全手册

关键词:C#

C#
*特别声明:以上内容来自于网络收集,著作权属原作者所有,如有侵权,请联系我们: admin#shaoqun.com (#换成@)。

英国标识:https://www.goluckyvip.com/tag/78377.html
英国标志建筑:https://www.goluckyvip.com/tag/78379.html
运费再下调:https://www.goluckyvip.com/tag/7838.html
英国标志性的建筑:https://www.goluckyvip.com/tag/78381.html
二战英国标志:https://www.goluckyvip.com/tag/78382.html
购买英国化妆品r标:https://www.goluckyvip.com/tag/78383.html
青岛崂山旅游攻略自驾游(详细介绍崂山自驾游路线和景点):https://www.vstour.cn/a/404242.html
海南旅游景点的路线 海南旅游景点大全攻略自驾游:https://www.vstour.cn/a/404243.html
相关文章
我的浏览记录
最新相关资讯
海外公司注册 | 跨境电商服务平台 | 深圳旅行社 | 东南亚物流