Tuesday 26 July 2011

Measure Execution Time taken by your code.

To measure an execution time taken by your code, just use the below piece of code at appropriate place in your code block.
using System.Diagnostics;

public static void Main(string[] args)
{
Console.WriteLine("In Main");
Stopwatch stopwatch = new Stopwatch();
stopwatch.Start();

//Your Code will go here

stopwatch.Stop();
var timeSpan = stopwatch.Elapsed;
var executionTimeTaken=String.Format("{0:00}:{1:00}:{2:00}.{3:00}",timeSpan.Hours, timeSpan.Minutes, timeSpan.Seconds, timeSpan.Milliseconds / 10);
Console.WriteLine(executionTimeTaken);
}

Thanks,
Md. jawed

Sunday 17 July 2011

J-AXE: A File Splitter in C#

Introduction

 J-AXE file splitter is a windows application developed using C# .Net to split file in to time interval,Based on size and total files after splits. I would not ignore the fact that this is some kind of new application, Already there is so many open source project are available to achieve the same purpose. Then the question is why I have spent lots of time to develop this app: allow me to explain the question: whatever application is available as open and/or paid, you will not get the options to splits the file based on Time interval or duration. But here you will get this functionality along with other functionality with new flavor; and the main point is that I wanted to develop this using C#.Net and by developing this kind of application I will get a chance to learn something new And even I would utilize my free time into something productive .So I did this and here is the UI of this J-AXE file splitter application.
 (http://www.codeproject.com/KB/applications/JAXEFileSplitter.aspx)


                                                  Fig 1. JAXE File Splitter UI.


Might be you will think that why this application name is J-AXE? Actually the letter J came from Jawed and AXE is to cut some wooden block. So, I have chosen this application name as J-AXE.

Background

 Of course there was something which triggers me to come up with this ideas/application. Last month I recorded family videos using my camcorder after that I added few songs and effect in to recorded video to make it movies so that I can distribute this among family’s members. Now I wanted to split this file into durations instead of size so that I can make it into different parts, something like Part1 as 30 minutes and Part2 as45 Minutes. I searched here and there for open source but whatever I got all were having functionality to splits file only by size not by the time interval.

Then this requirement triggered some chemical imagination into my mind to come up with some application to fulfill my requirement and here it is an application “J-AXE: A file Splitter”.
 
Using the code

 J-AXE: A file Splitter a Windows application is very handy in use. The whole solution is divided into 2 Projects .One project would be User interface and Second Project would be nothing but our main logic which is in DLL format. The idea to separate logic from UI and make logic as DLL so, that anybody can use it easily without referring to my UI implementation.

Let me give you some pictorial presentation of the whole scenario (Class Diagram):
 
                                Fig 2. Class Diagram of JAXE File Splitter with JAXE DLL.


First I would like to explain the implementation of UI part. Then, I will explain the Logic part :

PART1: User Interface
The Block diagram would be something like this:

                                   Fig 3. Block Digaram of JAXE File Splitter.


On UI User will get below option to splits the file:
1. Based on Duration (Time duration in sec/min)
2. Size(in Bytes/KB/MB)
3. And Number of files

Based on the option selected we will call the corresponding function to splits the file:

I have already published this application on Code project., for code details and explanation you can visit the below link for the same:

J-AXE: A File Splitter in C# on Code project by Me. Jawed.

~jawed

Saturday 9 July 2011

Move Items from one listbox to other list box

So,here is the piece of code for that:

public static void MoveSelItems(ListBox from, ListBox to)
{
for (var i = 0; i < from.SelectedItems.Count; i++)
{
to.Items.Add(from.SelectedItems[i].ToString());
from.Items.Remove(from.SelectedItems[i]);
}
}
~jawed

Splites files using C#

Here is the code snippet to Put files under AXE to splits the particular file and then place the output files at target location.

Using System.io;
Using System;

//Method to split files
public void SplitFileBasedOnNumberOfFiles(string inputFile, int noOfFiles,string outPutLocation)

{
//Open the file in read mode

var fs = new FileStream(inputFile, FileMode.Open, FileAccess.Read);
//read the inputs for how many files user want after splitting
var numberOfFiles = Convert.ToInt32(noOfFiles);
//get the size of each file after splitting
var sizeOfEachFile = (int)Math.Ceiling((double)fs.Length / numberOfFiles);
//start the for loop to go through num of file and split that
var baseFileName = Path.GetFileNameWithoutExtension(inputFile);
var extension = Path.GetExtension(inputFile);

for (var i = 1; i <= numberOfFiles; i++)
{
//get the base name
//get the extension of the file
//get the out put location with output file name
var outputFile = new FileStream(outPutLocation + "\\" + baseFileName + "_" + i.ToString().PadLeft(5, Convert.ToChar("0")) + extension, FileMode.Create, FileAccess.Write);
var bytesRead = 0;
//array of bytes
var buffer = new byte[sizeOfEachFile];
//read the file bytes by bytes and put into output buffer
if ((bytesRead = fs.Read(buffer, 0, sizeOfEachFile))
outputFile.Write(buffer, 0, bytesRead);
outputFile.Close();
}
fs.Close();
}


Print html or any files from Windows form using C#

Using this code you would be able to print any type of pages.
just copy and paste this code on any button event handler.

private void buttonPrint_Click(object sender, EventArgs e)
{
try
{
Process printProc = new Process();

 printProc.StartInfo.FileName =" yourFileName";
printProc.StartInfo.UseShellExecute = true;
printProc.StartInfo.Verb = "print";
printProc.StartInfo.CreateNoWindow = true;
printProc.StartInfo.WindowStyle = ProcessWindowStyle.Hidden;
printProc.Start();
}
catch (Exception ex)
{
MessageBox.Show(ex.Message, "Jawed Blog say!!", MessageBoxButtons.OK, MessageBoxIcon.Error);
}

}

Thanks,
Md. Jawed