日期:2014-05-17  浏览次数:21185 次

ASP.NET对文件的操作
操作文件必须引入using System.IO;命名空间

 

      //获取一个文件的大小
        System.IO.FileInfo file = new System.IO.FileInfo("C:\\t.txt");
        int fileSize = (int)file.Length / 1024;//把文件大小换算成KB
        Response.Write("<script>alert(" + fileSize.ToString() + ");</script>");

-------------------------------------

-------创建文件夹using System.IO;
private void button2_Click(object sender, EventArgs e)
    {
        if (Directory.Exists("\\picture"))//判断一个文件夹是否存在
        {
            MessageBox.Show("directory exists");
        }
        else {

    //创建一个文件夹
            Directory.CreateDirectory("\\picture");
        }
    }

Directory.Delete(path,true);//删除一个文件夹,其中path是文件夹的绝对路径

---------------------打开一个文件

Process.Start("C:\\t.txt");//打开一个文件

 

 

public void Page_Load(Object src,EventArgs e)  
{  
    StreamWriter rw = File.CreateText(Server.MapPath(".")+"[url=file://mytext.txt/]\\myText.txt[/url]");  
    rw.WriteLine("追逐理想");  
    rw.WriteLine("kzlll");  
    rw.WriteLine(".NET笔记");  
    rw.Flush();  
    rw.Close();  
}
打开文本文件
StreamReader sr = File.OpenText(Server.MapPath(".")+"[url=file://mytext.txt/]\\myText.txt[/url]");  
StringBuilder output = new StringBuilder();  
string rl;  
while((rl=sr.ReadLine())!=null)  
{  
output.Append(rl+"<br>");  
}  
lblFile.Text = output.ToString();  
sr.Close();  
  

C#追加文件
     StreamWriter sw = File.AppendText(Server.MapPath(".")+"[url=file://mytext.txt/]\\myText.txt[/url]");  
     sw.WriteLine("追逐理想");  
     sw.WriteLine("kzlll");  
     sw.WriteLine(".NET笔记");  
     sw.Flush();  
     sw.Close();  
C#拷贝文件
         string OrignFile,NewFile;  
         OrignFile = Server.MapPath(".")+"[url=file://mytext.txt/]\\myText.txt[/url]";  
         NewFile = Server.MapPath(".")+"[url=file://mytextcopy.txt/]\\myTextCopy.txt[/url]";  
         File.Copy(OrignFile,NewFile,true);  
C#删除文件
         string delFile = Server.MapPath(".")+"[url=file://mytextcopy.txt/]\\myTextCopy.txt[/url]";  
         File.Delete(delFile);  
C#移动文件
       string OrignFile,NewFile;  
       OrignFile = Server.MapPath