头文件: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;