Saturday, March 22, 2008

Get assembly info about current C# application

Version information of an assembly is stored in the AssemblyInfo.cs file, which consists of Major Version, Minor Version, Build Number and Revision details. You can access the assembly information using different methods.

1. To get the assembly version (AssemblyVersion in AssemblyInfo file), use the following line of code.

System.Reflection.Assembly.GetExecutingAssembly().GetName().Version

2. To obtain the file version number (AssemblyFileVersion in AssemblyInfo file), the following block of code can be used.

System.Diagnostics.FileVersionInfo.GetVersionInfo(System.Reflection.Assembly.GetExecutingAssembly().Location).FileVersion)

3. The GetCustomAttributes() can be used to obtain detailed information about the Assembly. The following block of code can be used to obtain the version information of the executing assembly.

object assemblyObjects = Assembly.GetEntryAssembly().GetCustomAttributes( (typeof(AssemblyFileVersionAttribute)), true);
if (assemblyObjects.Length > 0)
     MessageBox.Show(((AssemblyFileVersionAttribute)assemblyObjects[0]).Version);

The AssemblyFileVersionAttribute class is inside the System.Reflection namespace. You can obtain the complete set of attribute information, by passing just a boolean value to the GetCustomAttributes function (ie. by avoiding the specific Type information. This function has 2 overloads).
A function can be written using Generics, for extracting any required attribute information from the assembly. Basic version of such a function is given below.

public static T GetAssemblyAttribute<T>(Assembly assembly) where T : Attribute
{
    if (assembly == null)
        return null;

    object[] attributes = assembly.GetCustomAttributes(typeof(T), true);

    if (attributes == null)
        return null;

    if (attributes.Length == 0)
       return null;

    return (T)attributes[0];
}

The function can be invoked as follows.

AssemblyFileVersionAttribute fileInfo = GetAssemblyAttribute<AssemblyFileVersionAttribute>(Assembly.GetEntryAssembly())

Friday, March 14, 2008

Console Application - Current Directory

In the case of Windows application, obtaining the current directory is simple; just use Application.StartupPath. For console applications, it is different. Multiple methods can be used and a few of them are listed below.

1. Use System.Environment.CurrentDirectory. It returns the directory, from which the current process started.

2. System.Reflection.Assembly.GetExecutingAssembly().Location returns the fully qualified name of the current executable. To extract the current directory name, call function
System.IO.Path.GetDirectoryName() with this value.

Monday, March 3, 2008

Installing Visual C# 2005 Express Edition (Manual Installation)

The standard link for downloading C# Express edition provides the bootstrapper installer, which downloads the required installation files once you start the installation process. It takes time, depending on the packages selected (C# alone, ~30 MB and including MSDN, more than 300 MB).

If the bootstrapper installer fails to install on your computer because of networking problems, or if you want to download a full copy of the Express Edition (probably to burn on CD), then you need to follow the manual installation process. The steps are given below.

Step 1: Download the image file (VCS.IMG) by clicking on this link. After downloading, if necessary, this image file can be directly burned to a CD by using a third party software package such as Nero or Roxio’s Easy Media Creator. If you are not planning to burn the image, continue to next step.

Step 2: Many CD writing softwares allows you to mount disc image files (IMG, ISO etc) in a virtual drive as if they were physical discs in a physical drive. Smart-Projects's IsoBuster is one such package. Download and install IsoBuster to the local system.

Step 3: Open the VCS.IMG file from IsoBuster. You can see the list of files contained inside the img file in the right hand pane.

Step 4: Select all files in the right hand side by clicking CTRL+A. Right click on the selection and then select "Extract Objects" option from the context menu that is displayed. The screenshot is given below.


Step 5: Select the folder to extract the contents of the image file and click OK. This will start the extraction process of the image files. This process may take a few minutes to complete. The sample screenshot is given below.


Step 6: Once the extraction process is complete, execute the setup.exe file to begin the Express Edition installation process.

Step 7: After finishing the installation, it is necessary to register the software. If you have a Hotmail id, you can use the same for the installation process. If not, its better to create a new one; since it is always better to have a Windows Live Id. Microsoft will send the product registration key to your email address. Use this key to register the Express edition, by selecting the Register option from the Help menu.