Text File Splitter - C#.NET Custom Events

I recently received a a large text file (many megabytes) which was a concatenation of many different smaller files.  The smaller files where of different size and the separated by a particular character. I did a quick Google search, but almost all of the file splitting applications available split files based on size.

I needed to split a file based on a certain ASCII character.

image

Written using C#, my text file splitting app is extremely simple.

Step 1:

Read the entire text file into a string variable

TextReader tr = new StreamReader(filePath); string file = tr.ReadToEnd();

Step 2:

Using the string function 'Split', create a string array.

string[] files = file.Split(splitChar);

Step 3:

Iterate through the string array, and write each to a separate file.

foreach (string file in files) { if (file.Length != 0) { if (CreateFile(file, string.Format("{0}\\output_{1}.{2}", outputDir, _writtenCount.ToString(), fileExt))) { _writtenCount++; } else { _errorCount++; } } else { _errorCount++; } }

That's it!!!

While there is some beauty in simplistic design, the differentiating aspect of this application is the status screen.

image

All of the file reading\splitting\writing is contained within the class 'Splitter'.  I have tried to keep it independent of the application so that
it could be re-used within other applications if needed.  Thus it cannot know about the application, it's forms or controls.

However, I do want my applications (that use this class) to provide the user some feedback on how the splitting process is proceeding.  I have achieved this by raising 'events' within the splitter class.

 

First of all the event delegate's need to be defined, they are defined globally but still within the 'splitter' class.

public delegate void FileProcessedHandler(object sender); public delegate void FileWrittenHandler(object sender); public delegate void FileCompleteHandler(object sender);

Then the events need to be declared within the class.

public event FileProcessedHandler FileProcessedEvent; public event FileWrittenHandler FileWrittenEvent; public event FileCompleteHandler FileCompleteEvent;

Then the events can be raised within the code of the class.

public void ProcessFile(string filename, char splitCharacter, string outputDirectory, string outputExtension) { string file = ReadFile(filename); string[] splitFiles = SplitFile(file, splitCharacter.ToString().ToCharArray()); _fileCount = splitFiles.Length; FileProcessedEvent(this); SaveFiles(splitFiles, outputDirectory, outputExtension); FileCompleteEvent(this); }

If you would like to download the application, or the full source code, please go to the Text File Splitter Project.

Trackback URL for this post:

http://ontheperiphery.veraida.com/trackback/35

Casino 1273770080

Casino 1273770080

Comments

GrEEtIngS!

siti sicuri per acquistare viagra esenzione viagra ordinare levitra comprare online stati uniti dazio comprare il cialis in linea acquisto di letto in italia comprare viagra spedito dall italia fentermina vendita on line ordinare cialis compro viagra al prezzo migliore comprare levitra generico in italia disfunzione erettile durante masturbazione ordinare il cialis online ingrosso farmaci online vendita viagra kamagra levitra levitra a prix discount ordinare kamagra in maniera sicura viagra toulouse cialis acquistare disfunzione erettile +testimonianze lechar!