C#文件處理

 头文件:using System.IO;
文件copy:
private void btnCopy_Click(object sender, System.EventArgs e)
{
 string OrignFile= Server.MapPath(".")+"//CreateText.txt";
 string NewFile = Server.MapPath(".")+"//NewCreateText.txt";
 //拷贝文件
 try
 {
 File.Copy(OrignFile,NewFile);//文件copy使用
 if(File.Exists(OrignFile))//判断文件是否存在
 {
 lblEFromFile.Text = OrignFile + "存在<br>";
        }
 else
 {
 lblEFromFile.Text = OrignFile + "不存在<br>";
 }

        if(File.Exists(NewFile))
 {
 FileInfo fi = new FileInfo(NewFile);
 DateTime Ctime = fi.CreationTime;
 lblEToFile.Text = NewFile + "已经存在<br>创建时间:" + Ctime.ToString() + "<br>";
 }
 else
 {
   lblEToFile.Text = NewFile + "不存在<br>";
 }
 }
 catch(Exception ee)//出错处理
 {
    lblError.Text = "不能拷贝文件,错误信息为:"+ee.Message;
 }
}
创建文件并写入文件:
using System.IO;
using System.Text;
function writefile()
{
       //建立StreamWriter为写做准备
       StreamWriter rw = File.CreateText(Server.MapPath(".")+"//CreateText.txt");
       //使用WriteLine写入内容
       rw.WriteLine("使用File.CreateText 方法");
       rw.WriteLine("返回StreamWriter流,利用这个流进行写入。");
       //将缓冲区的内容写入文件
       rw.Flush();
       //关闭rw对象
       rw.Close();
 
       //打开文本文件
       StreamReader sr = File.OpenText(Server.MapPath(".")+"//CreateText.txt");
        StringBuilder output = new StringBuilder();
 string rl;
 while((rl=sr.ReadLine())!=null)
 {
      output.Append(rl+"<br>");
 }
       lblFile.Text = output.ToString();
       sr.Close();
}
----文件删除
function deletefile()
{
//首先判断文件是否存在   
string delFile = Server.MapPath(".")+"//NewCreateText.txt";
if(File.Exists(delFile))
{
//建立FileInfo对象,取得指定的文件信息
FileInfo fi = new FileInfo(delFile);
DateTime CreateTime = fi.CreationTime;

Label lblOne = new Label();
lblOne.Text = delFile + "存在<br>创建时间为:" + CreateTime.ToString() + "<p>";
plShow.Controls.Add(lblOne);

  try
     {
 //删除文件
 File.Delete(delFile);
 Label lblOk = new Label();
 lblOk.Text = "删除文件"+delFile+"成功";
 plShow.Controls.Add(lblOk);

     }
  catch(Exception ee)
   {
        //捕捉异常
 Label lblFileExists = new Label();
 lblFileExists.Text = "不能删除文件"+delFile+"<br>";
 plShow.Controls.Add(lblFileExists);
    }
  }
else
   {
 Label lblError = new Label();
 lblError.Text = delFile + "根本就不存在";
 plShow.Controls.Add(lblError);
    }

}

----文件移动

File.Move(OrignFile,NewFile);

---文件读取
 function read()
{
  string strFileName = FileSelect.PostedFile.FileName;
  if(Path.GetFileName(strFileName)=="")
  return;
  StreamReader sr = File.OpenText(strFileName);
  StringBuilder output = new StringBuilder();
  string rl;
  while((rl=sr.ReadLine())!=null)
  {
    output.Append(rl+"<br>");
  }
    lblFile.Text = output.ToString();
    sr.Close();
}

----查找文件目录

private void btnFind_Click(object sender, System.EventArgs e)
{
 try
 {
     if(tbInput.Text.Trim()=="")
      {
  lbPath.Text = "文件名为空!";
  return;
       }
 string[] drives = System.IO.Directory.GetLogicalDrives();

 foreach (string str in drives)
 {
     
        if(ProcessDirectory(str))
        break;
 }
    if(!bExist)
     lbPath.Text = "不存在此文件!";
 }
 catch (System.IO.IOException)
 {
  Response.Write("I/O错误!");
 }
 catch (System.Security.SecurityException)
 {
  Response.Write("没有访问权限!");
 }

}
public bool  ProcessDirectory(string targetDirectory)
 {
 try
 {
  // Process the list of files found in the directory
  string [] fileEntries = Directory.GetFiles(targetDirectory);//盘符
  foreach(string fileName in fileEntries)
  {
   if(ProcessFile(fileName))
    return true;
     
  }

  // Recurse into subdirectories of this directory
  string [] subdirectoryEntries = Directory.GetDirectories(targetDirectory);//盘符下的文件
  foreach(string subdirectory in subdirectoryEntries)
  {
   if(ProcessDirectory(subdirectory))//递归实现遍历
   return true;
  }
  return false;
  }
 catch(Exception)
 {
  return false;
 }
 }
  
public bool ProcessFile(string strFileName)
 {
 if(Path.GetFileName(strFileName).ToLower()==tbInput.Text.Trim().ToLower())
 {
         lbPath.Text = strFileName.ToLower();
  bExist=true;
  return true;
 }
 else
 return false;
 }


-----取得路径文件的文件名:
string [] up =this.File1.PostedFile.FileName.Split(new char []{'//'});
this.File1.PostedFile.SaveAs(MapPath("")+"//"+this.TextBox1.Text+"//"+up[up.Length-1]); 
文件名称:"+this.File1.PostedFile.FileName+"<br>";
文件大小:"+this.File1.PostedFile.ContentLength.ToString()+"<br>";
文件类型:"+this.File1.PostedFile.ContentType; 

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值