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); 9) 写文件 StreamWriter swr = File.CreateText(Server.MapPath("test.txt")); 10)删除文件 // 删除硬盘上的文件 11)目录遍历 public void ListFiles(string pathname) 12)文件修改时间 FileInfo fi = new FileInfo(@"c:test.txt"); 13)从含路径的文件名中提取文件名 System.IO.Path.GetFileName(fullPath);//文件名 (编辑:李大同) 【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容! |