Click here to Skip to main content
15,879,474 members
Articles / Programming Languages / C#
Article

File Splitter Utility in C# - WinForms

Rate me:
Please Sign up or sign in to vote.
4.53/5 (19 votes)
25 Sep 20033 min read 109.1K   6.2K   47   17
Simple File Splitter / Joiner utility that demonstrates FCL and Winforms UI processing

Sample Image - FileSplitter.jpg

Introduction

This project is a simple file splitter / joiner application.  It's comprised of a simple UI that interfaces with a simple controller class. The controller class is responsible for coordinating the requests from the UI, initiation of the 2 worker classes (FileSplitter & FileJoiner), and deal with updating the UI with the progress of the activity.

The project introduces some nice features of the Streams present in the .NET Base Classes. Stuff that would be lots more code in C++ can be cut down considerably.  It also provides for tying events & delegates between the tiers (UI, Controller, Processing).

Background

I originally created the worker classes in order to split up large MPEG files so they could fit onto 600 MB CD's.  Routinely, however, I find a need to split files when transfering files over the internet using FTP, email, etc.  While utilities such as WinRar do this for you, in the end you have to pay for the software for a simple task.  They do add zip capabilities to the mix, but MPEG files, and existing Zip files are already zipped.  So, I just needed a splitter/joiner tool and here it is.

Using the code

FileSplitter / File Joiner Classes

These classes expose a set of simple methods and an event called ProgressNotify.  This event will just provide the percent of completion of the task in process.  There's further control capabilities for tuning of the size of the internal buffer used along with the size of the file segments.  Not all properties of the class are utilized by the UI.  It also just uses a simple filename mechanism for the output files.  If the input file is called Test.zip, the segments are called Test.zip.0, Test.zip.1, etc.

FileSplitter has one real method that just takes a filename to split and uses the file naming convention.

FileJoiner has a bunch of extra methods for joining files, but only 1 is used by the UI. 

In the end, FileJoiner does the equivalent of a DOS command "copy /b file1 + file2 + file3 finalfile ".

Controller

Within the UI project, there's a controller class that is responsible for accepting the requests from the UI and spawning, using the ThreadPool, a worker thread. 

The constructor for Controller accepts the form and a delegate on that form that will process the progress updates.  The form has a handler as well that just updates the progress bar within the status bar using OwnerDraw.

C#
public Controller( Form owner, Delegate senderDelegate)
{
    m_sender = owner;
    m_senderDelegate = senderDelegate;
}

It then registers for the FileSplitter/FileJoiner using it's NotifyHandler delegate to accept the events.

C#
fs.ProgressNotify += new ProgressNotifyHandler(NotifyHandler);

NotifyDelegate then in turn, uses the following method to asynchronously update the UI with the progress of the task.

C#
private void NotifyHandler( int i ) { m_sender.BeginInvoke( 
      m_senderDelegate, new object[] { i } ); }

MainForm

The main form is simple.  Double clicking on the File Name text field just brings up a file selection dialog.  Choosing a file, then either Megabytes or Kilobytes, then click "Split File". 

When joining files, if the file chosen doesn't end in a '0' (indicating it's the 1st in the series) you'll get an exception.  Also, it's only smart enough to join files in sequence numerically.  If there's file missing on the join, it will continue without fail - although this is probably not the correct behavior a simple change to FileJoiner could deal with that.

The other interesting thing is having a progress bar appear on the status bar within a panel.

That is accomplished using the following - 1st add the progress bar to the controls collection

C#
progressBar1 = new ProgressBar();
this.Controls.Add(progressBar1); //not really sure if I need this!!!
this.statusBar.Controls.Add( this.progressBar1 );

Then, there's adding a method for the statusbar's OwnerDraw event and wiring it up.

The wireup:

C#
this.statusBar.DrawItem += 
  new System.Windows.Forms.StatusBarDrawItemEventHandler(
    this.statusBar_DrawItem);        

The method to draw:

C#
private void statusBar_DrawItem(object sender, 
  System.Windows.Forms.StatusBarDrawItemEventArgs sbdevent)
{
    if ( sbdevent.Panel == this.statusBarPanel1 )
    {
        this.progressBar1.Bounds = sbdevent.Bounds;
    }
} 

History

  • 2003-09-25 1st Version

 

License

This article has no explicit license attached to it but may contain usage terms in the article text or the download files themselves. If in doubt please contact the author via the discussion board below.

A list of licenses authors might use can be found here


Written By
Web Developer
United States United States
I have over 15 years of experience in the Application Development. Unfortunately, I still remember punch cards from College and VisiCalc on the Apple II.

My recent experience (about 6 years) covers the 2 main camps in distributed computing: J2EE based and COM[+] / .NET.

Lately, it's been deep .NET, C#, ASP.NET and the rest of the .NET Framework.

I've been working on Internet related technologies since 1993, initially writing Perl scripts under the original NCSA Http server.

Comments and Discussions

 
NewsI extend this to a completely new thing [modified] Pin
fixfify24-Aug-11 1:39
fixfify24-Aug-11 1:39 
GeneralExcellent Job Pin
wakhtar6-Jan-10 10:38
wakhtar6-Jan-10 10:38 
QuestionHow to add splitter bar in iframe ? Pin
mithilesh swarge27-Mar-08 1:01
mithilesh swarge27-Mar-08 1:01 
AnswerRe: How to add splitter bar in iframe ? Pin
fixfify24-Aug-11 1:40
fixfify24-Aug-11 1:40 
GeneralProblem with big files Pin
canozurdo11-Jan-08 3:39
canozurdo11-Jan-08 3:39 
QuestionCan i use this DLL in my Software? [modified] Pin
Ronakkumar Patel9-May-07 7:25
Ronakkumar Patel9-May-07 7:25 
QuestionSplitter and Join does not work for Zip files Pin
Thowfiq Raja18-Apr-07 19:59
Thowfiq Raja18-Apr-07 19:59 
GeneralBrowser plugin Pin
subraok12319-Jul-06 18:17
subraok12319-Jul-06 18:17 
GeneralRe: Browser plugin Pin
Shawn Cicoria20-Jul-06 3:03
Shawn Cicoria20-Jul-06 3:03 
GeneralNice Utility. Pin
PSK_3-Sep-05 4:30
PSK_3-Sep-05 4:30 
GeneralGreat Job! Pin
bneacetp6-Jun-04 22:57
bneacetp6-Jun-04 22:57 
GeneralCool utility but here are some comments... Pin
Scott Seeber7-Oct-03 8:42
Scott Seeber7-Oct-03 8:42 
Question.ZIP Files Corrupted? Pin
C. Augusto Proiete1-Oct-03 16:48
C. Augusto Proiete1-Oct-03 16:48 
AnswerRe: .ZIP Files Corrupted? Pin
Shawn Cicoria2-Oct-03 0:24
Shawn Cicoria2-Oct-03 0:24 
Answersolution: Re: .ZIP Files Corrupted? Pin
iptv19-Sep-06 22:54
iptv19-Sep-06 22:54 
GeneralFile Splitter Questions Pin
Steve Graddy30-Sep-03 3:23
Steve Graddy30-Sep-03 3:23 
GeneralRe: File Splitter Questions Pin
Shawn Cicoria30-Sep-03 3:42
Shawn Cicoria30-Sep-03 3:42 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.