NET操作文件 一 -----File类的使用

来源:互联网 发布:绝地求生n卡画面优化 编辑:程序博客网 时间:2024/05/16 10:12

 

NET操作文件 一 -----File类的使用

System.IO
Directory:用于创建、移动和枚举通过目录和子目录
File:用于创建、复制、删除、移动和打开文件
Path:对包含文件或目录路径信息的String实例执行操作
StreamReader、StreamWriter:以一种特定的编码写字符

File类常用的方法
AppendText:创建一个StreamWriter对象,用于在指定文件的末尾添加新的内容
Copy:复制指定文件
Move:移动文件
Delete:删除文件
Exit:判断指定文件是否存在
Open:以指定的方式、权限打开指定文件
OpenRead:以只读方式打开指定文件
OpenText:打开文本文件,返回流
OpenWrite:以读写方式打开指定文件
Cteate:创建一个指定文件
CreateText:创建一个文本文件

File类的使用

using System.IO;
using System.Text;
private void Page_Load(object sender, System.EventArgs e)
{
//建立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();
}

using System.IO;
using System.Text;
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();
}

文件复制
private void Page_Load(object sender, System.EventArgs e)
{
string OrignFile= Server.MapPath(".")+"
//CreateText.txt";
string 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>";
}
}
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);
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;
}
}

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>";
}
}
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);
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 = "不能移动文件";
}
}

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);
}
}

FileSteam常用属性和方法
CanRead:判断当前是否支持读取
Canwrite:判断当前是否支持写入
CanSeek:是否支持搜索
IsAsync:是否处于异步打开模式
Postion:设置获取当前流所处位置
Flush:将当前缓存区的数据写入文件
Lock:锁定流,防止其他文件访问
Seek:设置当前流操作的指针位置

原创粉丝点击