首页 > 编程 > .NET > 正文

Asp.net IO类目录操作实例 完成获得驱动器列表 显示目录文件 读取文件内容

2024-07-10 13:05:49
字体:
来源:转载
供稿:网友

asp.net io类目录操作实例 完成取得驱动器列表 显示目录下的子目录和文件 读取文件内容 显示文件信息

iodirectory.aspx.cs代码(实现驱动器目录列表)

private void page_load(object sender, system.eventargs e)
  {
   // 在此处放置用户代码以初始化页面
   string[] drives=directory.getlogicaldrives();
   int numofdrives=drives.length;
   response.write("<ul>");
   for(int i=0;i<numofdrives;i++){
   response.write("<li><a href=/"listdir.aspx?dir=");
    response.write(server.urlencode(drives[i]));
    response.write("/">"+drives[i]);
    response.write("</a><br>");
   }
   response.write("</ul>");
      }

listdir.aspx.cs代码(显示目录下的子目录和文件)

private void page_load(object sender, system.eventargs e)
  {
   // 在此处放置用户代码以初始化页面
   string strdirlist=request.querystring.get("dir");
   directoryinfo directory=null;
   try{
   directory=new directoryinfo(strdirlist);    
    //读取目录属性
    response.write("<p>creation:"+directory.creationtime.tostring()+"</p>");
    directoryinfo[] subdirectory=directory.getdirectories();
    response.write("<ul>");
    for(int i=0;i<subdirectory.length;i++){
    response.write("<li><a href=/"listdir.aspx?dir=");
     response.write(server.urlencode(subdirectory[i].fullname));
     response.write("/">"+subdirectory[i].name);
     response.write("</a><br>");
    }
    response.write("</ul>");
    fileinfo[] thefiles=directory.getfiles();//可以使用getfiles("*.txt")进行文件搜索
    response.write("<ul>");
    for(int i=0;i<thefiles.length;i++){
    response.write("<li><a href=/"showfile.aspx?file=");
     response.write(server.urlencode(thefiles[i].fullname));
     response.write("/">"+thefiles[i].name);
     response.write("</a><br>");
    }
    response.write("</ul>");
       }
  catch(exception ex){
    response.write("access not possible. error:<i>");
    response.write(e.tostring()+"</i>");
    response.end();
   }
  }

showfile.aspx.cs代码(显示文件内容)

public string strfileshow;
  public fileinfo thisone;
  private void page_load(object sender, system.eventargs e)
  {
   // 在此处放置用户代码以初始化页面
   encoding ed=encoding.getencoding("gb2312");
   response.contentencoding=ed;
   request.contentencoding=ed;
   strfileshow=request.querystring.get("file");
   thisone=new fileinfo(strfileshow);
   response.write("文件名:"+thisone.name+"<br>");
   response.write("文件路径:"+thisone.fullname+"<br>");
   response.write("文件目录:"+thisone.directoryname+"<br>");
   response.write("文件建立时间:"+thisone.creationtime.tostring()+"<br>");
   response.write("文件大小:"+thisone.length+"bytes<br>");
   response.write("最后访问时间:"+thisone.lastaccesstime+"<br>");
   streamshow();
  }
  private void streamshow(){//显示文件
   //相关学习stream streamwriter streamreader filestream stream text 编码问题postedfile
   //filestream fs=new filestream(thisone,filemode.open,fileaccess.read);
   //response.charset=encoding.getencoding("gb2312");
   streamreader dr=thisone.opentext();
   char[] thebuffer=new char[255];
   int nread=dr.readblock(thebuffer,0,255);
   //string strshow=dr.readtoend();
   response.write("<pre>");
   //response.write(strshow);
   response.write(server.htmlencode(new string(thebuffer,0,nread)));
   //response.write(new string(thebuffer,0,nread,system.text.encoding.getencoding("gb2312")));
   response.write("</pre>");
  }


发表评论 共有条评论
用户名: 密码:
验证码: 匿名发表