I am using the hyper v role of windows server 2008 R2. I want to install OS in the virtual machine through C# code.
Also if there is an existing VHD of other VM can I able to link that VHD to a new VM with C# code. Waiting for your help.
Ah. No. You can not never ever install an OS from C# code, you know. How would you do that on a physical server?
There are some ways to autoamte the setup process. They are windows integrated (unattended install) and a prepared image could possibly use a .NET program to pull an answer file automatically, but that is something different than you asked for.
All the configuration you can easily do from any windows integrated language (which includes any .net language). Hyper-v, being a windows system feature, is fully exposed through WMI.
has some starts for using PowerShell, the same principles apply for any WMI capable language.
C# is just a language, and much like any other. The question you're asking has little to do with language.
If you're asking if you can programmatically manipulate a VM under Hyper-V, the answer is yes. Hyper-V is entirely managed through WMI interfaces, as defined here:
You can interact with that in C#, using the .Net WMI classes, or you can (probably more easily) drive PowerShell cmdlets with your C# code. Whichever you're more comfortable with, they'll be more similar than different, as they both resolve down to .Net classes.