Thursday, December 25, 2008

Visual Studio 2008, Intellisense not working after statment completion

Since I upgraded to visual studio 2008, my intellisense stop working. When i complete the statment, i am not getting the intellisense to finish my statement, but if i hit Ctrl + Space, my intellisense was popping up, but that's not enough for me. Everytime i can't hit the keys to get my statement complete. So i just poked into the various option for making things working properly.

When i look into the Tools->Option ->Text Editor->C# (My programming language) - The two checkbox under the statement completion, was unchecked, having checked out the two options, my intellisense starts working.

So, if you have any issues like, intellisense not working, try this approach first.

Happy Programming

Wednesday, December 17, 2008

How to read and create a temporary file from embedded resource



While I was writing the unit test for one of the scenario, where I need to get the data from a csv file from the specified path, for running my unit test. Then comes, the question - How can i add the file to the solution and the get the necessary data from the file and what are the ways? There are two options



  1. First mark the csv file as an embedded resource in the solution, and have a little utility to copy the file to your temp folder and get the path from there.

  2. Mark the csv file as an embedded resource in the solution, and then stream the file straight from the assembly.

The second option is probably preferred because you don't pick up the bottle neck of touching the disk, but unfortunately, my test method accepts the path of the file name, so i have to write a utility to read the file from the assembly, and copy it to the temp folder. Here are the steps to work it out.
To add an file as embedded resource to the project, follow the steps below.
Since the test file will be available with us, just add the file to the project, by choosing Add Existing Item menu in the right click option of the project.



After adding the project, go to the properties of the file added, In the Build Action selection embedded resource, and rebuild the project, that's it your files has been added to the project as an embedded resource.


So now how to read the embedded resource, and create a temporary file.


With the help of assembly.GetMainfestResourceStream(resourceName), you can get the resource stream and the Path.GetTempFileName(), creates a uniquely named, zero byte temporary file on the disk, and returns the full path of that file, and now write the stream to the specified path.



Below is the sample which reads the embedded resource and creates a temp file in the temporary folder.



private const string SampleFile = "TestProject.CRM.ImportData.csv";
public static string UnpackManifestResource(string resourceName, string extenstion, Assembly assemblyName)
{
// Extract the specified resource as a stream
Assembly assembly = assemblyName;
if(assemblyName == null)
assembly = Assembly.GetExecutingAssembly();
string targetPath = Path.GetTempFileName();
if (!string.IsNullOrEmpty(extenstion))
targetPath = Path.ChangeExtension(targetPath, extenstion);
using (Stream resourceStream = assembly.GetManifestResourceStream(resourceName))
{
if (resourceStream != null)
{
var readBytes = new byte[resourceStream.Length];
resourceStream.Read(readBytes, 0, (int)resourceStream.Length);
using (var outputStream = new FileStream(targetPath, FileMode.Create))
{
outputStream.Write(readBytes, 0, readBytes.Length);
}
}
}
return targetPath;
}

Monday, December 15, 2008

CDO1.2.1 throwing COMException on Outlook 2007

I was working on a Outlook Add-In to save email from Outlook 2003 and 2007 using Outlook Object Modle, which seems to work fine. Then we got a requirement to support the Exchange format mail to, in which we got stucked, while reteriving the email id of the recipients and sender it gives in exchange format, which we doesn't want. To overcome this issue we have used CDO 1.21 to get the SMTP address. This works fine in Outlook 2003, When we are testing it in Outlook 2007, we got an exception, message saying

"Retrieving the COM class factory for component with CLSID {3FA7DEB3-6438-101B-ACC1-00AA00423326} failed due to the following error: ". When i browsed through the net, i came to know that, Beginning in Exchange 2007 Beta 2 and Outlook 2007 Beta 2, CDO 1.2.1 will no longer be provided as a part of the install of the product. As a result, there is functionality missing that many applications depend upon. CDO 1.2.1 is a package providing access to Outlook-compatible objects through a COM-based API.

So if you are using CDO library to access Outlook objects and have plans to move your application to Office 2007. You need to install the CDO 1.2.1 separately. You can dowload the CDO 1.2.1 from here

Happy programming