Windows Phone Developers

Wednesday, December 23, 2009

Check Multiple Instances of an Application using C#

How to check if multiple instances of the application are running using C# (.NET)

Here is a simple code to check if multiple instances of current application are running in the machine using System.Diagnostics

private static void check_application_process_instances()

{

Process[] oProcess;

String sModuleName;

String sProcessName;

sModuleName = Process.GetCurrentProcess().MainModule.ModuleName;

sProcessName = System.IO.Path.GetFileNameWithoutExtension(sModuleName);

oProcess = Process.GetProcessesByName(sProcessName);

if (oProcess.Length > 1 )

{

MessageBox.Show("More than one instance is running!");

}

}


Digg Technorati Delicious StumbleUpon Reddit BlinkList Furl Mixx Facebook Google Bookmark Yahoo
ma.gnolia squidoo newsvine live netscape tailrank mister-wong blogmarks slashdot spurl StumbleUpon

Where are the .NET DLLs available in the system

.NET DLLs (3.0 and above)are available @ C:\Program Files\Reference Assemblies\Microsoft\Framework\v3.5

Or

C:\Windows\Microsoft.NET\Framework\v2.0.50727

C# .NET DLLs, System DLLs in Windows



.NET DLL Location.NET DLL Location


.NET DLL Location.NET DLL Location

Digg Technorati Delicious StumbleUpon Reddit BlinkList Furl Mixx Facebook Google Bookmark Yahoo
ma.gnolia squidoo newsvine live netscape tailrank mister-wong blogmarks slashdot spurl StumbleUpon

Cannot implicitly convert type 'System.Diagnostics.Process[]' to 'System.Diagnostics.Process'

The compiler requires an explicit conversion. For example, you may need to cast an r-value to be the same type as an l-value. Or, you must provide conversion routines to support certain operator overloads.

Process oProcess;

oProcess = Process.GetProcessesByName(sProcessName);

Since GetProcessesByName creates an array of new Process components and associates them with the existing process resources that all share the specified process name, we need to declare oProcess as shown below:

Process[] oProcess;

oProcess = Process.GetProcessesByName(sProcessName);

C# Error Cannot implicitly convert type 'System.Diagnostics.Process[]' to 'System.Diagnostics.Process', .NET Error Cannot implicitly convert type 'System.Diagnostics.Process[]' to 'System.Diagnostics.Process'

Digg Technorati Delicious StumbleUpon Reddit BlinkList Furl Mixx Facebook Google Bookmark Yahoo
ma.gnolia squidoo newsvine live netscape tailrank mister-wong blogmarks slashdot spurl StumbleUpon

Cannot declare instance members in a static class

This error occurs if you declare a non-static member in a class that is declared static. It is not possible to create instances of static classes, so instance variables would not be meaningful. The static keyword should be applied to all members of static classes.

In this example get_application_process is not declared as static

static class Program

{

private void get_application_process()

{

Process apProcess;

String sModuleName;

}

Solution:

private static void get_application_process()

should solve the problem

Digg Technorati Delicious StumbleUpon Reddit BlinkList Furl Mixx Facebook Google Bookmark Yahoo
ma.gnolia squidoo newsvine live netscape tailrank mister-wong blogmarks slashdot spurl StumbleUpon

The type or namespace name 'Process' could not be found (are you missing a using directive or an assembly reference?)

Yes, the using directive is missing!

using System.Diagnostics;

Should solve the problem

Digg Technorati Delicious StumbleUpon Reddit BlinkList Furl Mixx Facebook Google Bookmark Yahoo
ma.gnolia squidoo newsvine live netscape tailrank mister-wong blogmarks slashdot spurl StumbleUpon

How to check user input in C# (.NET) / VB.NET

How to parse user input using C# (.NET)

Many times we do not get the anticipated input from the user (either as direct entry / through some input files). Converting the input, for example, to say integer can be done as shown below

public static void TryParse_Examples()

{

bool bConversion;

int result;

// Failure //

bConversion = int.TryParse(null, out result);

if (bConversion == false)

{

MessageBox.Show ("Error Occurred!");

}

// Failure //

bConversion = int.TryParse("s1", out result);

if (bConversion == false)

{

MessageBox.Show("Error Occurred!");

}

// Success //

bConversion = int.TryParse("100", out result);

if (bConversion == false)

{

MessageBox.Show("Error Occurred!");

}

}

Digg Technorati Delicious StumbleUpon Reddit BlinkList Furl Mixx Facebook Google Bookmark Yahoo
ma.gnolia squidoo newsvine live netscape tailrank mister-wong blogmarks slashdot spurl StumbleUpon