Advertisement
Guest User

Untitled

a guest
May 27th, 2015
354
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 8.64 KB | None | 0 0
  1.  
  2. using Microsoft.VisualBasic;
  3. using System;
  4. using System.Collections;
  5. using System.Collections.Generic;
  6. using System.Data;
  7. using System.Diagnostics;
  8.  
  9. public sealed class Form1
  10. {
  11.   /// <summary>
  12.   /// Handles the <see cref="FileSplitter"/> instance.
  13.   /// </summary>
  14.   private FileSplitter withEventsField_splitter = new FileSplitter { BufferSize = .BufferSize };
  15.  
  16.   private FileSplitter splitter {
  17.     get { return withEventsField_splitter; }
  18.     set {
  19.       if (withEventsField_splitter != null) {
  20.         withEventsField_splitter.SplitProgressChanged -= Splitter_SplitProgressChangedArgs;
  21.         withEventsField_splitter.MergeProgressChanged -= Splitter_MergeProgressChangedArgs;
  22.       }
  23.       withEventsField_splitter = value;
  24.       if (withEventsField_splitter != null) {
  25.         withEventsField_splitter.SplitProgressChanged += Splitter_SplitProgressChangedArgs;
  26.         withEventsField_splitter.MergeProgressChanged += Splitter_MergeProgressChangedArgs;
  27.       }
  28.     }
  29.   }
  30.  
  31.   // Splitter settings.
  32.   private readonly string workingDir = "C:\\Test";
  33.   private readonly string fileName = "File.mp3";
  34.   private readonly string filePath = IO.Path.Combine(workingDir, fileName);
  35.   private readonly string chunkName = "File.Part";
  36.   private readonly string chunkExt = "fs";
  37.  
  38.   // Merger settings.
  39.   private readonly string firstChunkPath = string.Format("{0}\\{1}.01.{2}", workingDir, chunkName, chunkExt);
  40.   private readonly string targetFileName = string.Format("{0}\\{1}.{2}", workingDir, "Merged", IO.Path.GetExtension(fileName));
  41.  
  42.   // Some default chunk sizes to split a file.
  43.   private readonly int kilobyte = 1024;
  44.   private readonly int megabyte = 1048576;
  45.   private readonly int gigabyte = 1073741824;
  46.   private readonly int halfFileSize = Convert.ToInt32(new IO.FileInfo(filePath).Length / 2);
  47.  
  48.   // The label controls that will display the progress.
  49.   private Label labelSplit1 = new Label();
  50.   private Label labelSplit2 = new Label();
  51.   private Label labelSplit3 = new Label();
  52.   private Label labelMerge1 = new Label();
  53.   private Label labelMerge2 = new Label();
  54.   private Label labelMerge3 = new Label();
  55.  
  56.   // The button controls to start a split or merge operation.
  57.   private Button withEventsField_buttonSplit;
  58.   private Button buttonSplit {
  59.     get { return withEventsField_buttonSplit; }
  60.     set {
  61.       if (withEventsField_buttonSplit != null) {
  62.         withEventsField_buttonSplit.Click -= ButtonSplit_Click;
  63.       }
  64.       withEventsField_buttonSplit = value;
  65.       if (withEventsField_buttonSplit != null) {
  66.         withEventsField_buttonSplit.Click += ButtonSplit_Click;
  67.       }
  68.     }
  69.   }
  70.  
  71.   private Button withEventsField_buttonMerge = new Button();
  72.   private Button buttonMerge {
  73.     get { return withEventsField_buttonMerge; }
  74.     set {
  75.       if (withEventsField_buttonMerge != null) {
  76.         withEventsField_buttonMerge.Click -= ButtonMerge_Click;
  77.       }
  78.       withEventsField_buttonMerge = value;
  79.       if (withEventsField_buttonMerge != null) {
  80.         withEventsField_buttonMerge.Click += ButtonMerge_Click;
  81.       }
  82.     }
  83.   }
  84.  
  85.   /// <summary>
  86.   /// Initializes a new instance of the <see cref="Form1"/> class.
  87.   /// </summary>
  88.   public Form1()
  89.   {
  90.     // This call is required by the designer.
  91.     this.InitializeComponent();
  92.  
  93.     // Set the controls properties.
  94.     buttonSplit.Text = "Split";
  95.     buttonSplit.Font = new Font(this.Font.FontFamily, 14f, FontStyle.Bold);
  96.     buttonSplit.Size = new Size(200, 75);
  97.     buttonSplit.Location = new Point(0, 0);
  98.     buttonSplit.Cursor = Cursors.Hand;
  99.  
  100.     buttonMerge.Text = "Merge";
  101.     buttonMerge.Font = new Font(this.Font.FontFamily, 14f, FontStyle.Bold);
  102.     buttonMerge.Size = new Size(200, 75);
  103.     buttonMerge.Location = new Point(buttonSplit.Location.X + buttonSplit.Width, 0);
  104.     buttonMerge.Cursor = Cursors.Hand;
  105.  
  106.     labelSplit1.Text = "Total Progress:";
  107.     labelSplit1.AutoSize = true;
  108.     labelSplit1.Font = new Font(this.Font.FontFamily, 9f, FontStyle.Regular);
  109.     labelSplit1.Location = new Point(0, buttonSplit.Location.Y + buttonSplit.Height + 10);
  110.  
  111.     labelSplit2.Text = "Chunk Progress:";
  112.     labelSplit2.AutoSize = true;
  113.     labelSplit2.Font = new Font(this.Font.FontFamily, 9f, FontStyle.Regular);
  114.     labelSplit2.Location = new Point(0, labelSplit1.Location.Y + labelSplit1.Height);
  115.  
  116.     labelSplit3.Text = "Chunk Count:";
  117.     labelSplit3.AutoSize = true;
  118.     labelSplit3.Font = new Font(this.Font.FontFamily, 9f, FontStyle.Regular);
  119.     labelSplit3.Location = new Point(0, labelSplit2.Location.Y + labelSplit2.Height);
  120.  
  121.     labelMerge1.Text = "Total Progress:";
  122.     labelMerge1.AutoSize = true;
  123.     labelMerge1.Font = new Font(this.Font.FontFamily, 9f, FontStyle.Regular);
  124.     labelMerge1.Location = new Point(buttonMerge.Location.X, buttonMerge.Location.Y + buttonMerge.Height + 10);
  125.  
  126.     labelMerge2.Text = "Chunk Progress:";
  127.     labelMerge2.AutoSize = true;
  128.     labelMerge2.Font = new Font(this.Font.FontFamily, 9f, FontStyle.Regular);
  129.     labelMerge2.Location = new Point(buttonMerge.Location.X, labelMerge1.Location.Y + labelMerge1.Height);
  130.  
  131.     labelMerge3.Text = "Chunk Count:";
  132.     labelMerge3.AutoSize = true;
  133.     labelMerge3.Font = new Font(this.Font.FontFamily, 9f, FontStyle.Regular);
  134.     labelMerge3.Location = new Point(buttonMerge.Location.X, labelMerge2.Location.Y + labelMerge2.Height);
  135.  
  136.     // Add the controls on the UI.
  137.     this.Controls.AddRange({
  138.       labelSplit1, labelSplit2, labelSplit3,
  139.       labelMerge1, labelMerge2, labelMerge3,
  140.       buttonSplit, buttonMerge
  141.     })
  142.  
  143.     // Set the Form properties.
  144.     this.Size = new Size(buttonSplit.Width + buttonMerge.Width + 20, 200);
  145.     this.FormBorderStyle = Windows.Forms.FormBorderStyle.FixedDialog;
  146.     this.MaximizeBox = false;
  147.   }
  148.  
  149.   /// <summary>
  150.   /// Handles the <see cref="Button.Click"/> event of the <see cref="ButtonSplit"/> control.
  151.   /// </summary>
  152.   private void ButtonSplit_Click()
  153.   {
  154.     this.splitter.Split(sourceFile: this.filePath,
  155.                         chunkSize: this.halfFileSize,
  156.                         chunkName: this.chunkName,
  157.                         chunkExt: this.chunkExt,
  158.                         overwrite: true,
  159.                         deleteAfterSplit: false);
  160.  
  161.     // Or...
  162.     //Me.splitter.Split(sourceFile:=Me.filePath,
  163.     //                  chunkCount:=2,
  164.     //                  chunkName:=Me.chunkName,
  165.     //                  chunkExt:=Me.chunkExt,
  166.     //                  overwrite:=True,
  167.     //                  deleteAfterSplit:=False)
  168.   }
  169.  
  170.   /// <summary>
  171.   /// Handles the <see cref="Button.Click"/> event of the <see cref="ButtonMerge"/> control.
  172.   /// </summary>
  173.   private void ButtonMerge_Click()
  174.   {
  175.     this.splitter.Merge(sourceChunk: this.firstChunkPath,
  176.                         targetFile: this.targetFileName,
  177.                         overwrite: true,
  178.                         deleteChunksAfterMerged: false);
  179.   }
  180.  
  181.   /// <summary>
  182.   /// Handles the <see cref="FileSplitter.SplitProgressChangedArgs"/> event of the <see cref="Splitter"/> instance.
  183.   /// </summary>
  184.   /// <param name="sender">The source of the event.</param>
  185.   /// <param name="e">The <see cref="FileSplitter.SplitProgressChangedArgs"/> instance containing the event data.</param>
  186.   private void Splitter_SplitProgressChangedArgs(object sender, FileSplitter.SplitProgressChangedArgs e)
  187.   {
  188.     labelSplit1.Text = string.Format("Total Progress: {0}%", e.TotalProgress.ToString("n1"));
  189.     labelSplit2.Text = string.Format("Chunk Progress: {0}%", e.ChunkProgress.ToString("n1"));
  190.     labelSplit3.Text = string.Format("Current  Chunk: {0} of {1}", Convert.ToString(e.ChunksCreated + 1), Convert.ToString(e.ChunksToCreate));
  191.     Application.DoEvents();
  192.   }
  193.  
  194.   /// <summary>
  195.   /// Handles the <see cref="FileSplitter.MergeProgressChangedArgs"/> event of the <see cref="Splitter"/> instance.
  196.   /// </summary>
  197.   /// <param name="sender">The source of the event.</param>
  198.   /// <param name="e">The <see cref="FileSplitter.MergeProgressChangedArgs"/> instance containing the event data.</param>
  199.   private void Splitter_MergeProgressChangedArgs(object sender, FileSplitter.MergeProgressChangedArgs e)
  200.   {
  201.     labelMerge1.Text = string.Format("Total Progress: {0}%", e.TotalProgress.ToString("n1"));
  202.     labelMerge2.Text = string.Format("Chunk Progress: {0}%", e.ChunkProgress.ToString("n1"));
  203.     labelMerge3.Text = string.Format("Current  Chunk: {0} of {1}", Convert.ToString(e.ChunksMerged + 1), Convert.ToString(e.ChunksToMerge));
  204.     Application.DoEvents();
  205.   }
  206.  
  207. }
  208.  
  209. //=======================================================
  210. //Service provided by Telerik (www.telerik.com)
  211. //=======================================================
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement