Tag Archives: C#.Net

Using C#.Net Create Zip File

From C# code, files can be easily compressed in Zip format by getting the advantage of  Zip functionality available in J#. First of all we have to add reference of the J# .NET library in our project. Physically, it resides as a file named vjslib.dll

Add Reference vjslib (J# .Net Library):

In Solution Explorer Right click your project on References and click on “Add Reference” -> select the .NET tab -> scroll down and select “vjslib” -> click OK.
After adding reference, the Java library classes could be refered  within your application.

C# Code:

using java.util;
using java.util.zip;
using java.io;

 private void CreateZipFile(string DestinationZipFileName, string SourceFileToZip)

 FileOutputStream fileOutStream;
 fileOutStream = new FileOutputStream(DestinationZipFileName);
 ZipOutputStream zipOutStream ;
 zipOutStream = new ZipOutputStream(fileOutStream);

 FileInputStream fileInStream ;
 fileInStream = new FileInputStream(SourceFileToZip);

 ZipEntry zipEntry;
 zipEntry = new ZipEntry(Path.GetFileName(SourceFileToZip));


 sbyte[] buffer = new sbyte[1024];
 int len = 0;
 while ((len = fileInStream.read(buffer)) >= 0)
 zipOutStream.write(buffer, 0, len);



I hope this simple example will help you a lot.

Add Custom Key tag in web.config / aap.config appSettings

Very often it is required to add custom tags or key in app.config or in web.config. Here is a simple way to add key in <appSettings> and then access it.

Find section <appSettings> or <appSettings/> in .config file and add your required keys and values, the following example is same for both  .config files. here we go

An appSettings element in the root Web.config / App.config file that looks like the following:


<add key=myKeyNamevalue=my custom setting value/>


The <appSettings> element is a direct child of the <configuration> element and a peer of the system.web element.

Values read from the appSettings element of the .config file are always of type String. If the specified key does not exist in the .config file, no error occurs. Instead, an empty string is returned

C# Code:

using System.Collections.Specialized;

using System.Configuration;

string strKeysAndValues = "";

// get all keys and values in AppSettings element

NameValueCollection sAll = ConfigurationManager.AppSettings;

foreach (string s in sAll.AllKeys)


 strKeysAndValues += "Key" + s + " value:"+ sAll.Get(s)+" ";



string strKeysAndValues= ConfigurationSettings.AppSettings["myKeyName"].ToString();

World’s Ever Simplest Example to Understand Thread Pool

1. Step

//creating class that contain method which would be used for threads start method

class ContaingThreadStartMethod{

private ManualResetEvent _doneEvent;

public ContaingThreadStartMethod(ManualResetEvent doneEvent)


_doneEvent = doneEvent;


public void StartMethod(object obj){


int i = (int)obj;


catch (Exception ex)


string error_msg = ex.message;








2. Step

//Creating threads and

//maintain thread pool

ThreadCount = 20; // threads to be initiated

int number = 1; // object to be passed for manupulation

ManualResetEvent[] doneEvents = new ManualResetEvent[ThreadCount];

for (int i = 0; i &lt; ThreadCount; i++)


doneEvents[i] = new ManualResetEvent(false);

ContaingThreadStartMethod ctsm = new ContaingThreadStartMethod(doneEvents[i]);

ThreadPool.QueueUserWorkItem(ctsm.StartMethod, number);


// Wait for all threads in pool to calculation…