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