Posts Tagged ‘.Net’

I have always thought about the fact that Visual Studio comes with such awesome utilities,still it is  difficult to run one by one everytime from outside.

I was trying to figure out a way & it took me to msdn  which talks about how to run external tools from visual studio.I felt as if my wish got granted so fast 🙂 In my previous post I talked about the tool guidgen.exe which helps to create guids & here I will explain how you can run the same guidgen or some other tool from Visual Studio itself.

Open Visual Studio,On the Tools menu  choose External Tools

In the External Tools dialog box,click Add, and enter a name in Title Box – CreateGuid

Add path in Command box

C:\Program Files\Microsoft SDKs\Windows\v7.0A\bin\NETFX 4.0 Tools\guidgen.exe

Leave Arguments box as empty as for this one you do not require that

Add the following to Initial directory

C:\Program Files\Microsoft SDKs\Windows\v7.0A\bin\NETFX 4.0 Tools\

Click Ok ,You are done!

Again click on tools,you will be able to see the menu item with the name you added in Title Box.Click on it,the utility is loaded for you.

Same steps apply to any of the exes which you find in Microsoft SDKs folder.Read more here

Hope you find this useful 🙂


In one of my stored pocedure’s an input parameter accepts Guid value & every time I wanted to test it,I used to run class from C# with Guid.NewGuid().ToString() to copy value & add it to stored procedure.Quite weird right 🙂 Because I never knew how else I can generate a guid value.

Visual Studio External Tools come with a tool guidgen.exe that can help you create Guid in a specified format.

Path – C:\Program Files\Microsoft SDKs\Windows\v7.0A\bin\NETFX 4.0 Tools\guidgen.exe

How to Use guidgen.exe?

  1. Run exe
  2. Select the format you want for the GUID.
  3. Click New GUID & then Copy.

The GUID is copied to the Clipboard so that you can paste it whereever you want.

If you want to generate another GUID, click New GUID

I decided to write a method utilizing Tuple which is newly introduced in VS 2010.I did not bother to open a new solution,just tried to add a new method of return type Tuple in an existing project’s class.

It took half an hour to figure out why it was giving a compilation error saying “The type or namespace name “Tuple” could not be found”.I rushed to msdn to figure out which namespace it referes to or any dll I need to refer.I found,it is added in System namespace which is already added to my cs file.I kept on googling for sometime & nothing helped.Finally figured out that though my solution is VS2010,Target Framework is not set to 4.0 & it’s still 3.5 😦 so simple it is,still took long time to track the exact issue.

Moral is,whenever you are implementing something newly introduced to framework,make sure that you check the target framework version & not the visual studio version :).

I came across this error “Could not find a part of the path” while copying a file from one location to another.It threw exception at the second parameter of this below line

File.Copy(Path,FilePath + @"\" +FileName, true);

Value expected for this second parameter was like this C:\Templates\ANUTESTCODE11\test.xml where the test.xml gets added from the value of FileName.At one particulat section of calling code,the FileName was null & It was reading as C:\Templates\ANUTESTCODE11\ which resulted in the error message “Could not find a part of the path”

I found this during unit testing & this scenario has to be handled in the code.You should not give a chance for your code to fail & the appropriate way of using File.Copy is as below.This code snippet was suggested by one of the readers & adding it here.

{ if (!string.IsNullOrEmpty(FileName))
{ string newPath = System.IO.Path.Combine(FilePath, FileName);
if (File.Exists(Path) && Path.ToLower() != newPath.ToLower())
File.Copy(Path, newPath, true);

catch (Exception ex){ // handle the exception as desired}

It took sometime to figure out what went wrong when I was getting an Unauthorized Access Exception when trying to delete folder from C#.

UnAuthorizedAcess Exception when trying to delete a folder

There was a misleading factor that the folder was residing on our server & had an impression that authorization is not given to do so.I started thinking from a code perspective,when I could delete the folder manually & later via code when I manually changed the readonly attribute to false. So the problem was that readonly attribute gets set by itself while folder/file is getting created or copied via code hence it will not let you delete unless you remove the attributes.

I am adding the code snippet to delete the folder with the comments herewith

public void RemoveFolder(string[] FolderNames)
//Gets Path from web.config
string Path = ConfigurationManager.AppSettings[“Root”];
//Loops through all the folder names & form the DirectoryInfo Path
FolderNames.ToList().ForEach(foldername =>

DirectoryInfo Folder = new DirectoryInfo(Path + @”\” + foldername.Trim());
//Checks if folder exists in the Path provided
if (Folder.Exists)
//Sets attributes for the DirectoryInfo
Folder.Attributes = Folder.Attributes & ~System.IO.FileAttributes.ReadOnly;
//Gets all files inside the folder
FileInfo[] files = Folder.GetFiles();
//For each file,set the attributes so that it does not throw
//unauthorized access exception while trying to delete
Array.ForEach<FileInfo>(files, new Action<FileInfo>(
                            f =>
                                File.SetAttributes(f.FullName, FileAttributes.Normal);



Hope you find this post useful 🙂

Let me first wish all my readers a very very happy & prosperous New Year 2011!

Have you seen the Visual Studio 2010 Events for 2011?A great oppourtunity to hear directly from the experts about VS 2010.These online events are free to attend & open to all.Check it out!

Visual Studio 2010 Events for 2011

It’s been few days since I wrote anything as I was enjoying my christmas vecation 🙂 Hope you all had a wonderful holiday time celebrating christmas.

What I am going to share today is about using extension methods in .Net.I faced this scenario recently & thought will make a note of this so that months later I can look back & remember what I learnt. Extension methods are a special kind of static method that enable you to “add” methods to existing types without creating a new derived type, recompiling, or otherwise modifying the original type.

For example if you are using LINQ to read a OracleDataReader & want to check if a column exists in a DataReader object,we do not have a direct method that does so.Here comes the use of a custom extension method.These are the steps to follow

  • Define a static class to contain the extension method.
  • The class must be visible to client.
  • Implement the extension method as a static method with at least the same visibility as the containing class.
  • The first parameter of the method specifies the type that the method operates on; it must be preceded with the this modifier.
  • namespace Helper

    public static class Extension

    public static bool HasColumn(this IDataRecord dr, string columnName)
    for (int i = 0; i < dr.FieldCount; i++)
    if (dr.GetName(i).Equals(columnName, StringComparison.InvariantCultureIgnoreCase))
    return true;
    return false;

  • In the calling code, add a using directive to specify the namespace that contains the extension method class.
  • Call the methods as if they were instance methods on the type.
  • Note that the first parameter is not specified by calling code because it represents the type on which the operator is being applied, and the compiler already knows the type of your object. You only have to provide arguments for parameters 2 through n.

    public void Format(OracleDataReader reader)
    bool iscolumnexist = reader.HasColumn(“Description”);

    catch (Exception ex)


    Hope you find this code snippet useful 🙂