The .NET Framework 4.0 contains a plethora of command line tools ranging from build, deployment, debugging, security to Interop tools, and so on. Here’s a list of important command line tools in the .NET Framework 4.0 which can be run using the Visual Studio Command Prompt. The description of these tools has been taken from the MSDN documentation.

Assembly, Build, Deployment and Configuration Tools

Tools Description
Al.exe (Assembly Linker) The Assembly Linker generates a file that has an assembly manifest from one or more files that are either modules or resource files.
Gacutil.exe (Global Assembly Cache Tool) The Global Assembly Cache tool allows you to view and manipulate the contents of the global assembly cache and download cache.
Ilasm.exe (MSIL Assembler) The MSIL Assembler generates a portable executable (PE) file from Microsoft intermediate language (MSIL)
Ildasm.exe (MSIL Disassembler) The MSIL Disassembler is a companion tool to the MSIL Assembler (Ilasm.exe). Ildasm.exe takes a portable executable (PE) file that contains Microsoft intermediate language (MSIL) code and creates a text file suitable as input to Ilasm.exe.
Installutil.exe (Installer Tool) The Installer tool is a command-line utility that allows you to install and uninstall server resources by executing the installer components in specified assemblies
Mage.exe (Manifest Generation and Editing Tool) and MageUI.exe The Manifest Generation and Editing Tool (Mage.exe) is a command-line tool that supports the creation and editing of application and deployment manifests.
Ngen.exe (Native Image Generator) The Native Image Generator (Ngen.exe) is a tool that improves the performance of managed applications. Ngen.exe creates native images, which are files containing compiled processor-specific machine code, and installs them into the native image cache on the local computer
Regasm.exe (Assembly Registration Tool) The Assembly Registration tool reads the metadata within an assembly and adds the necessary entries to the registry, which allows COM clients to create .NET Framework classes transparently.
Regsvcs.exe (.NET Services Installation Tool) The .NET Services Installation tool loads and registers an assembly, generates, registers, and installs a type library into a specified COM+ application and configures services that you have added programmatically to your class.
Resgen.exe (Resource File Generator) The Resource File Generator converts text (.txt or .restext) files and XML-based resource format (.resx) files to common language runtime binary (.resources) files that can be embedded in a runtime binary executable or compiled into satellite assemblies.
Storeadm.exe (Isolated Storage Tool) The Isolated Storage tool lists or removes all existing stores for the current user.

Security Tools

Tools Description
Caspol.exe (Code Access Security Policy Tool) The Code Access Security (CAS) Policy tool (Caspol.exe) enables users and administrators to modify security policy for the machine policy level, the user policy level, and the enterprise policy level
Certmgr.exe (Certificate Manager Tool) Manages certificates, certificate trust lists (CTLs), and certificate revocation lists (CRLs).
Makecert.exe (Certificate Creation Tool) The Certificate Creation tool generates X.509 certificates for testing purposes only. It creates a public and private key pair for digital signatures and stores it in a certificate file
Mscorcfg.msc (.NET Framework Configuration Tool) The .NET Framework Configuration tool (Mscorcfg.msc) is a Microsoft Management Console (MMC) snap-in that enables you to manage and configure assemblies in the GAC and adjust code access security policy
Peverify.exe (PEVerify Tool) Helps you verify whether your Microsoft intermediate language (MSIL) code and associated metadata meet type safety requirements.
SignTool.exe (Sign Tool) Sign Tool is a command-line tool that digitally signs files, verifies signatures in files, and time-stamps files.
Sn.exe (Strong Name Tool) The Strong Name tool (Sn.exe) helps sign assemblies with strong names. Sn.exe provides options for key management, signature generation, and signature verification.

Debugging Tools

Tools Description
Fuslogvw.exe (Assembly Binding Log Viewer) Displays information about assembly binds to help you diagnose why the .NET Framework cannot locate an assembly at run time.
MDbg.exe (.NET Framework Command-Line Debugger) and Sos.dll The NET Framework Command-Line Debugger helps tools vendors and application developers find and fix bugs in programs that target the .NET Framework common language runtime.

