欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

C#文件目錄操作方法匯總

 更新時間:2016年04月27日 10:19:35   作者:jerrylsxu  
本文主要列舉出C#文件和目錄操作的一些方法,包括創(chuàng)建、移動、遍歷目錄,讀寫文件等方法,有需要的小伙伴可以學習一下。

需要 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);//文件名

相關文章

最新評論