ASP.NET 文件操作 代码-----文件移动 拷贝 创建 删除 读取
using System;
using System.Collections;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Web;
using System.Web.SessionState;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.HtmlControls;
using System.IO;
namespace AspFileOp
{
/// <summary>
/// FileMove 的摘要说明。
/// </summary>
public class FileMove : System.Web.UI.Page
{
protected System.Web.UI.WebControls.Label lblBFromFile;
protected System.Web.UI.WebControls.Label lblBToFile;
protected System.Web.UI.WebControls.Label lblEFromFile;
protected System.Web.UI.WebControls.Label lblEToFile;
protected System.Web.UI.WebControls.Button btnMove;
protected System.Web.UI.WebControls.Label lblError;
private void Page_Load(object sender, System.EventArgs e)
{
string OrignFile,NewFile;
OrignFile = Server.MapPath(".")+"\\CreateText.txt";
NewFile = Server.MapPath(".")+"\\..\\NewCreateText.txt";
//首先判断源文件和新文件是否都存在
if(File.Exists(OrignFile))
{
lblBFromFile.Text = OrignFile + "存在<br>";
}
else
{
lblBFromFile.Text = OrignFile + "不存在<br>";
}
if(File.Exists(NewFile))
{
lblBToFile.Text = NewFile + "存在<br>";
}
else
{
lblBToFile.Text = NewFile + "不存在<br>";
}
}
#region Web Form Designer generated code
override protected void OnInit(EventArgs e)
{
//
// CODEGEN:该调用是 ASP.NET Web 窗体设计器所必需的。
//
InitializeComponent();
base.OnInit(e);
}
/// <summary>
/// 设计器支持所需的方法 - 不要使用代码编辑器修改
/// 此方法的内容。
/// </summary>
private void InitializeComponent()
{
this.btnMove.Click += new System.EventHandler(this.btnMove_Click);
this.Load += new System.EventHandler(this.Page_Load);
}
#endregion
private void btnMove_Click(object sender, System.EventArgs e)
{
string OrignFile,NewFile;
OrignFile = Server.MapPath(".")+"\\CreateText.txt";
NewFile = Server.MapPath(".")+"\\..\\NewCreateText.txt";
//移动文件
try
{
File.Move(OrignFile,NewFile);
////拷贝文件 File.Copy(OrignFile,NewFile);
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 = "不能移动文件";
}
}
}
}
------------
创建文件CODE
//建立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();
----删除文件CODE
using System;
using System.Collections;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Web;
using System.Web.SessionState;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.HtmlControls;
using System.IO;
namespace AspFileOp
{
/// <summary>
/// FileDelete 的摘要说明。
/// </summary>
public class FileDelete : System.Web.UI.Page
{
protected System.Web.UI.WebControls.Button btnDelete;
protected System.Web.UI.WebControls.Panel plShow;
private void Page_Load(object sender, System.EventArgs e)
{
}
#region Web Form Designer generated code
override protected void OnInit(EventArgs e)
{
//
// CODEGEN:该调用是 ASP.NET Web 窗体设计器所必需的。
//
InitializeComponent();
base.OnInit(e);
}
/// <summary>
/// 设计器支持所需的方法 - 不要使用代码编辑器修改
/// 此方法的内容。
/// </summary>
private void InitializeComponent()
{
this.btnDelete.Click += new System.EventHandler(this.btnDelete_Click);
this.Load += new System.EventHandler(this.Page_Load);
}
#endregion
private void btnDelete_Click(object sender, System.EventArgs e)
{
//首先判断文件是否存在
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);
}
}
}
}
-----
读取文件CODE
using System;
using System.Collections;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Web;
using System.Web.SessionState;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.HtmlControls;
using System.IO;
using System.Text;
namespace AspFileOp
{
/// <summary>
/// FileOpenTextReadText 的摘要说明。
/// </summary>
public class FileOpenTextReadText : System.Web.UI.Page
{
protected System.Web.UI.WebControls.Button btnRead;
protected System.Web.UI.WebControls.Label Label2;
protected System.Web.UI.HtmlControls.HtmlInputFile FileSelect;
protected System.Web.UI.WebControls.Label lblFile;
private void Page_Load(object sender, System.EventArgs e)
{
}
#region Web Form Designer generated code
override protected void OnInit(EventArgs e)
{
//
// CODEGEN:该调用是 ASP.NET Web 窗体设计器所必需的。
//
InitializeComponent();
base.OnInit(e);
}
/// <summary>
/// 设计器支持所需的方法 - 不要使用代码编辑器修改
/// 此方法的内容。
/// </summary>
private void InitializeComponent()
{
this.btnRead.Click += new System.EventHandler(this.btnRead_Click);
this.Load += new System.EventHandler(this.Page_Load);
}
#endregion
private void btnRead_Click(object sender, System.EventArgs e)
{
//打开文本文件
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();
}
}
}