C#:文件創建、復制、移動、刪除



//創建文件夾
Directory.CreateDirectory(Server.MapPath("a"));
Directory.CreateDirectory(Server.MapPath("b"));
Directory.CreateDirectory(Server.MapPath("c"));
//移動b到a
Directory.Move(Server.MapPath("b"), Server.MapPath("a\\b"));
//刪除c
Directory.Delete(Server.MapPath("c"));

//2.---------文件創建、復制、移動、刪除---------

//創建文件
//使用File.Create創建再復制/移動/刪除時會提示:文件正由另一進程使用,因此該進程無法訪問該文件
//改用 FileStream 獲取 File.Create 返回的 System.IO.FileStream 再進行關閉就無此問題
FileStream fs;
fs = File.Create(Server.MapPath("a.txt"));
fs.Close();
fs = File.Create(Server.MapPath("b.txt"));
fs.Close();
fs = File.Create(Server.MapPath("c.txt"));
fs.Close();
//復制文件
File.Copy(Server.MapPath("a.txt"), Server.MapPath("a\\a.txt"));
//移動文件
File.Move(Server.MapPath("b.txt"), Server.MapPath("a\\b.txt"));
File.Move(Server.MapPath("c.txt"), Server.MapPath("a\\c.txt"));
//刪除文件
File.Delete(Server.MapPath("a.txt"));

//3.---------遍歷文件夾中的文件和子文件夾並顯示其屬性---------

if(Directory.Exists(Server.MapPath("a")))
{
     //所有子文件夾
     foreach(string item in Directory.GetDirectories(Server.MapPath("a")))
     {
         Response.Write("<b>文件夾:" + item + "</b><br/>");
         DirectoryInfo directoryinfo = new DirectoryInfo(item);
         Response.Write("名稱:" + directoryinfo.Name + "<br/>");
         Response.Write("路徑:" + directoryinfo.FullName + "<br/>");
         Response.Write("創建時間:" + directoryinfo.CreationTime + "<br/>");
         Response.Write("上次訪問時間:" + directoryinfo.LastAccessTime + "<br/>");
         Response.Write("上次修改時間:" + directoryinfo.LastWriteTime + "<br/>");
         Response.Write("父文件夾:" + directoryinfo.Parent + "<br/>");
         Response.Write("所在根目錄:" + directoryinfo.Root + "<br/>");
         Response.Write("<br/>");
     }

     //所有子文件
     foreach (string item in Directory.GetFiles(Server.MapPath("a")))
     {
         Response.Write("<b>文件:" + item + "</b><br/>");
         FileInfo fileinfo = new FileInfo(item);
         Response.Write("名稱:" + fileinfo.Name + "<br/>");
         Response.Write("擴展名:" + fileinfo.Extension +"<br/>");
         Response.Write("路徑:" + fileinfo.FullName +"<br/>");
         Response.Write("大小:" + fileinfo.Length +"<br/>");
         Response.Write("創建時間:" + fileinfo.CreationTime +"<br/>");
         Response.Write("上次訪問時間:" + fileinfo.LastAccessTime +"<br/>");
         Response.Write("上次修改時間:" + fileinfo.LastWriteTime +"<br/>");
         Response.Write("所在文件夾:" + fileinfo.DirectoryName +"<br/>");
         Response.Write("文件屬性:" + fileinfo.Attributes +"<br/>");
         Response.Write("<br/>");
     }
}

//4.---------文件讀寫---------

if (File.Exists(Server.MapPath("a\\a.txt")))
{
     StreamWriter streamwrite = new StreamWriter(Server.MapPath("a\\a.txt"));
     streamwrite.WriteLine("木子屋");
     streamwrite.WriteLine("http://www.baidu.com/");
     streamwrite.Write("2008-04-13");
     streamwrite.Close();

     StreamReader streamreader = new StreamReader(Server.MapPath("a\\a.txt"));
     Response.Write(streamreader.ReadLine());
     Response.Write(streamreader.ReadToEnd());
     streamreader.Close();
}

獲取文件的版本信息:
FileVersionInfo myFileVersionInfo1 = FileVersionInfo.GetVersionInfo("D:\\TEST.DLL");
textBox1.Text="版本號: " + myFileVersionInfo1.FileVersion;

