马上加入IBC程序猿 各种源码随意下,各种教程随便看! 注册 每日签到 加入编程讨论群

C#教程 ASP.NET教程 C#视频教程程序源码享受不尽 C#技术求助 ASP.NET技术求助

【源码下载】 社群合作 申请版主 程序开发 【远程协助】 每天乐一乐 每日签到 【承接外包项目】 面试-葵花宝典下载

官方一群:

官方二群:

C#操作文件及文件夹的几种方法

[复制链接]
查看3659 | 回复2 | 2016-6-15 09:18:22 | 显示全部楼层 |阅读模式
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#论坛 www.ibcibc.com IBC编程社区
C#
C#论坛
IBC编程社区
ibcadmin | 2016-6-15 09:21:25 | 显示全部楼层
1
C#论坛 www.ibcibc.com IBC编程社区
C#
C#论坛
IBC编程社区
言わぬが花 | 2016-7-4 15:01:54 | 显示全部楼层
3637-2
*滑块验证:
您需要登录后才可以回帖 登录 | 立即注册

本版积分规则