System.IO命名空间常用的操作主要有两部分:
★创建一个文本的语法:
public static StreamWriter File.CreateText(路径)
创建文本文件
using System.IO;
using System.Text;
public partial class _Default : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
//建立StreamWrite
StreamWriter rw = File.CreateText(Server.MapPath(".") + "\\myText.txt");
rw.WriteLine("我爱asp.net");//使用WriteLine写入内容;
rw.WriteLine("我爱北京天安门");
rw.Flush();//将缓冲区的内容写入文件;
rw.Close();//关闭rw对象;
//打开文本文件;
StreamReader sr = File.OpenText(Server.MapPath(".") + "\\myText.txt");
StringBuilder output = new StringBuilder();
string rl;
while ((rl = sr.ReadLine() )!= null)
{
output.Append(rl + "<br>");//StringBuilder.Append(*) 在实例的结尾追加;
}
lblFile.Text = output.ToString();
sr.Close();
}
}
using System.IO;
using System.Text;
public partial class _Default : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
//建立StreamWrite
StreamWriter rw = File.CreateText(Server.MapPath(".") + "\\myText.txt");
rw.WriteLine("我爱asp.net");//使用WriteLine写入内容;
rw.WriteLine("我爱北京天安门");
rw.Flush();//将缓冲区的内容写入文件;
rw.Close();//关闭rw对象;
//打开文本文件;
StreamReader sr = File.OpenText(Server.MapPath(".") + "\\myText.txt");
StringBuilder output = new StringBuilder();
string rl;
while ((rl = sr.ReadLine() )!= null)
{
output.Append(rl + "<br>");//StringBuilder.Append(*) 在实例的结尾追加;
}
lblFile.Text = output.ToString();
sr.Close();
}
}
程序首先在当前目录下创建一个文本文件,然后写入两行文字,接着创建一个StreamReader对象读取文件的内容,并将文件的内容显示到浏览器上:
★添加文本文件
函数"public static StreamWriter AppendText(string path)"为文件添加内容其功能是课教案用于添加文本的StreamWriter对象,使用方法如下:
注意下面的while而不是if
添加文字
public partial class Default2 : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
StreamWriter sw=File .AppendText (Server.MapPath (".")+"\\myText.txt");
sw.WriteLine ("我还是爱asp.net");
sw.WriteLine ("当然了,我很爱小宝贝哦!");
sw.Flush ();
sw.Close ();
//以下读取
StreamReader sr = File.OpenText(Server.MapPath(".") + "\\myText.txt");
StringBuilder output = new StringBuilder();
string rl;
while ((rl = sr.ReadLine()) != null)
{
output.Append(rl);
}
txtfile.Text = output.ToString();
sr.Close();
}
}
public partial class Default2 : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
StreamWriter sw=File .AppendText (Server.MapPath (".")+"\\myText.txt");
sw.WriteLine ("我还是爱asp.net");
sw.WriteLine ("当然了,我很爱小宝贝哦!");
sw.Flush ();
sw.Close ();
//以下读取
StreamReader sr = File.OpenText(Server.MapPath(".") + "\\myText.txt");
StringBuilder output = new StringBuilder();
string rl;
while ((rl = sr.ReadLine()) != null)
{
output.Append(rl);
}
txtfile.Text = output.ToString();
sr.Close();
}
}
程序创建一个StreamWriter对象,向文本文件添加文本
★拷贝文本文件
使用File对象的Copy方法,格式:"File.Copy(OrignFile,NewFile,true)",参数true是指如果目标文件存在就覆盖;
复制文本
using System.IO;
using System.Text;
public partial class Copy : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
string OrignFile, NewFile;
OrignFile = Server.MapPath(".") + "\\myText.txt";
NewFile =Server.MapPath (".")+"\\myCopy.txt";
//拷贝文件
try
{
File .Copy (OrignFile ,NewFile ,true );
if (File.Exists(NewFile))//如果myCopy.txt存在;
{
FileInfo fi = new FileInfo(NewFile);
DateTime ctime = fi.CreationTime;
lbl.Text = NewFile + "<br>创建的时间是:" + ctime.ToString() +"<br>";
}
else
{
lbl.Text = NewFile + "不存在<br>";
}
}
catch
{
lblError.Text = "不能拷贝!";
}
}
}
using System.IO;
using System.Text;
public partial class Copy : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
string OrignFile, NewFile;
OrignFile = Server.MapPath(".") + "\\myText.txt";
NewFile =Server.MapPath (".")+"\\myCopy.txt";
//拷贝文件
try
{
File .Copy (OrignFile ,NewFile ,true );
if (File.Exists(NewFile))//如果myCopy.txt存在;
{
FileInfo fi = new FileInfo(NewFile);
DateTime ctime = fi.CreationTime;
lbl.Text = NewFile + "<br>创建的时间是:" + ctime.ToString() +"<br>";
}
else
{
lbl.Text = NewFile + "不存在<br>";
}
}
catch
{
lblError.Text = "不能拷贝!";
}
}
}
如果源文件存在的话就可以拷贝了.并且在浏览器上显示出源文件的创建时间;
★删除文件
基本语法:File.Delete(文件路径),如果要删除的文件不存在的话程序会有异常;所以要先判断文件是否存在
delete
using System.IO;
using System.Text;
public partial class Delete : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
//首先判断文件是否存在;
string delFile = Server.MapPath(".") + "\\myCopy.txt";
try
{
//删除文件
File.Delete(delFile);
Label lblOK = new Label();
lblOK.Text = "删除成功!";
Panel1.Controls.Add(lblOK);
}
catch(Exception ee)//捕捉异常;
{
Label lblError = new Label();
lblError.Text = "不能删除!可能因为没有这个文件!";
Panel1.Controls.Add(lblError );
}
}
}
using System.IO;
using System.Text;
public partial class Delete : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
//首先判断文件是否存在;
string delFile = Server.MapPath(".") + "\\myCopy.txt";
try
{
//删除文件
File.Delete(delFile);
Label lblOK = new Label();
lblOK.Text = "删除成功!";
Panel1.Controls.Add(lblOK);
}
catch(Exception ee)//捕捉异常;
{
Label lblError = new Label();
lblError.Text = "不能删除!可能因为没有这个文件!";
Panel1.Controls.Add(lblError );
}
}
}
★移动文件
语法:File.Move(OrignFile, NewFile).同样,如果源文件不存在的话程序也会出错,移动完毕后,元文件就不存在了
move
using System.IO;
using System.Text;
public partial class Move : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
string OrignFile, NewFile;
OrignFile = Server.MapPath(".") + "\\myText.txt";
NewFile = Server.MapPath(".") + "\\myCopy.txt";
try
{
//移动文件;
File.Move(OrignFile ,NewFile );
Label lblOK = new Label();
lblOK.Text = "移动成功!";
Panel1.Controls.Add(lblOK);
}
catch (Exception ee)//捕捉异常;
{
Label lblError = new Label();
lblError.Text = "不能移动!可能因为没有这个文件!";
Panel1.Controls.Add(lblError);
}
}
}
using System.IO;
using System.Text;
public partial class Move : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
string OrignFile, NewFile;
OrignFile = Server.MapPath(".") + "\\myText.txt";
NewFile = Server.MapPath(".") + "\\myCopy.txt";
try
{
//移动文件;
File.Move(OrignFile ,NewFile );
Label lblOK = new Label();
lblOK.Text = "移动成功!";
Panel1.Controls.Add(lblOK);
}
catch (Exception ee)//捕捉异常;
{
Label lblError = new Label();
lblError.Text = "不能移动!可能因为没有这个文件!";
Panel1.Controls.Add(lblError);
}
}
}