c# File 操作

來源:互聯網
上載者:User

標籤:des   style   http   color   os   io   使用   ar   for   

//1.---------檔案夾建立、移動、刪除---------

//建立檔案夾
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 inDirectory.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 inDirectory.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 = newStreamWriter(Server.MapPath("a//a.txt"));
    streamwrite.WriteLine("木子屋");
    streamwrite.WriteLine("http://www.mzwu.com/");
    streamwrite.Write("2008-04-13");
    streamwrite.Close();

    StreamReader streamreader = newStreamReader(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(stringfilePath)

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

  按行讀取檔案:

int fileCount=0;
// Open the file just specified such that no one else can useit.
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# File 操作

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.