⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 ex-13-03

📁 Programming Csharp Source Code(代码) Programming Csharp Source Code
💻
📖 第 1 页 / 共 2 页
字号:
using System;
using System.Collections;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.IO;
using System.Windows.Forms;


/// <remarks>
///    File Copier - WinForms demonstration program
///    (c) Copyright 2001 Liberty Associates, Inc.
/// </remarks>
namespace FileCopier
{
	/// <summary>
	/// Form demonstrating Windows Forms implementation
	/// </summary>
	public class Form1 : System.Windows.Forms.Form
	{
		/// <summary>
		/// Required designer variable.
		/// </summary>
		private System.ComponentModel.Container 
         components;

      /// <summary>
      ///   Tree view of potential target directories
      /// </summary>
      private System.Windows.Forms.TreeView tvwTargetDir;

      /// <summary>
      ///    Tree view of source directories
      ///    includes check boxes for checking
      ///    chosen files or directories
      /// </summary>
      private System.Windows.Forms.TreeView tvwSource;

      /// <summary>
      ///   Tree view of potential target directories
      /// </summary>
      private System.Windows.Forms.TextBox txtTargetDir;

      /// <summary>
      ///    Label displays progress when
      ///    copying or deleting files
      /// </summary>
      private System.Windows.Forms.Label lblStatus;

      /// <summary>
      ///    If checked, when copying we'll
      ///    overwrite existing files
      /// </summary>
      private System.Windows.Forms.CheckBox chkOverwrite;

      /// <summary>
      ///    When pressed, sets all check boxes
      ///    in source tree view to clear
      /// </summary>
      private System.Windows.Forms.Button btnClear;

      /// <summary>
      ///    Shuts the application
      /// </summary>
      private System.Windows.Forms.Button btnCancel;

      /// <summary>
      ///    Deletes the selected files
      /// </summary>
      private System.Windows.Forms.Button btnDelete;

      /// <summary>
      ///    Copies the selected files
      ///    to the target directory
      /// </summary>
      private System.Windows.Forms.Button btnCopy;

      private System.Windows.Forms.Label label1;
      private System.Windows.Forms.Label label2;

      /// <summary>
      ///    internal class which knows how to compare 
      ///    two files we want to sort large to small, 
      ///    so reverse the normal return values.
      /// </summary>
      public class FileComparer : IComparer
      {
         public int Compare (object f1, object f2)
         {
            FileInfo file1 = (FileInfo) f1;
            FileInfo file2 = (FileInfo) f2;
            if (file1.Length > file2.Length)
            {
               return -1;  
            }
            if (file1.Length < file2.Length)
            {
               return 1;
            }
            return 0;
         }
      }

