Execute Commands From C#
I usually run across the need to execute commands or run applications during the installation or configuration of an application. I’ve found this technique very useful in place of using batch files. You have a much more robust environment to work with and you don’t have to worry about someone modifying your batch file. For this example, I’m just going to show you how execute a command that you would normally use in a Command Prompt. I normally include a function like the following in a static utility class so that it is accessible from anywhere in the application.
public static int ExecuteCommand(string Command, int Timeout)
{
int ExitCode;
ProcessStartInfo ProcessInfo;
Process Process;
ProcessInfo = new ProcessStartInfo(“cmd.exe”, “/C ” + Command);
ProcessInfo.CreateNoWindow = true;
ProcessInfo.UseShellExecute = false;
Process = Process.Start(ProcessInfo);
Process.WaitForExit(Timeout);
ExitCode = Process.ExitCode;
Process.Close();
return ExitCode;
}
Unique Sorting ({ 1, 1, 5, 5, 5, 9, 3, 3, 3, 9, 9, 9,2 })
Steps for unique sorting:
1. Create console application .
2. Copy following method in ‘Main’ method:
int[] DataArray = { 1, 1, 5, 5, 5, 9, 3, 3, 3, 9, 9, 9,2 };
int[] SortedUniqueData = MakeUnique(DataArray);for (int i = 0; i < SortedUniqueData.Length ; i++){
Console.WriteLine(“Sorted value : {0}”, SortedUniqueData[i]);}
Console.ReadLine();
3. Copy following method to outside ‘Main’ method , but within the same class:
static T[] MakeUnique<T>(T[] values){
System.Collections.Generic.Dictionary<T, bool> dict;dict = new System.Collections.Generic.Dictionary<T, bool>();
foreach (T value in values)if (!dict.ContainsKey(value)) dict.Add(value, false);
List<T> newDict = new List<T>(dict.Keys);newDict.Sort();
return newDict.ToArray();}
4. Now run the application, u will get unique sorting result.
cheearo man
.Net Micro Framework 2.0
The Microsoft .NET Micro Framework is an environment that extends the advantages of Microsoft .NET and the toolset in the Microsoft Visual Studio development system into a class of smaller, less expensive, and more resource-constrained devices than previously possible with other Microsoft embedded offerings.
Difference between exe and assembly
1. EXE contains entry-point through which we can execute exe file. while dll doesnot contain entry-point. so we cant execute it.
2. EXE is a executable file which runs in seperate process managed by os. while dll is
a dynamic link library (link at runtime) which used in exe or in other dll files.
3. EXE is an out-process file and dll is a in-process file.
Difference between process and service
when we start machine, service starts in background automatically although user doesnt log in. while any application starts by user will start process.
A process is any piece of software that is running on a computer. Some processes are services in that the start up when the computer starts. No user has to log on to start them. An “application” is generally a process that a user has to start.
Problem with ClientScript.RegisterClientScriptBlock when using Ajax
There’s a breaking change in the beta. to insert scripts during a partial postback youmust use the registerXXX static methods of the scriptmanager class.
e.g.
Microsoft.Web.UI.ScriptManager.RegisterClientScriptBlock(DropDownList1, typeof(DropDownList),“TestKey”,” alert(’TEst Key’); “,true);
Anonymous Methods in C#
You must be aware of delegates if you have worked in C# before. Even then let me describe delegates my way. Delegates are objects that encapsulate the reference to functions. Implementation of Event Handler code is one of the best examples of delegates.
When we double click on a button on the form to add the handler for that button’s click event, the windows form generate two separate codes. The actual event Handler and a hidden code (found in the designer code) to wire-up the button clicks event.
The code outputted is like this.
private void button1_Click(
object sender, System.EventArgs e)
{ }and
this.button1.Click += new System.EventHandler(
this.button1_Click);
[Note: The Signature for the button click event handler is defined in the System.EventHandler delegate]
We can simplify this a bit by using anonymous method. We can create the whole handler method inline without defining a method name. We will still require the patameters.
this.button1.Click +=
delegate(object sender, EventArgs e)
{ };
Here you can see there is no method name and we are not delegating the event to another method instead writing the method inline.
Static Class – A When to use class
We use static class when we have to separate data and behavior that will be independent of any object identity. The data and functions do not change regardless of what happens to the object.
A static class can only contain static members. We cannot create an instance of a static class. Static class is always sealed and they cannot contain instance constructor. Hence we can also say that creating a static class is nearly same as creating a class with only static members and a private constructor (private constructor prevents the class from being instantiated).
The advantage of using the static class is that compiler can check that no instance of the class is accidentally added. The complier will not allow any instance class of a static class. We also cannot inherit a static class since it is sealed. Static class do not have constructor, but can still declare static constructor to set up the initial values.
Static class also makes the implementation simpler and faster since we do not have make and instance of the class to call its method. An example of good use of static class would be a class like math, which does all the mathematic function, or a currency converter class to convert currency class for converting the currency.