Geeks With Blogs
Annie Bougie C# Code Just a collection of some general coding to solve some common problems
Sucky MSTest and the "WaitAll for multiple handles on a STA thread is not supported" Error
If you are doing any multi-threading and are using MSTest, you will probably run across this error. For some reason, MSTest by default runs in STA threading mode. WTF, Microsoft! Why so stuck in the old COM world? When I run the same test using NUnit, I don't have this problem. Unfortunately, my company has chosen MSTest, so I have a lot of testing problems. NUnit is so much better, IMO. After determining that I wasn't referencing any unmanaged code that would flip the thread into STA, which can ......

Posted On Wednesday, April 7, 2010 5:59 AM

Encrypt and Decrypt Strings
Here is a quick solution to encrypting and decrypting passwords, or any strings, easily. Write a test that runs the GetNewKey method to generate a unique key. I put the key right in the class here for demonstration purposes, but you can store it anyplace. Write another test to encrypt your password so you know the value to save in your configuration settings, and you're all set. Just remember not to save your test with the clear text password in it! public static class Helper { private static string ......

Posted On Tuesday, March 2, 2010 4:41 AM

Using Delegates in Rhino Mocks With the .Do Statement
I use Rhino Mocks pretty much exclusively for mocking and testing. I find that there really isn't anything that I've wanted to do that I haven't been able to figure out how to accomplish with Rhino Mocks. What is everyone else using? I'd like to know so I can take a look. Anyway, as you know, with Expect.Call we can return property values, return values from functions, throw an error, etc. Sometimes just returning a static value isn't going to work, or it's just easier to give it some simple code ......

Posted On Friday, August 21, 2009 3:51 AM

Create an ADODB Recordset in code from XML
Recently I have been doing some work on an older system using ADODB recordsets. Every now and then we still have to support these old systems. I wanted to create an open recordset in code on the .NET side to pass to the older system, but ADODB really wants an open database connection in order to open a recordset. My first obvious attempt was to create the recordset object, then create the field objects and add them to the recordset. I could create the recordset this way, but I couldn't open it, and ......

Posted On Thursday, July 2, 2009 12:54 PM

SharePoint Adventures - Reading an Excel Spreadsheet From A Stream
When I needed to read an Excel spreadsheet from a SharePoint site, it seemed like a simple enough request. Previously, whenever I needed to open an Excel file, I used an OleDb connection with the following connection string: string connectionString = @"Provider=Microsoft.ACE.OL... Data Source={0};Persist Security Info=False; Extended Properties=""Excel 12.0;HDR=YES"""; connectionString = string.Format(connectionStr... filePath); Of course, you can't open the file from the SharePoint site this ......

Posted On Thursday, January 29, 2009 10:40 AM

InternalsVisibleTo with Strongly-Type Assemblies
For testing, I often use the InternalsVisibleTo for my unit tests. It looks like this: [assembly: InternalsVisibleTo("MyProje... When the assembly is strongly typed, though, this simple statement will not work, and we get the following message: Error: "Strong-name signed assemblies must specify a public key in their InternalsVisibleTo declarations" To fix this, you must do two things: You must sign the test assembly, and you must include the public key in the InternalsVisibleTo statement. ......

Posted On Wednesday, October 1, 2008 2:18 AM

Matching and Grouping Regular Expressions using Regex in C#
Regular expressions are one of those things that you may not need very often, but when you do, it really solves the problem. The usage of the Regex static methods may seem difficult, but they're pretty easy. Being able to use regular expressions readily will help you quickly write some code that would take you many hours longer by parsing the strings. I've compiled some code that uses the basic features of the Regex class. This article only covers the coding side of it. In order for it to work, you ......

Posted On Wednesday, September 17, 2008 4:09 PM

Recursive Search for a Single File
I recently had to write a routine to search for the location of a particular file in a directory tree. I could find lots of code to create an array of all the filenames in the tree, but I needed to just retrieve the location of the filename I was looking for. So, here's the code: public static string SearchForSingleFile(string path, string fileName) { string searchPath = Path.Combine(path, fileName); if (File.Exists(searchPath)) { return searchPath; } string foundFile = string.Empty; string[] dirs ......

Posted On Saturday, August 16, 2008 2:18 AM

Copyright © Annie Bougie | Powered by: GeeksWithBlogs.net