ASP.NET文件管理显示信息

using System; 
using System.Collections; 
using System.ComponentModel; 
using System.Data; 
using System.Drawing; 
using System.Web; 
using System.Web.SessionState; 
using System.Web.UI; 
using System.Web.UI.WebControls; 
using System.Web.UI.HtmlControls; 
using System.IO;

namespace filemanage2 

 /// <summary> 
 /// login 的摘要说明。 
 /// </summary> 
 public class login : System.Web.UI.Page 
 { 
  protected System.Web.UI.WebControls.Label Label1; 
  protected System.Web.UI.WebControls.Label Label3; 
  protected System.Web.UI.WebControls.Label Label4; 
  protected System.Web.UI.WebControls.Table Table1; 
  protected System.Web.UI.WebControls.Button Button1; 
  protected System.Web.UI.WebControls.Label Label2; 
  protected System.Web.UI.WebControls.Label Label6; 
  protected System.Web.UI.WebControls.Label Label5; 
  protected System.Web.UI.WebControls.Label Label7; 
  protected System.Web.UI.HtmlControls.HtmlInputFile myFile; 
  
  private void Page_Load(object sender, System.EventArgs e) 
  { 
   // 在此处放置用户代码以初始化页面 
   if(!this.IsPostBack) 
   { 
////    if(Session["smallfools_filemanage"]!=null) 
////    { 
//     if(Session["smallfools_filemanage"].ToString()!="pass") 
//     { 
//      Response.Redirect("index.aspx"); 
//     } 
//     else 
//     { 
      ListFile(); 
//     } 
//    } 
//    else 
//    { 
//     Response.Redirect("index.aspx"); 
//    } 
   } 
  }

  private void ListFile() 
  { 
   //接收路径 
   string strPath=Request.QueryString["path"]; 
   //系统标识符和版本号 
   string strSystem = Environment.OSVersion.ToString(); 
   //获取映射到进程上下文的物理内存量 
   string strRem=Environment.WorkingSet.ToString(); 
   //获取系统启动后经过的毫秒数 
   int iTC=Environment.TickCount/60000; 
   //系统目录的完全限定路径 
   string strSD=Environment.SystemDirectory; 
   //获取此本地计算机的 NetBIOS 名称 
   string strMN=Environment.MachineName; 
   //获取与当前用户关联的网络域名 
   string strUDN=Environment.UserDomainName;

   this.Label5.Text="系统:"+strSystem; 
   this.Label6.Text="可用内存:"+strRem; 
   this.Label7.Text="自上次重启已有:"+iTC+"分钟了"; 
   this.Label7.Text=this.Label7.Text+"<br>系统路径:<a href='login.aspx?path="+strSD+"'>"+strSD+"</a>"; 
   this.Label7.Text=this.Label7.Text+"<br>计算机的名称:"+strMN; 
   this.Label7.Text=this.Label7.Text+"<br>网络域名:"+strUDN;

   //如果接收的路径为空,则获取当前路径 
   if(strPath==null) 
   { 
    strPath=Request.ServerVariables["APPL_PHYSICAL_PATH"]; 
   } 
   else 
   { 
    if(strPath.Substring(strPath.Length-1,1)!="\\") 
    { 
     strPath=strPath+"\\"; 
    } 
   } 
   this.Label1.Text="当目录:"+strPath;

   try 
   { 
    //输出所有驱动器号 
    string[] drv=System.IO.Directory.GetLogicalDrives(); 
    this.Label3.Text="驱动器:"; 
    for(int i=0;i<drv.Length-1;i++) 
    { 
     this.Label3.Text=this.Label3.Text+"<a href='login.aspx?path="+drv[i]+"'>"+drv[i]+"</a> "; 
    }

    //当前目录下的所有文件夹 
    DirectoryInfo di=new DirectoryInfo(strPath); 
    DirectoryInfo[] wjj=di.GetDirectories(); 
    
    //上级目录 
    if(strPath.Substring(strPath.Length-2,2)!=":\\") 
    { 
     string[] aa=strPath.Split('\\'); 
     string bb=""; 
     for(int i=0;i<aa.Length-2;i++) 
     { 
      if(i==0) 
      { 
       bb=aa[i]; 
      } 
      else 
      { 
       bb=bb+"\\"+aa[i]; 
      } 
     } 
     this.Label4.Text="<br><a href='login.aspx?path="+bb+"'>上级目录:..</a> "; 
    }


    //输出当前目录下的所有文件夹 
    foreach(DirectoryInfo diTemp in wjj) 
    { 
     TableRow tr=new TableRow(); 
     //tr.BackColor=ColorTranslator.FromHtml("#dddddd"); 
     tr.Attributes.Add("class","folder"); 
     tr.Attributes.Add("onmouseover","this.className='2'"); 
     tr.Attributes.Add("onmouseout","this.className='folder'"); 
      
     TableCell td=new TableCell();

     //文件夹名 
     td=new TableCell(); 
     td.Text="<a href='login.aspx?path="+strPath+diTemp.Name+"'>"+diTemp.Name+"</a>"; 
     tr.Cells.Add(td);

     //空列 
     td=new TableCell(); 
     tr.Cells.Add(td);

     //属性 
     td=new TableCell(); 
     td.Text=diTemp.Attributes.ToString().Replace("Archive","存档").Replace("Compressed","压缩").Replace("Device","保留").Replace("Directory","目录").Replace("Encrypted","加密").Replace("Hidden","隐藏").Replace("Normal","正常").Replace("NotContentIndexed","非索引").Replace("Offline","脱机").Replace("ReadOnly","只读").Replace("ReparsePoint","重新分析点").Replace("SparseFile","稀疏").Replace("System","系统").Replace("Temporary","临时"); 
     tr.Cells.Add(td); 
     //建立时间 
     td=new TableCell(); 
     td.Text=diTemp.CreationTime.ToString(); 
     tr.Cells.Add(td); 
     //最后访问时间 
     td=new TableCell(); 
     td.Text=diTemp.LastAccessTime.ToString(); 
     tr.Cells.Add(td); 
     //最后修改时间 
     td=new TableCell(); 
     td.Text=diTemp.LastWriteTime.ToString(); 
     tr.Cells.Add(td); 
     //删除 
     string strBack=strPath+diTemp.Name; 
     strBack=strBack.Replace("\\","\\\\"); 
     td=new TableCell(); 
     td.Text="<a href=# onclick=\"javascript:delfolder('"+strBack+"')\">删除</a>"; 
     td.HorizontalAlign=HorizontalAlign.Center; 
     tr.Cells.Add(td); 
     //移动或重命名 
     td=new TableCell(); 
     td.Text="<a href=# onclick=\"javascript:movefolder('"+strBack+"')\">移动/重命名</a>"; 
     td.HorizontalAlign=HorizontalAlign.Center; 
                    tr.Cells.Add(td); 
     this.Table1.Rows.Add(tr);

    }

    //当前目录下的所有文件 
    FileInfo[] fi=di.GetFiles(); 
    foreach(FileInfo fiTemp in fi) 
    { 
      
     TableRow tr=new TableRow(); 
     //tr.BackColor=ColorTranslator.FromHtml("#eeeeee"); 
     tr.Attributes.Add("class","file"); 
     tr.Attributes.Add("onmouseover","this.className='2'"); 
     tr.Attributes.Add("onmouseout","this.className='file'"); 
     TableCell td=new TableCell();

     //文件名 
     td=new TableCell(); 
     td.Text="<a href='read.aspx?path="+strPath+fiTemp.Name+"'target=_blank><font color=#000000>"+fiTemp.Name+"</font></a>"; 
     tr.Cells.Add(td); 
     //大小 
     td=new TableCell(); 
     td.Text=fiTemp.Length.ToString(); 
     td.HorizontalAlign=HorizontalAlign.Right; 
     tr.Cells.Add(td); 
     //属性 
     td=new TableCell(); 
     td.Text=fiTemp.Attributes.ToString().Replace("Archive","存档").Replace("Compressed","压缩").Replace("Device","保留").Replace("Directory","目录").Replace("Encrypted","加密").Replace("Hidden","隐藏").Replace("Normal","正常").Replace("NotContentIndexed","非索引").Replace("Offline","脱机").Replace("ReadOnly","只读").Replace("ReparsePoint","重新分析点").Replace("SparseFile","稀疏").Replace("System","系统").Replace("Temporary","临时"); 
     tr.Cells.Add(td); 
     //建立时间 
     td=new TableCell(); 
     td.Text=fiTemp.CreationTime.ToString(); 
     tr.Cells.Add(td); 
     //最后访问时间 
     td=new TableCell(); 
     td.Text=fiTemp.LastAccessTime.ToString(); 
     tr.Cells.Add(td); 
     //最后修改时间 
     td=new TableCell(); 
     td.Text=fiTemp.LastWriteTime.ToString(); 
     tr.Cells.Add(td); 
     //删除 
     string strBack=strPath+fiTemp.Name; 
     strBack=strBack.Replace("\\","\\\\"); 
     td=new TableCell(); 
     td.Text="<a href=# onclick=\"javascript:delfile('"+strBack+"')\">删除</a>"; 
     td.HorizontalAlign=HorizontalAlign.Center; 
     tr.Cells.Add(td); 
     //移动或重命名 
     td=new TableCell(); 
     td.Text="<a href=# onclick=\"javascript:movefile('"+strBack+"')\">移动/重命名</a>"; 
     td.HorizontalAlign=HorizontalAlign.Center; 
     tr.Cells.Add(td); 
     this.Table1.Rows.Add(tr);

    } 
   } 
   catch(Exception ex) 
   { 
    this.Button1.Visible=false; 
    this.myFile.Visible=false; 
    this.Label2.Text="<br><p align=center>"+ex.Message+"</p>"; 
   } 
  }

  #region Web Form Designer generated code 
  override protected void OnInit(EventArgs e) 
  { 
   // 
   // CODEGEN:该调用是 ASP.NET Web 窗体设计器所必需的。 
   // 
   InitializeComponent(); 
   base.OnInit(e); 
  } 
   
  /// <summary> 
  /// 设计器支持所需的方法 - 不要使用代码编辑器修改 
  /// 此方法的内容。 
  /// </summary> 
  private void InitializeComponent() 
  {    
   this.Button1.Click += new System.EventHandler(this.Button1_Click); 
   this.Load += new System.EventHandler(this.Page_Load);

  } 
  #endregion

  private void Button1_Click(object sender, System.EventArgs e) 
  { 
   if(this.myFile.PostedFile!=null) 
   { 
    //接收路径 
    string strPath=Request.QueryString["path"];

    //如果接收的路径为空,则获取当前路径 
    if(strPath==null) 
    { 
     strPath=Request.ServerVariables["APPL_PHYSICAL_PATH"]; 
    } 
    else 
    { 
     if(strPath.Substring(strPath.Length-1,1)!="\\") 
     { 
      strPath=strPath+"\\"; 
     } 
    }

    //处理字符串 
    string strName=this.myFile.PostedFile.FileName; 
    int i=strName.LastIndexOf("\\"); 
    string strNewName=strName.Substring(i); 
    string strFullName=strPath+strNewName; 
    //保存文件 
    this.myFile.PostedFile.SaveAs(strFullName); 
    //Response.Redirect("index.aspx?path="+strPath); 
    this.Label2.Text="<center><font color=red>上传成功</font></center>"; 
    ListFile(); 
   } 
  } 
 } 

原文地址:https://www.cnblogs.com/top5/p/1681283.html