home *** CD-ROM | disk | FTP | other *** search
/ Chip 2002 September / Chip_2002-09_cd1.bin / zkuste / vbasic / Data / Utils / XZipNet.msi / Data1.cab / Main.cs < prev    next >
Encoding:
Text File  |  2002-02-05  |  13.1 KB  |  371 lines

  1. /*
  2.  * Xceed Zip for .NET - UsingExtendingDemo Sample Application
  3.  * Copyright (c) 2000-2002 - Xceed Software Inc.
  4.  * 
  5.  * [Main.cs]
  6.  * 
  7.  * This application demonstrates how to use the IsolatedFile and IsolatedFolder
  8.  * class of the EntendingDemo sample application. Both of these classes 
  9.  * demonstrate how to derive from the AbstractFolder and AbstractFile classes.
  10.  *  * 
  11.  * This file is part of Xceed Zip for .NET. The source code in this file 
  12.  * is only intended as a supplement to the documentation, and is provided 
  13.  * "as is", without warranty of any kind, either expressed or implied.
  14.  */
  15.  
  16. using System;
  17. using System.IO;
  18. using System.Drawing;
  19. using System.Collections;
  20. using System.ComponentModel;
  21. using System.Windows.Forms;
  22. using System.Data;
  23. using Xceed.FileSystem;
  24. using Xceed.FileSystem.Samples.ExtendingDemo;
  25.  
  26. namespace UsingExtendingDemo
  27. {  
  28.   public class Form1 : System.Windows.Forms.Form
  29.   {
  30.     private System.Windows.Forms.Button btCopy;
  31.     private System.Windows.Forms.Button btMove;
  32.     private System.Windows.Forms.Button btDelete;
  33.     private System.Windows.Forms.ImageList images;
  34.     private System.Windows.Forms.ListView listIsolated;
  35.     private System.Windows.Forms.Label label1;
  36.     private System.Windows.Forms.TextBox txtInput;
  37.     private System.Windows.Forms.Button btCreate;
  38.     private System.Windows.Forms.TextBox txtCreate;
  39.     private System.Windows.Forms.Label label2;    
  40.     private System.ComponentModel.IContainer components;
  41.  
  42.     public Form1()
  43.     {
  44.       //
  45.       // Required for Windows Form Designer support
  46.       //
  47.       InitializeComponent();
  48.  
  49.     }
  50.  
  51.     /// <summary>
  52.     /// Clean up any resources being used.
  53.     /// </summary>
  54.     protected override void Dispose( bool disposing )
  55.     {
  56.       if( disposing )
  57.       {
  58.         if (components != null) 
  59.         {
  60.           components.Dispose();
  61.         }
  62.       }
  63.       base.Dispose( disposing );
  64.     }
  65.  
  66.         #region Windows Form Designer generated code
  67.     /// <summary>
  68.     /// Required method for Designer support - do not modify
  69.     /// the contents of this method with the code editor.
  70.     /// </summary>
  71.     private void InitializeComponent()
  72.     {
  73.       this.components = new System.ComponentModel.Container();
  74.       System.Resources.ResourceManager resources = new System.Resources.ResourceManager(typeof(Form1));
  75.       this.btCopy = new System.Windows.Forms.Button();
  76.       this.btMove = new System.Windows.Forms.Button();
  77.       this.btDelete = new System.Windows.Forms.Button();
  78.       this.images = new System.Windows.Forms.ImageList(this.components);
  79.       this.listIsolated = new System.Windows.Forms.ListView();
  80.       this.label1 = new System.Windows.Forms.Label();
  81.       this.txtInput = new System.Windows.Forms.TextBox();
  82.       this.btCreate = new System.Windows.Forms.Button();
  83.       this.txtCreate = new System.Windows.Forms.TextBox();
  84.       this.label2 = new System.Windows.Forms.Label();
  85.       this.SuspendLayout();
  86.       // 
  87.       // btCopy
  88.       // 
  89.       this.btCopy.Location = new System.Drawing.Point(8, 8);
  90.       this.btCopy.Name = "btCopy";
  91.       this.btCopy.TabIndex = 5;
  92.       this.btCopy.Text = "Copy";
  93.       this.btCopy.Click += new System.EventHandler(this.btCopy_Click);
  94.       // 
  95.       // btMove
  96.       // 
  97.       this.btMove.Location = new System.Drawing.Point(88, 8);
  98.       this.btMove.Name = "btMove";
  99.       this.btMove.TabIndex = 6;
  100.       this.btMove.Text = "Move";
  101.       this.btMove.Click += new System.EventHandler(this.btMove_Click);
  102.       // 
  103.       // btDelete
  104.       // 
  105.       this.btDelete.Location = new System.Drawing.Point(168, 8);
  106.       this.btDelete.Name = "btDelete";
  107.       this.btDelete.TabIndex = 8;
  108.       this.btDelete.Text = "Delete";
  109.       this.btDelete.Click += new System.EventHandler(this.btDelete_Click);
  110.       // 
  111.       // images
  112.       // 
  113.       this.images.ColorDepth = System.Windows.Forms.ColorDepth.Depth8Bit;
  114.       this.images.ImageSize = new System.Drawing.Size(16, 16);
  115.       this.images.ImageStream = ((System.Windows.Forms.ImageListStreamer)(resources.GetObject("images.ImageStream")));
  116.       this.images.TransparentColor = System.Drawing.Color.White;
  117.       // 
  118.       // listIsolated
  119.       // 
  120.       this.listIsolated.FullRowSelect = true;
  121.       this.listIsolated.Location = new System.Drawing.Point(8, 80);
  122.       this.listIsolated.MultiSelect = false;
  123.       this.listIsolated.Name = "listIsolated";
  124.       this.listIsolated.Size = new System.Drawing.Size(312, 152);
  125.       this.listIsolated.SmallImageList = this.images;
  126.       this.listIsolated.TabIndex = 14;
  127.       this.listIsolated.View = System.Windows.Forms.View.List;
  128.       this.listIsolated.DoubleClick += new System.EventHandler(this.listIsolated_DoubleClick);
  129.       // 
  130.       // label1
  131.       // 
  132.       this.label1.Location = new System.Drawing.Point(8, 40);
  133.       this.label1.Name = "label1";
  134.       this.label1.Size = new System.Drawing.Size(100, 16);
  135.       this.label1.TabIndex = 16;
  136.       this.label1.Text = "Destination folder :";
  137.       // 
  138.       // txtInput
  139.       // 
  140.       this.txtInput.Location = new System.Drawing.Point(8, 56);
  141.       this.txtInput.Name = "txtInput";
  142.       this.txtInput.Size = new System.Drawing.Size(312, 20);
  143.       this.txtInput.TabIndex = 17;
  144.       this.txtInput.Text = "D:\\dump";
  145.       // 
  146.       // btCreate
  147.       // 
  148.       this.btCreate.Location = new System.Drawing.Point(248, 256);
  149.       this.btCreate.Name = "btCreate";
  150.       this.btCreate.TabIndex = 18;
  151.       this.btCreate.Text = "Create";
  152.       this.btCreate.Click += new System.EventHandler(this.btCreate_Click);
  153.       // 
  154.       // txtCreate
  155.       // 
  156.       this.txtCreate.Location = new System.Drawing.Point(8, 256);
  157.       this.txtCreate.Name = "txtCreate";
  158.       this.txtCreate.Size = new System.Drawing.Size(232, 20);
  159.       this.txtCreate.TabIndex = 19;
  160.       this.txtCreate.Text = "test.txt";
  161.       // 
  162.       // label2
  163.       // 
  164.       this.label2.Location = new System.Drawing.Point(8, 240);
  165.       this.label2.Name = "label2";
  166.       this.label2.Size = new System.Drawing.Size(160, 16);
  167.       this.label2.TabIndex = 20;
  168.       this.label2.Text = "File or folder name to create:";
  169.       // 
  170.       // Form1
  171.       // 
  172.       this.AutoScaleBaseSize = new System.Drawing.Size(5, 13);
  173.       this.ClientSize = new System.Drawing.Size(328, 285);
  174.       this.Controls.AddRange(new System.Windows.Forms.Control[] {
  175.                                                                   this.label2,
  176.                                                                   this.txtCreate,
  177.                                                                   this.btCreate,
  178.                                                                   this.txtInput,
  179.                                                                   this.label1,
  180.                                                                   this.listIsolated,
  181.                                                                   this.btDelete,
  182.                                                                   this.btMove,
  183.                                                                   this.btCopy});
  184.       this.Name = "Form1";
  185.       this.Text = "UsingExtendingDemo sample application";
  186.       this.Load += new System.EventHandler(this.Form1_Load);
  187.       this.ResumeLayout(false);
  188.  
  189.     }
  190.         #endregion
  191.  
  192.     /// <summary>
  193.     /// The main entry point for the application.
  194.     /// </summary>
  195.     [STAThread]
  196.     static void Main() 
  197.     {
  198.       Application.Run(new Form1());
  199.     }
  200.  
  201.     /// <summary>
  202.     /// Load the form and call the <see cref="FillList"/> method to 
  203.     /// initialize the contents of the listview.
  204.     /// </summary>    
  205.     private void Form1_Load(object sender, System.EventArgs e)
  206.     {      
  207.       FillList( @"\" );      
  208.     }
  209.  
  210.     /// <summary>
  211.     /// Fill the listview control with the items of the folder.
  212.     /// </summary>
  213.     /// <param name="folderName">The fullname and path of the folder whose contents will
  214.     /// be listed.</param>
  215.     private void FillList( string folderName )
  216.     {      
  217.       listIsolated.Items.Clear();
  218.       IsolatedFolder folder = new IsolatedFolder( folderName );
  219.  
  220.       listIsolated.Items.Add( @".", 1 );      
  221.  
  222.       foreach( FileSystemItem item in folder.GetItems( false ) )
  223.       {
  224.         if( item is IsolatedFolder )
  225.         {
  226.           listIsolated.Items.Add( item.Name, 1 ); // Folder
  227.         }
  228.         else
  229.         {
  230.           listIsolated.Items.Add( item.Name, 0 ); //File
  231.         }
  232.       }
  233.  
  234.       m_currentFolder = folder.FullName;         
  235.     }
  236.  
  237.     /// <summary>
  238.     /// Copy the file or folder (and it's content) to the destination disk folder.
  239.     /// </summary>    
  240.     private void btCopy_Click(object sender, System.EventArgs e)
  241.     {
  242.       DiskFolder destFolder = new DiskFolder( txtInput.Text );
  243.  
  244.       // Check the ImageIndex of the list item to see if we are dealing with a 
  245.       // file or a folder.
  246.       if( listIsolated.FocusedItem.ImageIndex == 0 ) // File
  247.       {
  248.         IsolatedFile file = new IsolatedFile( m_currentFolder + listIsolated.FocusedItem.Text );
  249.         file.CopyTo( destFolder, true );
  250.       }
  251.       else // Folder
  252.       {
  253.         IsolatedFolder folder = new IsolatedFolder( m_currentFolder + listIsolated.FocusedItem.Text );
  254.         folder.CopyTo( destFolder, true );
  255.       }
  256.     }
  257.  
  258.     /// <summary>
  259.     /// Move the file or folder (and it's content) to the destination disk folder.
  260.     /// </summary>    
  261.     private void btMove_Click(object sender, System.EventArgs e)
  262.     {
  263.       DiskFolder destFolder = new DiskFolder( txtInput.Text );
  264.  
  265.       // Check the ImageIndex of the list item to see if we are dealing with a 
  266.       // file or a folder.
  267.       if( listIsolated.FocusedItem.ImageIndex == 0 ) // File
  268.       {
  269.         IsolatedFile file = new IsolatedFile( m_currentFolder + listIsolated.FocusedItem.Text );
  270.         file.MoveTo( destFolder, true );
  271.       }
  272.       else // Folder
  273.       {
  274.         IsolatedFolder folder = new IsolatedFolder( m_currentFolder + listIsolated.FocusedItem.Text );
  275.         folder.MoveTo( destFolder, true );
  276.       }
  277.  
  278.       // Refresh our list view control.
  279.       FillList( m_currentFolder );
  280.     }
  281.  
  282.     /// <summary>
  283.     /// Delete the selected item in the list view control
  284.     /// </summary>    
  285.     private void btDelete_Click(object sender, System.EventArgs e)
  286.     {      
  287.       // Check the ImageIndex of the list item to see if we are dealing with a
  288.       // file or a folder.
  289.       if( listIsolated.FocusedItem.ImageIndex == 0 ) // File
  290.       {
  291.         IsolatedFile file = new IsolatedFile( m_currentFolder + listIsolated.FocusedItem.Text );
  292.         file.Delete();
  293.       }
  294.       else // Folder
  295.       { 
  296.         if( listIsolated.FocusedItem.Text != @"." )
  297.         {
  298.           IsolatedFolder folder = new IsolatedFolder( m_currentFolder + listIsolated.FocusedItem.Text );        
  299.  
  300.           if( !folder.IsRoot )
  301.           {
  302.             folder.Delete();
  303.           }
  304.         }        
  305.       }
  306.       // Refresh our list view control.
  307.       FillList( m_currentFolder );             
  308.     }
  309.  
  310.     /// <summary>
  311.     /// Create the new item.
  312.     /// </summary>    
  313.     private void btCreate_Click(object sender, System.EventArgs e)
  314.     {
  315.       // If the filename includes ans extension, we will create a file.
  316.       if( Path.HasExtension( txtCreate.Text ) )
  317.       {
  318.         IsolatedFile file = new IsolatedFile( m_currentFolder + txtCreate.Text );
  319.         
  320.         if( !file.Exists )
  321.         {
  322.           file.Create();
  323.         }
  324.       }
  325.       else // If the filename does not have an extension, we will create a folder.
  326.       {
  327.         IsolatedFolder folder = new IsolatedFolder( m_currentFolder + txtCreate.Text );
  328.  
  329.         if( !folder.Exists )
  330.         {
  331.           folder.Create();
  332.         }
  333.       }
  334.  
  335.       // Refresh our list view control.
  336.       FillList( m_currentFolder );        
  337.     }
  338.   
  339.     /// <summary>
  340.     /// Change the current location when clicking on an item in the list.
  341.     /// </summary>    
  342.     private void listIsolated_DoubleClick(object sender, System.EventArgs e)
  343.     {
  344.       // We will only change folders if the ImageIndex indicates that the
  345.       // selected items is a folder.
  346.       if( listIsolated.FocusedItem.ImageIndex == 1 )
  347.       {
  348.         // If we are at the root level, we cannot go up a level. Therefore
  349.         // we verify to make sure that the folder is not a root folder
  350.         // before attempting to fill the list. 
  351.         IsolatedFolder folder = new IsolatedFolder( m_currentFolder );
  352.  
  353.         // If the selected item is "." and is not a root folder, we 
  354.         // go to the parent folder and list the contents.
  355.         if( listIsolated.FocusedItem.Text == @"." && !folder.IsRoot )
  356.         {          
  357.           FillList( Path.GetDirectoryName( m_currentFolder.Remove( m_currentFolder.Length -1, 1 ) ) );          
  358.         }
  359.         else // Go to the selected subfolder and list the contents.
  360.         {
  361.           FillList( m_currentFolder + listIsolated.FocusedItem.Text );
  362.         }
  363.       }
  364.     }   
  365.  
  366.     // Contains the path and name of the current folder.
  367.     private string m_currentFolder;    
  368.   }
  369. }      
  370.  
  371.