更改文件屬性,刪除只讀文件:

  下例欲將E:\test.txt文件拷貝至D:\tmp\test.txt,但D:\tmp\test.txt已經存在。

//File.Copy(sourceFile,destinationFile,true); 用來拷貝文件
//當destinationFile已經存在時,無法將文件file1拷貝到目標文件,
//因此先刪除destination文件,File.Delete()方法不能刪除只讀文件,
//因此,如果文件屬性為只讀(Attributes屬性中會包含有"ReadOnly"),
//先把文件屬性重置為Normal,然后再刪除:
string file1="E:\\test.txt";
string destinationFile="d:\\tmp\\test.txt";
if(File.Exists(destinationFile))
{
 FileInfo fi=new FileInfo(destinationFile);
 if(fi.Attributes.ToString().IndexOf("ReadOnly")!=-1)
  fi.Attributes=FileAttributes.Normal;
  File.Delete(destinationFile);
}
File.Copy(file1,destinationFile,true);

判斷文件是否存在:File.Exists(string filePath)

  判斷目錄是否存在:Directory.Exists("D:\\LastestVersion")

  按行讀取文件:

int fileCount=0;
// Open the file just specified such that no one else can use it.
StreamReader sr = new StreamReader(textBox1.Text.Trim());
while(sr.Peek() > -1)//StreamReader.Peek()返回下一個可用字符,但不使用它
{
 listBox1.Items.Add(sr.ReadLine());
 fileCount++;
}
sr.Close();

按行寫入文件:
StreamWriter sw = new StreamWriter("D:\\result.txt");
for(int i=0;i<10;i++)
{
 sw.WriteLine("這是第"+i.ToString()+"行數據");
}

C#追加文件
StreamWriter sw = File.AppendText(Server.MapPath(".")+"\\myText.txt");
sw.WriteLine("追逐理想");
sw.WriteLine("kzlll");
sw.WriteLine(".NET筆記");
sw.Flush();
sw.Close();

C#拷貝文件
string OrignFile,NewFile;
OrignFile = Server.MapPath(".")+"\\myText.txt";
NewFile = Server.MapPath(".")+"\\myTextCopy.txt";
File.Copy(OrignFile,NewFile,true);

C#刪除文件
string delFile = Server.MapPath(".")+"\\myTextCopy.txt";
File.Delete(delFile);

C#移動文件
string OrignFile,NewFile;
OrignFile = Server.MapPath(".")+"\\myText.txt";
NewFile = Server.MapPath(".")+"\\myTextCopy.txt";
File.Move(OrignFile,NewFile);

C#創建目錄
// 創建目錄c:\sixAge
DirectoryInfo d=Directory.CreateDirectory("c:\\sixAge");
// d1指向c:\sixAge\sixAge1
DirectoryInfo d1=d.CreateSubdirectory("sixAge1");
// d2指向c:\sixAge\sixAge1\sixAge1_1
DirectoryInfo d2=d1.CreateSubdirectory("sixAge1_1");
// 將當前目錄設為c:\sixAge
Directory.SetCurrentDirectory("c:\\sixAge");
// 創建目錄c:\sixAge\sixAge2
Directory.CreateDirectory("sixAge2");
// 創建目錄c:\sixAge\sixAge2\sixAge2_1
Directory.CreateDirectory("sixAge2\\sixAge2_1");

遞歸刪除文件夾及文件
<%@ Page Language=C#%>
<%@ Import namespace="System.IO"%>
<Script runat=server>
public void DeleteFolder(string dir)
{
    if (Directory.Exists(dir)) //如果存在這個文件夾刪除之
    {
        foreach(string d in Directory.GetFileSystemEntries(dir))
        {
            if(File.Exists(d))
                File.Delete(d); //直接刪除其中的文件
            else
                DeleteFolder(d); //遞歸刪除子文件夾
        }
        Directory.Delete(dir); //刪除已空文件夾
        Response.Write(dir+" 文件夾刪除成功");
    }
    else
        Response.Write(dir+" 該文件夾不存在"); //如果文件夾不存在則提示
}

protected void Page_Load (Object sender ,EventArgs e)
{
    string Dir="D:\\gbook\\11";
    DeleteFolder(Dir); //調用函數刪除文件夾
}

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM