home *** CD-ROM | disk | FTP | other *** search
/ io Programmo 40 / IOPROG_40.ISO / SOFT / NETFrameworkSDK.exe / comsdk.cab / samples.exe / ClsView / FileBrowser.aspx < prev    next >
Encoding:
Text File  |  2000-06-23  |  6.5 KB  |  229 lines

  1. <%
  2. /*=====================================================================
  3.   File:      FileBrowser.aspx
  4.  
  5.   Summary:   
  6.  
  7. ---------------------------------------------------------------------
  8.   This file is part of the Microsoft NGWS SDK Code Samples.
  9.  
  10.   Copyright (C) 2000 Microsoft Corporation.  All rights reserved.
  11.  
  12. This source code is intended only as a supplement to Microsoft
  13. Development Tools and/or on-line documentation.  See these other
  14. materials for detailed information regarding Microsoft code samples.
  15.  
  16. THIS CODE AND INFORMATION ARE PROVIDED "AS IS" WITHOUT WARRANTY OF ANY
  17. KIND, EITHER EXPRESSED OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE
  18. IMPLIED WARRANTIES OF MERCHANTABILITY AND/OR FITNESS FOR A
  19. PARTICULAR PURPOSE.
  20. =====================================================================*/
  21.  
  22. %>
  23.  
  24. <%@ Page Language="C#" %>
  25. <%@ Import Namespace="System"    %>
  26. <%@ Import Namespace="System.Reflection" %>
  27. <%@ Import Namespace="System.Collections" %>
  28. <%@ Import Namespace="System.IO"     %>
  29. <%@ Import Namespace="System.Threading"     %>
  30. <%@ Import Namespace="System.Text"     %>
  31. <%@ Import Namespace="LangUtil"     %>
  32.  
  33.  
  34. <%
  35. %>
  36. <Script runat=server>
  37.     private Directory CurrentDir = null;
  38.  
  39.  
  40. public class FileComparer : IComparer{
  41.   public int Compare( Object First, Object Second ) {
  42.     return ( 
  43.         String.Compare(
  44.              ((File)First).Name, 
  45.              ((File)Second).Name,
  46.              true 
  47.          )
  48.     );
  49.   }
  50. }
  51.  
  52. public class DirectoryComparer : IComparer{
  53.   public int Compare( Object First, Object Second ) {
  54.     return ( 
  55.         String.Compare(
  56.              ((Directory)First).Name, 
  57.              ((Directory)Second).Name,
  58.              true 
  59.          )
  60.     );
  61.   }
  62. }
  63.  
  64.  
  65. // Does what it says. Calling this method means that caller is still navigating and 
  66. // hasn't chosen a file yet.
  67.  
  68. public void ListDirectoryContents(TextWriter HTMLOutput, Directory Dir){
  69.     Response.Write("<Span style=\"font-weight:bold\">Current directory:</span> " + Dir.FullName + "<BR>");
  70.     Response.Flush();
  71.     // list Drives
  72.     string[] Drives = Directory.GetLogicalDrives();
  73.     Array.Sort(Drives);
  74.     for (int i = 0; i < Drives.Length; ++i){
  75.         HTMLOutput.Write("<IMG SRC=\"t.gif\" class=\"Icons\">");
  76.         HTMLOutput.WriteLine(
  77.             "<A HREF=\"FileBrowser.aspx?NewDrive=" 
  78.             + Drives[i]
  79.             + "&CurrentDir="
  80.             + CurrentDir.FullName 
  81.             + "\">" 
  82.             + Drives[i] 
  83.             + "</A>");
  84.         HTMLOutput.WriteLine("<BR>");
  85.     }
  86.  
  87.     // list parent of current drive.
  88.     if (CurrentDir.Parent != null){
  89.         HTMLOutput.Write(
  90.             "<IMG SRC=\"tplus.gif\" class=\"Icons\"><IMG SRC=\"foldericon.gif\" class=\"Icons\">"
  91.         );
  92.         HTMLOutput.Write(
  93.             "<A class=\"Directory\" HREF=\"FileBrowser.aspx?RequestDirectory=" 
  94.             + CurrentDir.Parent.FullName
  95.             + "&CurrentDir="
  96.             + CurrentDir.FullName 
  97.             + "&GetParent="
  98.             + "\"><parent directory></A><BR>"
  99.         );
  100.     }
  101.     Directory[] Dirs = Directory.GetDirectoriesInDirectory(CurrentDir.FullName);
  102.     DirectoryComparer DirComp = new DirectoryComparer();
  103.     Array.Sort(Dirs, DirComp);
  104.     for (int i = 0; i < Dirs.Length; ++i){
  105.         HTMLOutput.Write(
  106.             "<IMG SRC=\"tplus.gif\" class=\"Icons\"><IMG SRC=\"foldericon.gif\" class=\"Icons\"> "
  107.             );
  108.         HTMLOutput.WriteLine(
  109.             "<A HREF=\"FileBrowser.aspx?RequestDirectory=" 
  110.             + Dirs[i].FullName
  111.             + "&CurrentDir="
  112.             + CurrentDir.FullName
  113.             + "\" class=\"Directory\">" 
  114.             + Dirs[i].Name 
  115.             + "</A>");
  116.         HTMLOutput.WriteLine("<BR>");
  117.     }
  118.     
  119.     File[] Files = Dir.GetFiles("*");
  120.     FileComparer FileComp = new FileComparer();
  121.     Array.Sort(Files, FileComp);
  122.     for (int i = 0; i < Files.Length; ++i){
  123.         HTMLOutput.Write("<IMG SRC=\"t.gif\" class=\"Icons\">");
  124.         HTMLOutput.WriteLine(
  125.             "<A class=\"File\" target=\"menu\" HREF=\"Toc.aspx?Add=" 
  126.             + Files[i].Name
  127.             + "&CurrentDir="
  128.             + CurrentDir.FullName 
  129.             + "\""
  130.             + "onclick=\"javascript:window.close();\">" 
  131.             + Files[i].Name 
  132.             + "</A>");
  133.         HTMLOutput.WriteLine("<BR>");
  134.     }
  135. }
  136.  
  137.  
  138.  
  139.     /***************************************************/
  140.  
  141. //        Use Load as the main for the Web request.            
  142.         
  143.  
  144.  
  145. void Page_Load(Object sender, EventArgs EvArgs){
  146.     Response.Write("<HTML><HEAD><style type=\"text/css\">@import url(AddFile.css);</style><Title>ClassView: Microsoft NGWS SDK Sample</Title></HEAD><Body>");
  147.     try{
  148.         // load any possible values;
  149.         NameValueCollection QueryStrings = Request.QueryString;
  150.         string[] Keys, Values;
  151.         
  152.         if (QueryStrings.Count > 0)
  153.             Response.Write("Writing all parameters passed to this page.<BR>");
  154.         for (int j = 0; j < QueryStrings.Count; ++j){
  155.             Response.Write("Key: \"" + QueryStrings.AllKeys[j] + "\" Value: \"" + QueryStrings.All[j] + "\"<BR>");
  156.         }
  157.  
  158.         // if no one has chosen a directory to add yet, start with SysDir.
  159.         if (QueryStrings.Count == 0){
  160.             // set initial directory
  161.             CurrentDir = new Directory(Environment.SystemDirectory);
  162.             ListDirectoryContents(Response.Output, CurrentDir);            
  163.         }
  164.  
  165.         // if a new drive has been chosen, build a new directory and list that.
  166.         else if (QueryStrings["NewDrive"] != null){
  167.             try{
  168.                 Directory TempDirectory = new Directory(QueryStrings["NewDrive"]);
  169.                 CurrentDir = TempDirectory;
  170.             }
  171.             catch(Exception Ex){
  172.                 if (Ex is DirectoryNotFoundException){
  173.                     Response.Write("The " + QueryStrings["NewDrive"] + " drive isn't currently available. Try again.<BR>");
  174.                 }
  175.                 CurrentDir = new Directory(QueryStrings["CurrentDir"]);
  176.             }
  177.             ListDirectoryContents(Response.Output, CurrentDir);
  178.         }
  179.  
  180.         // if a directory has been chosen, build a new directory and list that.
  181.         else if (
  182.                 QueryStrings["RequestDirectory"] != null 
  183.                 && QueryStrings["CurrentDir"] != null 
  184.                 && QueryStrings["RequestFile"] == null
  185.                 && QueryStrings["NewDrive"] == null
  186.             ){
  187.             CurrentDir = new Directory(QueryStrings["RequestDirectory"]);
  188.             ListDirectoryContents(Response.Output, CurrentDir);
  189.  
  190.         }
  191.         
  192.         // otherwise, we are going to try to load an assembly.
  193.         else {
  194.             CurrentDir = new Directory(QueryStrings["CurrentDir"]);
  195.             Response.Write(
  196.                 "Yes, we are supposed to load \"" 
  197.                 + CurrentDir.FullName
  198.                 + "\\"
  199.                 + QueryStrings["RequestFile"] 
  200.                 + "\".<BR>"
  201.                 );
  202.             ListDirectoryContents(Response.Output, CurrentDir);
  203.         }
  204.     }
  205.     catch(Exception Ex){
  206.  
  207.         Response.Write(Ex.GetType().Name 
  208.             + " caught at : " 
  209.             + Ex.TargetSite.ReflectedType.Name 
  210.             + "."
  211.             + Ex.TargetSite.Name
  212.             + " >>" + Ex.Message);
  213.         Exception inner = Ex.InnerException;
  214.         while (inner != null){
  215.             Response.Write("<BR>InnerException is : " 
  216.                 + inner.GetType().Name
  217.                 + " --> "
  218.                 + inner.Message);
  219.             inner = inner.InnerException;
  220.         }
  221.     }
  222. } //END OF Load
  223.  
  224. </Script>
  225.  
  226.    </Body>
  227. </HTML>
  228.                 
  229.