Interop Tools

Tools Description
Tlbexp.exe (Type Library Exporter) The Type Library Exporter generates a type library that describes the types defined in a common language runtime assembly.
Tlbimp.exe (Type Library Importer) The Type Library Importer converts the type definitions found within a COM type library into equivalent definitions in a common
Advertisements

http://jesseliberty.com/2011/01/10/12-more-insanely-essential-utilities-for-programmers/

C# supports two types of constructor, a class constructor (static constructor) and an instance constructor (non-static constructor).

Static constructor is used to initialize static data members as soon as the class is referenced first time, whereas an instance constructor is used to create an instance of that class with <new> keyword. A static constructor does not take access modifiers or have parameters and can’t access any non-static data member of a class.

Since static constructor is a class constructor, they are guaranteed to be called as soon as we refer to that class or by creating an instance of that class.

You may say, why not initialize static data members where we declare them in the code. Like this :

private static int id = 10;
private static string name = "jack";

Static data members can certainly be initialized at the time of their declaration but there are times when value of one static member may depend upon the value of another static member. In such cases we definitely need some mechanism to handle conditional initialization of static members. To handlesuch situation, C# provides static constructor.

Let me explain you with examples :

//File Name : Test.cs
using System;
namespace Constructor
{
class Test
{
//Declaration and initialization of static data member
private static int id = 5;
public static int Id
{
get
{
return id;
}
}
public static void print()
{
Console.WriteLine("Test.id = " + id);
}
static void Main(string[] args)
{
//Print the value of id
Test.print();
}
}
}

In the above example, static data member <id> is declared and initialized in same line. So if you compile and run this program your output would look similar to this :

Test.id = 5

Lets create one more class similar to class Test but this time the value of its static data member would depend on the value of static data member <id> of class Test.id.

//File Name : Test1.cs
using System;
namespace Constructor
{
class Test1
{
private static int id ;
//Static constructor, value of data member id is set conditionally here.
//This type of initialization is not possible at the time of declaration.
static Test1()
{
if( Test.Id < 10 )
{
id = 20;
}
else
{
id = 100;
}
Console.WriteLine("Static<Class> Constructor for Class Test1 Called..");
}
public static void print()
{
Console.WriteLine("Test1.id = " + id);
}
static void Main(string[] args)
{
//Print the value of id
Test1.print();
}
}
}

As you can see in the above static constructor, static data member <id> is initialized conditionally. This type of initialization is not possible at the time of declaration. This is where static constructor comes in picture. So if you compile and run this program your output would look similar to this :

Static<Class> Constructor for Class Test1 Called..
id = 20

Since <id> in class Test was initialized with a value of 5, therefore <id> in class Test1 got initialized to a value of 20.

Some important point regarding static constructor from C# Language Specification and C# Programmer’s Reference :

1) The static constructor for a class executes before any instance of the class is created.
2) The static constructor for a class executes before any of the static members for the class are referenced.
3) The static constructor for a class executes after the static field initializers (if any) for the class.
4) The static constructor for a class executes at most one time during a single program instantiation
5) A static constructor does not take access modifiers or have parameters.
6) A static constructor is called automatically to initialize the class before the first instance is created or any static members are referenced.
7) A static constructor cannot be called directly.
8) The user has no control on when the static constructor is executed in the program.
9) A typical use of static constructors is when the class is using a log file and the constructor is used to write entries to this file.

Hopefully, this would clear the confusion some of developers have about static constructor.

A ASP.NET web page goes blank if you inject the javascript alert(); function using the Response.Write from the the code behind.

Here is the fix for it.

private void ShowMessage(string message)

