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

📄 ex-13-03

📁 Programming Csharp Source Code(代码) Programming Csharp Source Code
💻
📖 第 1 页 / 共 2 页
字号:
         // btnCancel
         // 
         this.btnCancel.Location = new System.Drawing.Point(464, 480);
         this.btnCancel.Name = "btnCancel";
         this.btnCancel.Size = new System.Drawing.Size(112, 32);
         this.btnCancel.TabIndex = 10;
         this.btnCancel.Text = "&Cancel";
         this.btnCancel.Click += new System.EventHandler(this.btnCancel_Click);
         // 
         // btnCopy
         // 
         this.btnCopy.Location = new System.Drawing.Point(464, 400);
         this.btnCopy.Name = "btnCopy";
         this.btnCopy.Size = new System.Drawing.Size(112, 32);
         this.btnCopy.TabIndex = 7;
         this.btnCopy.Text = "&Copy";
         this.btnCopy.Click += new System.EventHandler(this.btnCopy_Click);
         // 
         // lblStatus
         // 
         this.lblStatus.Location = new System.Drawing.Point(24, 456);
         this.lblStatus.Name = "lblStatus";
         this.lblStatus.Size = new System.Drawing.Size(424, 48);
         this.lblStatus.TabIndex = 13;
         // 
         // btnDelete
         // 
         this.btnDelete.Location = new System.Drawing.Point(464, 440);
         this.btnDelete.Name = "btnDelete";
         this.btnDelete.Size = new System.Drawing.Size(112, 32);
         this.btnDelete.TabIndex = 8;
         this.btnDelete.Text = "&Delete";
         this.btnDelete.Click += new System.EventHandler(this.btnDelete_Click);
         // 
         // Form1
         // 
         this.AutoScaleBaseSize = new System.Drawing.Size(5, 13);
         this.ClientSize = new System.Drawing.Size(592, 525);
         this.Controls.AddRange(new System.Windows.Forms.Control[] 
            {this.label2,
            this.label1,
            this.btnCopy,
            this.btnDelete,
            this.btnCancel,
            this.btnClear,
            this.chkOverwrite,
            this.lblStatus,
            this.txtTargetDir,
            this.tvwTargetDir,
            this.tvwSource});
         this.Name = "Form1";
         this.Text = "File Copier";
         this.ResumeLayout(false);

      }
		#endregion

		/// <summary>
		/// The main entry point for the application.
		/// </summary>
		[STAThread]
		static void Main() 
		{
			Application.Run(new Form1());
		}

 

      /// <summary>
      ///    Create an ordered list of all 
      ///    the selected files, copy to the
      ///    target directory
      /// </summary>
      private void btnCopy_Click(object sender, 
         System.EventArgs e)
      {
         // get the list
         ArrayList fileList = GetFileList();

         // copy the files
         foreach (FileInfo file in fileList)
         { 
            try
            {
               // update the label to show progress
               lblStatus.Text = "Copying " + txtTargetDir.Text + 
                  "\\" + file.Name + "...";
               Application.DoEvents();

               // copy the file to its desitnation location
               file.CopyTo(txtTargetDir.Text + "\\" + 
                  file.Name,chkOverwrite.Checked);
            }
               
            catch // (Exception ex)
            {
               // you may want to do more than just show the message
               // MessageBox.Show(ex.Message);
            }
         }
         lblStatus.Text = "Done.";
         Application.DoEvents();

      }

      /// <summary>
      ///    on cancel,  exit
      /// </summary>
      private void btnCancel_Click(object sender, System.EventArgs e)
      {
         Application.Exit();
      }

      /// <summary>
      ///    Tell the root of each tree to uncheck
      ///    all the nodes below
      /// </summary>
      private void btnClear_Click(object sender, System.EventArgs e)
      {
         // get the top most node for each drive
         // and tell it to clear recursively
         foreach (TreeNode node in tvwSource.Nodes)
         {
            SetCheck(node, false);
         }

      }
      
      /// <summary>
      ///    check that the user does want to delete
      ///    Make a list and delete each in turn
      /// </summary>
      private void btnDelete_Click(object sender, System.EventArgs e)
      {
         // ask them if they are sure
         System.Windows.Forms.DialogResult result = 
            MessageBox.Show(
            "Are you quite sure?",  // msg
            "Delete Files",         // caption
            MessageBoxButtons.OKCancel,  // buttons
            MessageBoxIcon.Exclamation,  // icons
            MessageBoxDefaultButton.Button2);  // default button

         // if they are sure...
         if (result == System.Windows.Forms.DialogResult.OK)
         {
            // iterate through the list and delete them.
            // get the list of selected files
            ArrayList fileNames = GetFileList();

            foreach (FileInfo file in fileNames)
            { 
               try
               {
                  // update the label to show progress
                  lblStatus.Text = "Deleting " + 
                     txtTargetDir.Text + "\\" + 
                     file.Name + "...";
                  Application.DoEvents();

                  // Danger Will Robinson!
                  file.Delete();

               }
                   
               catch (Exception ex)
               {
                  // you may want to do more than 
                  // just show the message
                  MessageBox.Show(ex.Message);
               }
            }
            lblStatus.Text = "Done.";
            Application.DoEvents();
         }
      }



      /// <summary>
      ///    Get the full path of the chosen directory
      ///    copy it to txtTargetDir
      /// </summary>
      private void tvwTargetDir_AfterSelect(
         object sender, 
         System.Windows.Forms.TreeViewEventArgs e)
      {
         // get the full path for the selected directory
         string theFullPath = GetParentString(e.Node);
            
         // if it is not a leaf, it will end with a back slash
         // remove the backslash
         if (theFullPath.EndsWith("\\"))
         {
            theFullPath = 
               theFullPath.Substring(0,theFullPath.Length-1);
         }
         // insert the path in the text box
         txtTargetDir.Text = theFullPath; 
      }

   
      /// <summary>
      ///    Mark each node below the current
      ///    one with the current value of checked
      /// </summary>
      private void tvwSource_AfterCheck(object sender, 
         System.Windows.Forms.TreeViewEventArgs e)
      {
         // Call a recursible method.
         // e.node is the node which was checked by the user.
         // The state of the check mark is already 
         // changed by the time you get here.
         // Therefore, we want to pass along 
         // the state of e.node.Checked.
         SetCheck(e.Node,e.Node.Checked);

      }
   

      /// <summary>
      ///    recursively set or clear check marks
      /// </summary> 
      private void SetCheck(TreeNode node, bool check)
      {
         // find all the child nodes from this node
         foreach (TreeNode n in node.Nodes)
         {
            n.Checked = check;   // check the node

            // if this is a node in the tree, recurse
            if (n.Nodes.Count != 0) 
            {
               SetCheck(n,check);
            }
         }   
      }

      /// <summary>
      ///    Given a node and an array list
      ///    fill the list with the names of
      ///    all the checked files
      /// </summary>
      // Fill the ArrayList with the full paths of 
      // all the fiels checked
      private void GetCheckedFiles(TreeNode node, 
         ArrayList fileNames)
      {
         // if this is a leaf...
         if (node.Nodes.Count == 0)
         {
            // if the node was checked...
            if (node.Checked)
            {
               // get the full path and add it to the arrayList
               string fullPath = GetParentString(node);
               fileNames.Add(fullPath);
            }
         }
         else  // if this node is not a leaf
         {
            // call this for all the subnodes
            foreach (TreeNode n in node.Nodes)
            {
               GetCheckedFiles(n,fileNames);
            }
         }
      }

      /// <summary>
      ///    Given a node, return the
      ///    full path name
      /// </summary>
      private string GetParentString(TreeNode node)
      {
         // if this is the root node (c:\) return the text
         if(node.Parent == null)
         {
            return node.Text;
         }
         else
         {
            // recurse up and get the path then 
            // add this node and a slash
            // if this node is the leaf, don't add the slash
            return GetParentString(node.Parent) + node.Text +  
               (node.Nodes.Count == 0  ? "" : "\\");
         }
      }
      

      /// <summary>
      ///    shared by delete and copy
      ///    creates an ordered list of all 
      ///    the selected files
      /// </summary>
      private ArrayList GetFileList()
      {
         // create an unsorted array list of the full file names
         ArrayList fileNames = new ArrayList();

         // fill the fileNames ArrayList with the 
         // full path of each file to copy
         foreach (TreeNode theNode in tvwSource.Nodes)
         {
            GetCheckedFiles(theNode, fileNames);
         }

         // Create a list to hold the fileInfo objects
         ArrayList fileList = new ArrayList();

         // for each of the file names we have in our unsorted list
         // if the name corresponds to a file (and not a directory)
         // add it to the file list
         foreach (string fileName in fileNames)
         {
            // create a file with the name 
            FileInfo file = new FileInfo(fileName);

            // see if it exists on the disk
            // this fails if it was a directory
            if (file.Exists)
            {
               // both the key and the value are the file
               // would it be easier to have an empty value?
               fileList.Add(file);
            }
         }

         // Create an instance of the IComparer interface
         IComparer comparer = (IComparer) new FileComparer();

         // pass the comparer to the sort method so that the list
         // is sorted by the compare method of comparer.
         fileList.Sort(comparer); 

         return fileList;
      }


	}
}

⌨️ 快捷键说明

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