Archive for the ‘.Net4.0’ Category

Sharing a small tip 🙂 If you want to add spaces to the end of a string,C# gives us method PadRight to do that.If the length of string is specified as 16 for eg and incase user enters only three characters and you want to add spaces to create a total length of 16,you can write as below.This example gets input from a textbox and adds spaces to the end of string entered.

this.txtLoginName.Text.ToString().PadRight(16);

Today I was having a look at one of our WCF Restful projects.To start with the code understanding,I decided to host that in my local host and then take a look.To my surprise,I was unable get a svc url as we usually get, for example: http://wordpress.com/Service1.svc .Digging further I realized, being a wcf restful service it’s not ideal having an extension this way and it has been achieved through System.Web.Routing integration .Routing allows to host a WCF restful service that responds to URIs without an extension .Cool right?It’s a new feature with .Net 4.0.I am avoiding a rewriting of an example code here as msdn has got a beautiful simple explanation over here.Take a look!

Today’s post is a small code snippet for you to write byte array to an xml file using FileStream and Binary Writer class’s Write method.Hope this helps 🙂

private void ByteArrayToFile(string fileName, byte[] byteArray)
{
FileStream fs = new FileStream(fileName + ".xml", FileMode.Create, FileAccess.ReadWrite);
BinaryWriter bw = new BinaryWriter(fs, Encoding.Unicode);
bw.Write(byteArray);
bw.Close();
fs.Close();
}

Recently I wanted to create a web sample & happened to open an EmptyWebsite from vs2010.What I could notice is,web.config looks very simple with few lines of code as below.

I was surprised & was eager to read about this.Reason behind this is that .NET 4 includes a new version of the CLR and a new .NET 4 specific machine.config file.Hence the config settings have been moved down to the machine config file.This machine.config file now automatically registers all of the ASP.NET tag sections, handlers, modules and settings for the following:

ASP.NET AJAX
ASP.NET Dynamic Data
ASP.NET Routing
ASP.NET Chart Control

 This change for ASP.NET 4.0 config files is to reduce the web.config file size,making things less complicated and easier to read.Cool right 🙂 N-joy!

Did you know that it has become very easy to verify 64-Bit Operating Systems and Processes with .Net4.0?

The Environment class has two new additions to help with this,Environment.Is64BitOperatingSystem and Environment.Is64BitProcess properties.Property returns true if it is 64-bit,otherwise  false.

Add the following line System.Diagnostics.Debugger.Break(); in the OnStart() method of Service. Compile in debug mode,install the windows service using installutil from command prompt and start the service.When you start the service, you will be prompted to debug the process. Select the Visual Studio debugger.It will take you to the service and you can debug accordingly.If you want to modify any code,Just stop it,modify the code,compile and re-start service.Hope this helps 🙂

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 http://msdn.microsoft.com/en-us/library/9814d200.aspx

Hope you find this useful 🙂