{

StringBuilder scriptBuilder = new StringBuilder("<script language=’javascript’>function window.onload(){");

scriptBuilder.Append("alert(‘" + message + "’);");

scriptBuilder.Append("}</script>");

if (!ClientScript.IsClientScriptBlockRegistered("MyMessage"))

{

ClientScript.RegisterClientScriptBlock(this.GetType(), "MyMessage", scriptBuilder.ToString());

}

}

private void UpdateConnectionString(string ConfigPath)

{

XmlDocument xmlDocument = new XmlDocument();

xmlDocument.Load(ConfigPath);

XmlNode parentNode = xmlDocument.DocumentElement;

if (parentNode.Name == "connectionStrings")

{

foreach (XmlNode childNode in parentNode.ChildNodes)

{

if (childNode.Name == "add" && childNode.Attributes["name"].Value=="MySqlConnection")

{

string sqlConnectionString = childNode.Attributes["connectionString"].Value;

SqlConnectionStringBuilder sqlBuilder = new SqlConnectionStringBuilder(sqlConnectionString);

sqlBuilder.InitialCatalog = "yourDatabaseName";

sqlBuilder.IntegratedSecurity = true;

sqlBuilder.Password = "";

//Change any other attributes using the sqlBuilder object

childNode.Attributes["connectionString"].Value = sqlBuilder.ConnectionString;

}

}

}

xmlDocument.Save(ConfigPath);

}

private string GetOpenPort()

{

int PortStartIndex = 1000;

int PortEndIndex = 2000;

IPGlobalProperties properties = IPGlobalProperties.GetIPGlobalProperties();

IPEndPoint[] tcpEndPoints = properties.GetActiveTcpListeners();

List<int> usedPorts = tcpEndPoints.Select(p => p.Port).ToList<int>();

int unusedPort = 0;

for (int port = PortStartIndex; port < PortEndIndex; port++)

{

if (!usedPorts.Contains(port))

{

unusedPort = port;

break;

}

}

return unusedPort.ToString();

}

private bool PublishWebSite(string SiteName)

{

try

{

string siteName = "YourSiteName";

string applicationPoolName = "ASP.NET v4.0 Classic";

string virtualDirectoryPath = "/";

//Path to the folder of the published code

string virtualDirectoryPhysicalPath = "yourDirectoryPath";

//IP address of current machine where the site is to be published

string ipAddress = "IpAddressOfServer";

//Port to be assigned to the site

string tcpPort = "1109";

//Site name that appears in the URL

string hostHeader = string.Format("www.{0}", siteName);

string applicationPath = "/";

long highestId = 1;

using (ServerManager serverManager = new ServerManager())

{

Site site = serverManager.Sites[siteName];

if (site != null)

{

return false;

}

ApplicationPool appPool = serverManager.ApplicationPools[applicationPoolName];

if (appPool == null)

{

throw new Exception(String.Format("Application Pool: {0} does not exist.", applicationPoolName));

}

highestId = serverManager.Sites.Max(i => i.Id);

highestId++;

site = serverManager.Sites.CreateElement();

site.SetAttributeValue("name", siteName);

site.Id = highestId;

site.Bindings.Clear();

//Assign the IP address , Port and site URL

string bind = ipAddress + ":" + tcpPort + ":" + hostHeader;

Binding binding = site.Bindings.CreateElement();

binding.Protocol = "http";

binding.BindingInformation = bind;

site.Bindings.Add(binding);

Application application = site.Applications.CreateElement();

application.Path = applicationPath;

application.ApplicationPoolName = applicationPoolName;

//Create a virtual directory in IIS

VirtualDirectory vdir = application.VirtualDirectories.CreateElement();

vdir.Path = virtualDirectoryPath;

//Set the physical path of the folder

vdir.PhysicalPath = virtualDirectoryPhysicalPath;

application.VirtualDirectories.Add(vdir);

site.Applications.Add(application);

//Add this new site to the pool of sites in IIS

serverManager.Sites.Add(site);

serverManager.CommitChanges();

}

return true;

}

catch (Exception)

{

return false;

}

}