System.IO命名空间操作文件系统

System.IO命名空间常用的操作主要有两部分:

  1. 对文件的处理 
  2. 对文件夹的处理  


对文件的处理

★创建一个文本的语法:
public static StreamWriter File.CreateText(路径)

ContractedBlock.gif ExpandedBlockStart.gif 创建文本文件
None.gifusing System.IO;
None.gif
using System.Text;
None.gif
None.gif
public partial class _Default : System.Web.UI.Page 
ExpandedBlockStart.gifContractedBlock.gif
dot.gif{
InBlock.gif    
protected void Page_Load(object sender, EventArgs e)
ExpandedSubBlockStart.gifContractedSubBlock.gif    
dot.gif{
InBlock.gif        
//建立StreamWrite
InBlock.gif
        StreamWriter rw = File.CreateText(Server.MapPath("."+ "\\myText.txt");
InBlock.gif        rw.WriteLine(
"我爱asp.net");//使用WriteLine写入内容;
InBlock.gif
        rw.WriteLine("我爱北京天安门");
InBlock.gif        rw.Flush();
//将缓冲区的内容写入文件;
InBlock.gif
        rw.Close();//关闭rw对象;
InBlock.gif
InBlock.gif        
//打开文本文件;
InBlock.gif
        StreamReader sr = File.OpenText(Server.MapPath("."+ "\\myText.txt");
InBlock.gif        StringBuilder output 
= new StringBuilder();
InBlock.gif        
string rl;
InBlock.gif        
while ((rl = sr.ReadLine() )!= null)
ExpandedSubBlockStart.gifContractedSubBlock.gif        
dot.gif{
InBlock.gif            output.Append(rl 
+ "<br>");//StringBuilder.Append(*) 在实例的结尾追加;
InBlock.gif

InBlock.gif
InBlock.gif         
ExpandedSubBlockEnd.gif        }

InBlock.gif        lblFile.Text 
= output.ToString();
InBlock.gif        sr.Close();
InBlock.gif
ExpandedSubBlockEnd.gif        }

ExpandedBlockEnd.gif}

程序首先在当前目录下创建一个文本文件,然后写入两行文字,接着创建一个StreamReader对象读取文件的内容,并将文件的内容显示到浏览器上:

★添加文本文件
函数"public static StreamWriter AppendText(string path)"为文件添加内容其功能是课教案用于添加文本的StreamWriter对象,使用方法如下:
注意下面的while而不是if

ContractedBlock.gif ExpandedBlockStart.gif 添加文字
None.gifpublic partial class Default2 : System.Web.UI.Page
ExpandedBlockStart.gifContractedBlock.gif
dot.gif{
InBlock.gif    
protected void Page_Load(object sender, EventArgs e)
ExpandedSubBlockStart.gifContractedSubBlock.gif    
dot.gif{
InBlock.gif        StreamWriter sw
=File .AppendText (Server.MapPath (".")+"\\myText.txt");
InBlock.gif        sw.WriteLine (
"我还是爱asp.net");
InBlock.gif        sw.WriteLine (
"当然了,我很爱小宝贝哦!");
InBlock.gif        sw.Flush ();
InBlock.gif        sw.Close ();
InBlock.gif
InBlock.gif        
//以下读取
InBlock.gif
        StreamReader sr = File.OpenText(Server.MapPath("."+ "\\myText.txt");
InBlock.gif        StringBuilder output 
= new StringBuilder();
InBlock.gif        
string rl;
InBlock.gif        
while ((rl = sr.ReadLine()) != null)
ExpandedSubBlockStart.gifContractedSubBlock.gif        
dot.gif{
InBlock.gif
InBlock.gif            output.Append(rl);
ExpandedSubBlockEnd.gif        }

InBlock.gif        txtfile.Text 
= output.ToString();
InBlock.gif        sr.Close();
InBlock.gif
InBlock.gif
InBlock.gif
ExpandedSubBlockEnd.gif    }

ExpandedBlockEnd.gif}

None.gif

程序创建一个StreamWriter对象,向文本文件添加文本

★拷贝文本文件
使用File对象的Copy方法,格式:"File.Copy(OrignFile,NewFile,true)",参数true是指如果目标文件存在就覆盖;

ContractedBlock.gif ExpandedBlockStart.gif 复制文本
None.gifusing System.IO;
None.gif
using System.Text;
None.gif
public partial class Copy : System.Web.UI.Page
ExpandedBlockStart.gifContractedBlock.gif
dot.gif{
InBlock.gif    
protected void Page_Load(object sender, EventArgs e)
ExpandedSubBlockStart.gifContractedSubBlock.gif    
dot.gif{
InBlock.gif        
string OrignFile, NewFile;
InBlock.gif        OrignFile 
= Server.MapPath("."+ "\\myText.txt";
InBlock.gif        NewFile 
=Server.MapPath (".")+"\\myCopy.txt";
InBlock.gif        
//拷贝文件
InBlock.gif
        try 
ExpandedSubBlockStart.gifContractedSubBlock.gif        
dot.gif{
InBlock.gif            File .Copy (OrignFile ,NewFile ,
true );
InBlock.gif            
if (File.Exists(NewFile))//如果myCopy.txt存在;
ExpandedSubBlockStart.gifContractedSubBlock.gif
            dot.gif{
InBlock.gif                FileInfo fi 
= new FileInfo(NewFile);
InBlock.gif                DateTime ctime 
= fi.CreationTime;
InBlock.gif                lbl.Text 
= NewFile + "<br>创建的时间是:" + ctime.ToString() +"<br>";
ExpandedSubBlockEnd.gif            }

InBlock.gif            
else
ExpandedSubBlockStart.gifContractedSubBlock.gif            
dot.gif{
InBlock.gif                lbl.Text 
= NewFile + "不存在<br>";
ExpandedSubBlockEnd.gif            }

InBlock.gif
InBlock.gif
ExpandedSubBlockEnd.gif        }

InBlock.gif        
catch
ExpandedSubBlockStart.gifContractedSubBlock.gif        
dot.gif{
InBlock.gif            lblError.Text 
= "不能拷贝!";
ExpandedSubBlockEnd.gif        }

InBlock.gif
ExpandedSubBlockEnd.gif    }

ExpandedBlockEnd.gif}

None.gif

如果源文件存在的话就可以拷贝了.并且在浏览器上显示出源文件的创建时间;

★删除文件
基本语法:File.Delete(文件路径),如果要删除的文件不存在的话程序会有异常;所以要先判断文件是否存在

ContractedBlock.gif ExpandedBlockStart.gif delete
None.gifusing System.IO;
None.gif
using System.Text;
None.gif
None.gif
public partial class Delete : System.Web.UI.Page
ExpandedBlockStart.gifContractedBlock.gif
dot.gif{
InBlock.gif    
protected void Page_Load(object sender, EventArgs e)
ExpandedSubBlockStart.gifContractedSubBlock.gif    
dot.gif{
InBlock.gif        
//首先判断文件是否存在;
InBlock.gif
        string delFile = Server.MapPath("."+ "\\myCopy.txt";
InBlock.gif        
try
ExpandedSubBlockStart.gifContractedSubBlock.gif        
dot.gif{
InBlock.gif            
//删除文件
InBlock.gif
            File.Delete(delFile);
InBlock.gif            Label lblOK 
= new Label();
InBlock.gif            lblOK.Text 
= "删除成功!";
InBlock.gif            Panel1.Controls.Add(lblOK);
InBlock.gif            
InBlock.gif
InBlock.gif
ExpandedSubBlockEnd.gif        }

InBlock.gif        
catch(Exception ee)//捕捉异常;
ExpandedSubBlockStart.gifContractedSubBlock.gif
        dot.gif{
InBlock.gif            Label lblError 
= new Label();
InBlock.gif            lblError.Text 
= "不能删除!可能因为没有这个文件!";
InBlock.gif            Panel1.Controls.Add(lblError );
ExpandedSubBlockEnd.gif        }

ExpandedSubBlockEnd.gif    }

ExpandedBlockEnd.gif}

★移动文件
语法:File.Move(OrignFile, NewFile).同样,如果源文件不存在的话程序也会出错,移动完毕后,元文件就不存在了

ContractedBlock.gif ExpandedBlockStart.gif move
None.gifusing System.IO;
None.gif
using System.Text;
None.gif
None.gif
public partial class Move : System.Web.UI.Page
ExpandedBlockStart.gifContractedBlock.gif
dot.gif{
InBlock.gif    
protected void Page_Load(object sender, EventArgs e)
ExpandedSubBlockStart.gifContractedSubBlock.gif    
dot.gif{
InBlock.gif        
string OrignFile, NewFile;
InBlock.gif
InBlock.gif        OrignFile 
= Server.MapPath("."+ "\\myText.txt";
InBlock.gif        NewFile 
= Server.MapPath("."+ "\\myCopy.txt";
InBlock.gif        
try
ExpandedSubBlockStart.gifContractedSubBlock.gif        
dot.gif{
InBlock.gif            
//移动文件;
InBlock.gif
            File.Move(OrignFile ,NewFile );
InBlock.gif            Label lblOK 
= new Label();
InBlock.gif            lblOK.Text 
= "移动成功!";
InBlock.gif            Panel1.Controls.Add(lblOK);
InBlock.gif
InBlock.gif
InBlock.gif
ExpandedSubBlockEnd.gif        }

InBlock.gif        
catch (Exception ee)//捕捉异常;
ExpandedSubBlockStart.gifContractedSubBlock.gif
        dot.gif{
InBlock.gif            Label lblError 
= new Label();
InBlock.gif            lblError.Text 
= "不能移动!可能因为没有这个文件!";
InBlock.gif            Panel1.Controls.Add(lblError);
ExpandedSubBlockEnd.gif        }

ExpandedSubBlockEnd.gif    }

ExpandedBlockEnd.gif}

None.gif


对文件的操作

 

转载于:https://www.cnblogs.com/sliuqin/archive/2007/01/03/610601.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值