asp.net io類目錄操作實(shí)例 完成取得驅(qū)動(dòng)器列表 顯示目錄下的子目錄和文件 讀取文件內(nèi)容 顯示文件信息
iodirectory.aspx.cs代碼(實(shí)現(xiàn)驅(qū)動(dòng)器目錄列表)
private void page_load(object sender, system.eventargs e)
  {
   // 在此處放置用戶代碼以初始化頁(yè)面
   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)
  {
   // 在此處放置用戶代碼以初始化頁(yè)面
   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")進(jìn)行文件搜索
    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代碼(顯示文件內(nèi)容)
public string strfileshow;
  public fileinfo thisone;
  private void page_load(object sender, system.eventargs e)
  {
   // 在此處放置用戶代碼以初始化頁(yè)面
   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("文件建立時(shí)間:"+thisone.creationtime.tostring()+"<br>");
   response.write("文件大小:"+thisone.length+"bytes<br>");
   response.write("最后訪問時(shí)間:"+thisone.lastaccesstime+"<br>");
   streamshow();
  }
  private void streamshow(){//顯示文件
   //相關(guān)學(xué)習(xí)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>");
  }
新聞熱點(diǎn)
疑難解答
圖片精選