		public Form1()
		{
			//
			// Required for Windows Form Designer support
			//
			InitializeComponent();

         // fill the source and target directory trees
         FillDirectoryTree(tvwSource, true);
         FillDirectoryTree(tvwTargetDir, false);
      }
      /// <summary>
      /// Fill the directory tree for either the Source or 
      /// Target TreeView.
      /// </summary>
      private void FillDirectoryTree(
         TreeView tvw, bool isSource)
      {
         //  Populate tvwSource, the Source treeView, 
         //  with the contents of 
         //  the local hard drive.
         //  First clear all the nodes.
         tvw.Nodes.Clear();

         //  Get the logical drives and put them into the 
         //  root nodes. Fill an array with all the 
         // logical drives on the machine.
         string[] strDrives = 
            Environment.GetLogicalDrives();

         //  Iterate through the drives, adding them to the tree.
         //  Use a try/catch block, so if a drive is not ready, 
         //  e.g. an empty floppy or CD,
         //    it will not be added to the tree.
         foreach (string rootDirectoryName in strDrives)
         {
            if (rootDirectoryName != @"C:\")
               continue;
            try 
            {
                    
               //  Fill an array with all the first level 
               //  subdirectories. If the drive is
               //  not ready, this will throw an exception.
               DirectoryInfo dir = 
                  new DirectoryInfo(rootDirectoryName);
               dir.GetDirectories();
               
               TreeNode ndRoot = new TreeNode(rootDirectoryName);

               //  Add a node for each root directory.
               tvw.Nodes.Add(ndRoot);

               //  Add subdirectory nodes.
               //  If treeview is the source, 
               // then also get the filenames.
               if (isSource)
               {
                  GetSubDirectoryNodes(
                     ndRoot, ndRoot.Text, true);
               }
               else
               {
                  GetSubDirectoryNodes(
                     ndRoot, ndRoot.Text, false);
               }

            }
            catch (Exception e)
            {
               //  Catch any errors such as 
               // Drive not ready.
                MessageBox.Show(e.Message);
            }
         }
      }      //  close for FillSourceDirectoryTree

      /// <summary>
      /// Gets all the subdirecories below the 
      /// passed in directory node.
      /// Adds to the directory tree.
      /// The parameters passed in at the parent node 
      /// for this subdirectory,
      /// the full path name of this subdirectory, 
      /// and a boolean to indicate
      /// whether or not to get the files in the subdirectory.
      /// </summary>
      private void GetSubDirectoryNodes(
         TreeNode parentNode, string fullName, bool getFileNames)
      {
         DirectoryInfo dir = new DirectoryInfo(fullName);
         DirectoryInfo[] dirSubs = dir.GetDirectories();


         //  Add a child node for each subdirectory.
         foreach (DirectoryInfo dirSub in dirSubs)
         {
            // do not show hidden folders
            if ( (dirSub.Attributes & FileAttributes.Hidden) 
               != 0 )
            {
               continue;
            }
                

            /// <summary>
            ///    Each directory contains the full path.
            ///    We need to split it on the backslashes, 
            ///    and only use
            ///    the last node in the tree.
            ///    Need to double the backslash since it 
            ///    is normally 
            ///    an escape character
            /// </summary>

            TreeNode subNode = new TreeNode(dirSub.Name);
            parentNode.Nodes.Add(subNode);
            
            //  Call GetSubDirectoryNodes recursively.
            GetSubDirectoryNodes(
               subNode,dirSub.FullName,getFileNames);
         }

         if (getFileNames)
         {
            //  Get any files for this node.
            FileInfo[] files = dir.GetFiles();

            // After placing the nodes, 
            // now place the files in that subdirectory.
            foreach (FileInfo file in files)
            {
               TreeNode fileNode = new TreeNode(file.Name);
               parentNode.Nodes.Add(fileNode);
            }
         }
      }

		/// <summary>
		/// Clean up any resources being used.
		/// </summary>
		public override void Dispose()
		{
			base.Dispose();
			if(components != null)
				components.Dispose();
		}

		#region Windows Form Designer generated code
		/// <summary>
		/// Required method for Designer support - do not modify
		/// the contents of this method with the code editor.
		/// </summary>
		private void InitializeComponent()
		{
         this.txtTargetDir = new System.Windows.Forms.TextBox();
         this.label1 = new System.Windows.Forms.Label();
         this.label2 = new System.Windows.Forms.Label();
         this.tvwSource = new System.Windows.Forms.TreeView();
         this.tvwTargetDir = new System.Windows.Forms.TreeView();
         this.btnClear = new System.Windows.Forms.Button();
         this.chkOverwrite = new System.Windows.Forms.CheckBox();
         this.btnCancel = new System.Windows.Forms.Button();
         this.btnCopy = new System.Windows.Forms.Button();
         this.lblStatus = new System.Windows.Forms.Label();
         this.btnDelete = new System.Windows.Forms.Button();
         this.SuspendLayout();
         // 
         // txtTargetDir
         // 
         this.txtTargetDir.Location = new System.Drawing.Point(304, 48);
         this.txtTargetDir.Name = "txtTargetDir";
         this.txtTargetDir.Size = new System.Drawing.Size(272, 20);
         this.txtTargetDir.TabIndex = 6;
         this.txtTargetDir.Text = "textBox1";
         // 
         // label1
         // 
         this.label1.Font = new System.Drawing.Font(
             "Microsoft Sans Serif", 12F, (System.Drawing.FontStyle.Bold | 
             System.Drawing.FontStyle.Underline), 
             System.Drawing.GraphicsUnit.Point, 
             ((System.Byte)(0)));
         this.label1.Location = new System.Drawing.Point(16, 24);
         this.label1.Name = "label1";
         this.label1.Size = new System.Drawing.Size(216, 24);
         this.label1.TabIndex = 14;
         this.label1.Text = "Source files:";
         // 
         // label2
         // 
         this.label2.Font = new System.Drawing.Font("Microsoft Sans Serif", 12F, 
            (System.Drawing.FontStyle.Bold | System.Drawing.FontStyle.Underline), 
            System.Drawing.GraphicsUnit.Point, ((System.Byte)(0)));
         this.label2.Location = new System.Drawing.Point(304, 24);
         this.label2.Name = "label2";
         this.label2.Size = new System.Drawing.Size(192, 24);
         this.label2.TabIndex = 15;
         this.label2.Text = "Target directory:";
         // 
         // tvwSource
         // 
         this.tvwSource.CheckBoxes = true;
         this.tvwSource.ImageIndex = -1;
         this.tvwSource.Location = new System.Drawing.Point(16, 48);
         this.tvwSource.Name = "tvwSource";
         this.tvwSource.SelectedImageIndex = -1;
         this.tvwSource.Size = new System.Drawing.Size(272, 344);
         this.tvwSource.TabIndex = 1;
         this.tvwSource.AfterCheck += 
            new System.Windows.Forms.TreeViewEventHandler(
            this.tvwSource_AfterCheck);
         // 
         // tvwTargetDir
         // 
         this.tvwTargetDir.ImageIndex = -1;
         this.tvwTargetDir.Location = new System.Drawing.Point(304, 72);
         this.tvwTargetDir.Name = "tvwTargetDir";
         this.tvwTargetDir.SelectedImageIndex = -1;
         this.tvwTargetDir.Size = new System.Drawing.Size(272, 320);
         this.tvwTargetDir.TabIndex = 11;
         this.tvwTargetDir.AfterSelect += new System.Windows.Forms.
            TreeViewEventHandler(this.tvwTargetDir_AfterSelect);
         // 
         // btnClear
         // 
         this.btnClear.Location = new System.Drawing.Point(104, 400);
         this.btnClear.Name = "btnClear";
         this.btnClear.Size = new System.Drawing.Size(88, 24);
         this.btnClear.TabIndex = 10;
         this.btnClear.Text = "C&lear";
         this.btnClear.Click += new System.EventHandler(this.btnClear_Click);
         // 
         // chkOverwrite
         // 
         this.chkOverwrite.Location = new System.Drawing.Point(320, 400);
         this.chkOverwrite.Name = "chkOverwrite";
         this.chkOverwrite.Size = new System.Drawing.Size(120, 24);
         this.chkOverwrite.TabIndex = 12;
         this.chkOverwrite.Text = "Overwrite if exists";
         // 

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -