How to change default Visual Studio version

This is simple yet many developers face this problem. So thought to blog about it hoping that this will be helpful to someone.

Say you have multiple versions of Visual Studios’ installed in your system. Obviously, lastly installed version will be default one i.e., launched when you type ‘devenv’ in Run Command (Start -> Run -> Command). Here is a way to change it to version which you use most or want to be opened when you use devenv.

  1. Open Registry Editor  – Start -> Run -> Type ‘regedit’ (without quotes)
  2. Navigate to HKEY_LOCAL_MACHINE -> SOFTWARE -> Microsoft -> Windows ->Currentversion -> App Paths -> devenv.exe
  3. Right-click on devenv.exe and click on Modify ( a dialog will be displayed where you can change the value of it)

Usually the value will be C:\Program Files (x86)\Microsoft Visual Studio x.x\Common7\IDE\devenv.exe

You have to replace that x.x to the version which you want.

Changing the default language in Visual Studio

You might have came across a situation where you set the default version of Visual Studio to some language mistakenly or you want to change it because of some reason. Here is the solution for it.
It might be looking silly. But for those who are in need. It mean a lot, I suppose.
There are two ways of doing this.
First:

  1. Choose Tools -> Import and Export Settings…
  2. Select Reset All Settings and click Next
  3. Select whether you would like to save the current settings (I would say not here) and click Next
  4. Select the settings you want to use(change the language to C#) and click Finish

But, this is not advisable if there are any environmental settings that are set in the VS. In this case, you can use the following approach.
Second:
Modifying CurrentSettings.vssettings file. This file can be found in My DocumentsVisual Studio xSettings directory.
Open CurrentSettings.vssettings in a text or XML editor, and search for DefaultProjectLanguage.
You can then change the value accordingly. Here are the two options for VB or C#.

<PropertyValuename="DefaultProjectLanguage">VB</PropertyValue>

<PropertyValuename="DefaultProjectLanguage">CSharp</PropertyValue>

Save the file, and once you open Visual Studio, you’re set.
Hope this helps!

    	
  1. Choose Tools -> Import and Export Settings...
  2. Select Reset All Settings and click Next
  3. Select whether you would like to save the current settings (I would say not here) and click Next
  4. Select the settings you want to use(change the language to C#) and click Finish

intereSTRINGS

‘inetreStrings’ – liked the nomenclature and the sense of humor of the author of it very much.

String vs string
Lower case string is simply an alias for System.String in the .NET Framework.
== or .Equals
A rule of thumb is that for almost all reference types e.g. objects, use Equals when you want to test equality. == compares whether two references (i.e. object instances) refer to the same object.
Primitive types can be compared for equality using ==, but in the case of strings, the variables on each side of the operators must be of type string.
Have a look at the following code and output, taken from Jon Skeet’s blog.

01    using  System;
02    
03    public class Test
04    {
05    static void Main()
06    {
07    // Create two equal but distinct strings
08    string a = new string(new char[] {'h', 'e', 'l', 'l', 'o'});
09    string b = new string(new char[] {'h', 'e', 'l', 'l', 'o'});
10    
11    Console.WriteLine (a==b);
12    Console.WriteLine (a.Equals(b));
13    
14    // Now let's see what happens with the same tests but
15    // with variables of type object
16    object c = a;
17    object d = b;
18    
19    Console.WriteLine (c==d);
20    Console.WriteLine (c.Equals(d));
21    }
22    }
23    The results are:
24    
25    True
26    True
27    False
28    True

@ before a string
Using @ before a string means the escape sequences are not processed. This making it easier to write fully qualified path names for example.
@”c:DocsSourcea.txt” // rather than “c:\Docs\Source\a.txt”
Stringbuilder
String objects are immutable, and so editing them, using Replace for example, results in a entirely new string being created, with new memory allocation. The original string is left intact in memory.
The StringBuilder class is designed for situations when one needs to work with a single string and make an arbitrary number of iterative changes to it. New memory allocation is not needed.

Came across the above on Bishop’s Blog while searching about something related to VSTO.

Today I am on MSDN Forum :)

Hurray… Today I am in the list of Top Answerers of the Month in VSTO Forum of MSDN. I am enthralled to see my name in such a great and universal Forum 🙂
This kind of things really incite interest in entrants like me to contribute more to the community.
Usually I visit Forum to get solutions to the problems faced by me. Some times, when I came across the problem which I have already faced, I will respond. But, these days, answering became a hobby of mine. When ever I get time, I am logging in and answering the forum posts, if I know the answer.
Here is a snapshot of the Top Answerers List taken from VSTO Forum on MSDN.

Code Access Security in .NET

Code Access Security is one of the powerful features of .NET but at the same time some confusion exists about its terminology and its usage. With this, I would like to make an attempt to explain some of the terminology with diagrams. This document does not explain all the details related to CAS, as lot of information already exists on the internet and only gives a jump start to Code Access Security.

The basics thing is whenever any code is being executed in managed world the .NET runtime verifies whether that code is allowed or not based on evidence and set of permissions.

Evidence

Where from the code came?

Permissions

What permissions are allowed for the code?

Basics Details

With the above basics let us explore Evidence and Permissions and other Code Access Security terminology.

Evidence

Evidence of an assembly is about its identity who it is and where it came from.

  • From where the assembly is being loaded?
  • If the assembly is downloaded from the web, what is the URL of the source directory?
  • What is Strong Name (if any)?
  • Who is the publisher (if digitally signed)?

How does System find out about this?
Codebase property of an assembly determines about its location which will result in either local directory, intranet/internet.One can get strong name and publisher details from the assembly manifest.
Once the Evidence of the assembly is gathered, the system grants some permission to this assembly.
How does System find out what permissions an assembly can have?
Time to explain some more terms. Let’s try to understand the following.

Policy Levels

.NET runtime security has 4 policy levels viz., Enterprise, Machine, User, and AppDomain. (which can be done programmatically).
Each policy has multiple code groups and multiple permission sets.

Code groups

Code group is a group of assemblies which satisfies the same condition. A code group will have one member condition which decides about particular assembly can be a member of this (group) or not.

Permissions and Permission Sets

Permission is what the code can do with a particular resource like File, Registry etc., and Permission Set is collection of permission.
.NET Default Policies will be

Now, we understood Policy Levels, Code Groups and their Permissions. It is time to find out how the system assigns the permissions to the loaded assembly.
Below is pseudo code how .NET Security system grants the permission to the assembly (Assumes no programmatic security)

public PermissionSet GetPermission(Assembly asm )

{

foreach( PolicyLevel pl in System.Security.Configuration.PolicyLevels )

{

foreach( CodeGroup cg in pl.CodeGroups )

{

if( cg.MembershipCondition exists in asm.Evidence )

{

// In given level it is the UNION of permissions across code groups

permissionSet[pl] = permissionSet[pl] Union cg.PermissionSet;

}

}

}

// Across levels it is intersecion of permissions

finalPermissionSet = permissionSet[“Enterprise”] Intersecion permissionSet[“Machine”]  Intersecion permissionSet[“User”];

return finalPermissionSet;

// Assume Exclusive and LevelFinal are not set

}

From above the .NET Security system assigns the permission set to the assembly.
At this point system has assigned the permission to the assembly

How does it get enforced?
The .NET classes ( System.IO.File , Microsoft.Win32.Registry classes will do the rest ) will enforce this by calling to appropriate security class.
Lets’ see how the File.OpenText code does this.

Pseudo code for OpenText()

{

string[] pathList = new string[1];

pathList[0] = pathToOpenFile;

new FileIOPermission( FileIOPermissionAccess.Read,pathList).Demand();

}

The call to .Demand will check the permission and throw an exception if it is not permitted to open the file.
If you have call to unmanaged dll where you open the text file the .NET will not be able to check whether you have permission to open or not.

Exclusive

Each code group in a policy consists of one property called Exclusive which means don’t consider any other permissions in that policy.
Lets’ walk through this…
Suppose your assembly belongs to Code group A, Code Group B and Code group C in Machine Policy.

Machine Policy

Code Group A

Permission Set A

Code Group B

Permission Set B

Code Group C

Permission Set C

And ‘Assembly Z’ falls in all code groups. Then permission set in this policy will be
Machine Level Policy for Assembly Z = (Permission Set A)   U (Permission Set B)  U (Permission Set C)  
But if set Exclusive property True in Code Group B then permission set in Machine Policy will be
MacineLevel Policy for Assembly Z =  ( Permission Set B )
Note: Still the final permission set will be intersection of Enterprise, Machine and User.
The exclusive property in .NET Configuration tool will be having check mark as


Level Final

There is another property called Level Final when checked, On Code Group, the policy in the next level will not considered.
Lets’ walk through this…

Enterprise Policy

Code Group

All_Code

Permission E

Machine Policy

Code Group

All_Code

Code Group A

Permission Set_A

Code Group B

Permission Set_B

Code Group C

Permission Set_C

User Policy

Code Group

Code Group 1

Permission Set_1

Code Group 2

Permission Set_2

Code Group 3

Permission Set_3

Suppose if an assembly belongs to All_Code in Enterprise, Code Group A,B,C in Machine and Code Group 1,2,3 in User then
Final Permission set = (Permision E) Intersection (Permision_A U Permission_B U Permission_C) Intersection (PermissionSet_1 U PermissionSet_2 U PermissionSet_3)
With Final Property set on Code Group B in Machine Policy then User Policy will not be evaluated and hence.
Final Permission set = (Permision E) Intersection ( Permision_A U Permission_B U Permission_C )
In .Net Security Configuration tool the Level Final property will be shown like below

With Exclusive and Level Final property one can precisely control the permission set applied to an assembly.

Deployment Image Servicing Management (DISM)

DISM is a new command line tool which was introduced in Windows 7 for deployment. DISM can be used to service a Window image or to prepare a Windows Pre-installation Environment (Windows PE) image. DISM also supports more features and functionality than the other tools (Package Manager) supported in Windows Vista, including logging, inventory commands with parse-able output, detailed help, offline INF driver package installation, direct application of MS update packages, and integration of international settings.

  1. Introduction to Deployment Image Servicing & Management (DISM)
    Deployment has a new tool in Windows 7: DISM (Deployment Image Servicing and Management). DISM is a command line tool which can be used to service a Windows® image or to prepare a Windows Preinstallation Environment (Windows PE) image. It replaces Package Manager (pkgmgr.exe), PEimg, and Intlcfg that were included in Windows Vista®. Read More
  2. Mounting a Wim Image for Offline Servicing
    Deployment Image Servicing and Management (DISM) is a tool which allows you to service your images offline. It supports both .wim and .vhd formats. In this article, we will look at how do you mount an Wim Image using this tool! Read More
  3. Adding or Removing Drivers from an offline Image
    You can offline service an image using DISM and add drivers to the mounted ones. You will need to download driver packages from OEM’s and ISV’s. These packages need to be INF-based packages (.exe or .msi driver installation is not available offline). Read More
  4. Enable/Disable Features using DISM on an offline Image
    You can offline service an image using DISM and enable/disable a feature on the mounted ones. In this short article, let us see on how we can do the same. Read More
  5. Unmount and clean a Wim Image using DISM Commands
    DISM provides you an option to load Wim images onto your local drive and the service it offline. You can either add/remove a driver, enable/disable features, perform other customizations.. without even installing them on a physical box. Now when you have mounted a Wim image, its really necessary to check if the image is mounted correctly. you can do that by running the command, dism /get-MountedWiminfo. Read More

Prepare your Applications for Windows 7

We all have this question in our mind.

“Why does my application break when we change the Operating Systems?”

Typically, whenever a new OS is released, Microsoft makes every effort to ensure application Compatibility. Unfortunately, as the OS evolves and when reliability, security and performance improvements are made, there are some comprises which are made that ends up breaking some applications. Thankfully, when the changes are decided to be made, there is some mitigation planned to address the applications which are broken. Hence, Application Compatibility is one important which is on the top of everybody’s mind.

This series of posts covers information on how the core changes of Windows 7 will affect the applications. Also, it covers some basic methods to mitigate these issues from the viewpoint of Developers, IT Pros and End-users.

A key consideration when moving to Windows 7 is whether your applications will run successfully. Download the Microsoft Application Compatibility Toolkit (ACT) to access the necessary tools and documentation to evaluate and mitigate application compatibility issues before deploying Windows 7.

Community Tech Days @ Hyderbad Rocked!!!

The day started a bit early today as we have to present a small session at Community Tech Days @ Hyderabad which was confirmed today itself.
The event went very well, over 200 attendees have attended the event. Speakers from various states came to Hyderabad to attend the event.
I first met Harish Ranganathan, my favorite evangelist just after entering the hall.
Hima Bindu delivered the first session on Web Development Enhancements in VS 2010 which was very interactive and informative, explained about the enhancements in VS 2010 in the Web Development domain. Next Arun Ganesh spoke on WCF – A Developer’s Perspective, which pulled out some interesting points on WCF. After this, we got a chance to give out our mini session on Hyderabad Techies. 🙂
It was really a great experience to stand infront of Tech Don’s and deliver a session (especially for a guy like me). That’s why, I was a bit tensed in the beginning, but covered later 🙂
We got many appreciations and compliments from some great people over there (both personally and regarding Hyderabad Techies).

Next sessions are IT Pro realted. Didn’t expect many developers to these sessions as it is for administrators ans IT Pros’. But, to our surprise, there are more number of developers than administrators.
Hariveer and Muqeet delivered the sessions on VS Lab Management 2010 and IIS 7.5 for Administrators.

The last but not least is Windows 7 – Core OS Enhancements and how it would matter our Applications by Vijay Raj. (I was scared after reading this big title at first). At first, I thought that it was some thing like promotion to Windows 7. But, it is not! The session went in a rational manner describing the facts of Windows 7 and its development. This was the most jovial and fun-filled session of all the sesions in the event. We really enjoyed the session like anything. The way he explained and the examples he used to describe was very funny.
I really had a great time with Vijay today, both before and after his session.

Today is a most memorable day in my life which I would never forget in my life. I am very happy and proud to be a part of the Event.
Looking forward for the next CTD…

Why add-ins load slowly?

Hi all,
This question pestered my mind for so many days.
I have developed an add-in. It is working fine but taking much time to load. After studying all the issues related to it, I came to know that the add-in is connecting to crl.microsoft.com and downlaod.microsoft.com every time I open the application (my add-in loads at start-up). This problem comes up with self-signed certificates. It will check the exitence of  self-certificate in Certificate Revocation List.
There are some work-arounds for solving this problem.
This behavior will occur with any .NET 1.1 and 2.0 assembly that is authenticode-signed, not only Measurement Studio assemblies. Digital signing is also referred to as code signing. Code signing a .NET library is strongly recommended by Microsoft, and Measurement Studio signs all of our ActiveX and .NET components.
Code signing of assemblies makes components tamper-proof and ensures that you know the identity of the component publisher.
The reason why this problem is occurring is because of the mechanism used by the .NET Common Language Runtime (CLR) to verify code-signed .NET assemblies. Part of the verification process requires an online look-up to check whether the certificate with which the assembly is signed has been revoked and is no longer valid. Windows does this by downloading a CRL (Certificate Revocation List). The first time a code-signed assembly is loaded by the .NET CLR, the CRL is downloaded from the certificate provider’s server and cached on the system.
When the .NET CLR loads a code-signed assembly and is unable to reach the CRL distribution point, it records the failure as an inability to provide the assembly evidence that it was code-signed. So the assembly is allowed to load, but is not marked as being digitally signed. There is a 15 second delay for CRL retrievals. This is how long the CLR will keep on re-trying to download the CRL before it finally times out. So the delay in loading the .NET assembly occurs because Windows is unable to download the CRL and keeps trying to download it for 15 seconds before timing out.
This behavior is by design.
The .NET CLR will not indicate any error or throw any security exception when verifying a signed assembly if the CRL distribution point cannot be reached. An error here from WinVerifyTrust(), the API used by the .NET CLR to verify a code signed assembly, prevents the assembly from being marked as code signed. Note that this does not apply to assemblies loaded thru the Internet Explorer hosting interface.
You could manually download the CRL and install it on your system. But the CRL is valid only for 10-15 days, so unless your system is able to update the file after this time, you will run into the same problem again.
Microsoft recommends disabling CRL checking as a workaround by disabling this option in Internet Explorer. Use the following steps to disable the CRL checking in Internet Explorer:

  1. Select Start»Control Panel.
  2. Double-click Internet Options.
  3. Select the Advanced tab.
  4. In the Security section, uncheck the Check for publisher’s certificate revocation option.

By disabling the CRL checking using the Internet Options, you are not exposing yourself to a security threat because this check is not working. The reason why this problem is showing up is because your network settings are not allowing Windows to access the CRL.
In addition, it is possible to programmatically set the CRL verification. When the Check for publisher’s certificate revocation is unchecked, a setting in the registry is changed.
To turn off CRL verification, set HKCUSoftwareMicrosoftWindowsCurrentVersionWinTrustTrust ProvidersSoftware PublishingState from 0x00023c00 to 0x00023e00.
To turn CRL Checking on again, reset the State key to 0x00023c00"
Another work-around is to select the ‘Never search for updates’ option in Add-In Properties->Publish->Updates.
So by unchecking the above mentioned option in Internet Explorer and disabling the search for updates in Visual Studio, the problem will be solved.