Extract embeded resource in C#

To create embedded resource, Right click on item and set BuildAction as Embeded Resource.

Now extract embedded resource use following code.

public static string ExtractEmbededResource(string resourceName)
{

 //look for the resource name
 resourceName = “.” + resourceName;
 foreach (string currentResource in Assembly.GetExecutingAssembly().GetManifestResourceNames())
 {
 if (currentResource.LastIndexOf(resourceName) != -1)
 {
 string fqnTempFile = System.IO.Path.GetTempFileName();
 string path = System.IO.Path.GetDirectoryName(fqnTempFile);
 string rootName = System.IO.Path.GetFileNameWithoutExtension(fqnTempFile);
 string destFile = path + @”\” + rootName + “.” + System.IO.Path.GetExtension(currentResource);

 System.IO.Stream fs = System.Reflection.Assembly.GetExecutingAssembly().GetManifestResourceStream(currentResource);
 byte[] buff = new byte[fs.Length];
 fs.Read(buff, 0, (int)fs.Length);
 fs.Close();

 System.IO.FileStream destStream = new System.IO.FileStream(destFile,FileMode.Create);
 destStream.Write(buff, 0, buff.Length);
 destStream.Close();
 return destFile;
 }
 }
 return "";
}
Advertisements

Install Window Service Programmatically

First implement the window service and build it. Your output will be create in the debug/release folder as an exe. Now to install window service on machine, do the following –

Download .cs file from following location
http://dl.dropbox.com/u/5969904/Learnings/ServiceInstaller.cs

Write following code to install the service –

string ServiceName = “TestWindowService”;

string ServiceDisplayName = “TestWindowService”;

string ServiceExePath = @”C:\TestWindowService\Debus\TestWindowService.exe”;

ServiceTools.ServiceInstaller.InstallAndStart(ServiceName, ServiceDisplayName, ServiceExePath);

Write following code to UnInstall the service –

string ServiceName = “TestWindowService”;

//Check if serice is installed

if (ServiceTools.ServiceInstaller.ServiceIsInstalled(ServiceName))

{

//Uninstall Service

ServiceTools.ServiceInstaller.Uninstall(ServiceName);

}

Programmatically download file form web using C#

/// <summary>

/// This method downloads a file form internet url.

/// </summary>

void DownloadDotNetFramework()

{

    string OutPutpath = @”C:\DownlodedRtf.rtf”;

    WebClient oWebClient = new WebClient();

    string url = @”http://dl.dropbox.com/u/5969904/How%20to%20use%20the%20Public%20folder.rtf&#8221;;

    //Add callback function which will call after download complete.

    oWebClient.DownloadFileCompleted += new AsyncCompletedEventHandler(oWebClient_DownloadFileCompleted);

    oWebClient.DownloadFileAsync(new Uri(url), OutPutpath);

}

 

/// <summary>

/// Method which will call after completion of download.

/// </summary>

/// <param name=”sender”></param>

/// <param name=”e”></param>

void oWebClient_DownloadFileCompleted(object sender, AsyncCompletedEventArgs e)

{

    MessageBox.Show(“File downloaded.”);

}

Set SWF as screensaver using C#.Net

Here is the link to set swf file as Screen Saver machine on all Windows Operating System including XP, Vista, Window etc.

Click on following link to download exe –

http://dl.dropbox.com/u/5969904/Learnings/ScreenTimeReplacementInstaller.exe

Due to problem in attachment, i am not able to attach the code.

Example of Binary Search in C#

Following is the example of binary search –

/// <summary>

/// Example of Binary Search.

/// </summary>

/// <param name=”Value”></param>

/// <returns></returns>

public int FindTheValuePosition(int Value)

{

    //Sorted Array

    int[] myArray = new int[] { 1, 2, 4, 6, 10, 14, 15, 19, 20, 34, 36, 38, 40, 42, 50, 55, 60, 61, 62, 63, 67, 70, 78, 80, 82, 84, 86, 88, 90, 92, 93, 99, 101, 105, 107, 110, 115 };

 

 

    int FirstIndex = 0;

    int LastIndex = myArray.Length;

 

    //Check that value is in range of array.

    if (Value > myArray[myArray.Length – 1])

        return -1;

 

    if (Value < myArray[0])

        return -1;

 

    while (FirstIndex <= LastIndex)

    {

        int Mid = (FirstIndex + LastIndex) / 2;

        if (Value > myArray[Mid])

        {

            FirstIndex = Mid + 1;

        }

        else if (Value < myArray[Mid])

        {

            LastIndex = Mid – 1;

        }

        else

        {

            return Mid;

        }

    }

    return -1; //Not Found

}