C#文件目錄操作方法匯總
需要 using System.IO;
1) 相對路徑轉(zhuǎn)絕對路徑
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) 創(chuàng)建目錄
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# ListView 點擊表頭對數(shù)據(jù)進行排序功能的實現(xiàn)代碼
這篇文章主要介紹了C# ListView 點擊表頭對數(shù)據(jù)進行排序功能的實現(xiàn)代碼,需要的朋友可以參考下2017-04-04C#實現(xiàn)將商品金額小寫轉(zhuǎn)換成大寫的方法
這篇文章主要介紹了C#實現(xiàn)將商品金額小寫轉(zhuǎn)換成大寫的方法,涉及C#數(shù)組與字符串的相關操作技巧,具有一定參考借鑒價值,需要的朋友可以參考下2016-08-08