Simple Thread Safe Singleton
This is simple but should work:
public sealed class Singleton
{
private static Singleton _instance = new Singleton();
private Singleton() { }
public static Singleton Instance
{
get
{
return _instance;
}
}
}
As the CLR synchronises static constructors it is thread safe. Also static constructors are not called until the types is accessed to you have lazy instantiation (use nesting if there are other static methods).
Run this program as program as an administrator
Posted by John in Vista, Visual Studio on June 29, 2007
Vista will try and run most programs without admin rights even if you are signed on as an admin. Some programs such as Visual Studio need admin rights so it is worth setting them to always run as administrator.
Confusingly, looking at a shortcut’s properties, the compatibility tab has an option to “Run this program as an administrator”. This option is greyed out:

To set the shortcut to run as administrator go to the shortcut tab > click on Advanced… > check Run as administrator:

Macro to Set Outlook’s Master Category List
I upgrade, move, or reinstall the OS on my PCs fairly often so setting a master category list with a macro is useful:
Public Sub ResetCategories()
DeleteAllCategories
CreateCategory “! goals”, 1, 0
CreateCategory “! objectives”, 2, 0
CreateCategory “! projects”, 3, 0
CreateCategory “@ anywhere”, 4, 0
CreateCategory “@ computer”, 5, 0
CreateCategory “@ email”, 6, 0
CreateCategory “@ errands”, 7, 0
CreateCategory “@ home”, 8, 0
CreateCategory “@ office”, 9, 0
CreateCategory “@ phone”, 10, 0
CreateCategory “1:1″, 11, 0
CreateCategory “2 inbox”, 23, 1
CreateCategory “2 someday maybe”, 24, 0
CreateCategory “2 waiting for”, 19, 0
CreateCategory “meeting”, 22, 0
CreateCategory “holiday”, 17, 0
CreateCategory “social”, 18, 0
CreateCategory “STS”, 20, 0
CreateCategory “travelling”, 21, 0
CreateCategory “cards”, 25, 0
End Sub
Private Sub DeleteAllCategories()
Dim objNameSpace As NameSpace
Dim objCategory As Category
Set objNameSpace = Application.GetNamespace(“MAPI”)
If objNameSpace.Categories.Count > 0 Then
For Each objCategory In objNameSpace.Categories
objNameSpace.Categories.Remove (objCategory.CategoryID)
Next
End If
Set objCategory = Nothing
Set objNameSpace = Nothing
End Sub
Private Sub CreateCategory(strCategoryName As String, intColor As Integer, intKey As Integer)
Dim objNameSpace As NameSpace
Dim objCategory As Category
Set objNameSpace = Application.GetNamespace(“MAPI”)
If intColor > 25 Then intColor = -1
objNameSpace.Categories.Add strCategoryName, intColor, intKey
Set objCategory = Nothing
Set objNameSpace = Nothing
End Sub
Outlook Item to Task Macro
The following macros convert outlook items to tasks with different categories. Based on this blog post which in turn is based on this one.
- The item body is copied to the task body and the item itself is added as an attachment.
- If multiple items are selected, multiple tasks will be created.
- Created tasks are left open for editing.
- I am in two minds about deleting the item after the task is created and have decided against it at the moment.
- It appears that OL 2007 does not allow shortcut keys to be assigned to macros
.
Public Sub CreateInboxTaskFromItem()
CreateCatagorisedTaskFromItem (“2 inbox”)
End Sub
Public Sub CreateWaitingTaskFromItem()
CreateCatagorisedTaskFromItem (“2 waiting for”)
End Sub
Public Sub CreateSomedayTaskFromItem()
CreateCatagorisedTaskFromItem (“2 someday maybe”)
End Sub
Public Sub CreateTaskFromItem()
CreateCatagorisedTaskFromItem (“”)
End Sub
Private Sub CreateCatagorisedTaskFromItem(catagory As String)
Dim olTask As Outlook.TaskItem
Dim olItem As Object
Dim olExp As Outlook.Explorer
Dim fldCurrent As Outlook.MAPIFolder
Dim olApp As Outlook.Application
Set olApp = Outlook.CreateObject(“Outlook.Application”)
Set olExp = olApp.ActiveExplorer
Set fldCurrent = olExp.CurrentFolder
Dim cntSelection As Integer
cntSelection = olExp.Selection.Count
For i = 1 To cntSelection
Set olTask = olApp.CreateItem(olTaskItem)
Set olItem = olExp.Selection.Item(i)
olTask.Attachments.Add olItem
olTask.Body = olTask.Body + olItem.Body
If catagory = “2 waiting for” Then
olTask.Subject = olItem.SenderName & “: ” & olItem.Subject
Else
olTask.Subject = olItem.Subject
End If
olTask.Categories = catagory
‘olItem.Delete
olTask.Display
olTask.Save
Next
End Sub
Vista Install Blank Screen
Trying to install Vista on a machine and getting a blank screen after one of the reboots?
This could be caused by an incompatible display driver. I solved this problem by:
- Reboot the machine and hit F8 to get the Windows boot menu.
- Select Safe Mode with Networking.
- Vista will boot then will complain that it cannot continue with the installation in Safe Mode. Do not hit OK!
- Press Shift + F10 to get a command window.
- Run Device Manager by entering ‘devmgmt.msc’.
- Right click the video card and select Update Driver.
- Choose ‘Select a Driver’.
- Select ‘Choose from a list’.
- Click on the generic VGA driver and hit Next to install it.
- Close the wizard, close the Device Manager, close the command prompt.
- Click OK on the installation message and Vista will reboot.
- Finish the install, create a restore point, then install the correct video driver.
How to Use InternalsVisibleTo
Posted by John in CSharp, Code, Visual Studio on June 18, 2007
VS2005 defaults new classes to internal rather than public (although it appears that Orcas defaults to public). This is good as it reduces the public interface of your assembly and it encourages you to actually think about whether a class needs to be public.
You can allow other assemblies access to your internal types and members though, by using the InternalsVisibleTo attribute. This is especially useful for allowing your test assembly to access your internal classes.
This attribute can only be applied to strongly named assemblies.
[assembly:InternalsVisibleTo("TestAssembly, PublicKey=TestAssembly’s public key")]
The attribute requires the assembly name and the public key. To retrieve the public key use sn.exe. Open a visual studio command prompt and enter:
sn.exe -Tp AssemblyPath
Alternatively if you use an alternate file explorer you could set up a macro.
This page has an unspecified potential security risk.
With Vista (and probably XP and IE7) right clicking on a zip file on a network share brings up an Internet Explorer dialogue:
This page has an unspecified potential security risk. Would you like to continue?
It appears that the OS thinks that your network share is in the Internet Zone rather than the Intranet Zone. This is easily fixed by:
- Control Panel > Internet Options > Security.
- Select Local intranet > click Sites > Advanced.
- Type your server name into the textbox (e.g. If the server is called FileServer just type FileServer) and click Add.
- Click Close > OK > OK.
How to Give an Assembly a Strong Name
Posted by John in CSharp, Code, Visual Studio on June 14, 2007
To Strongly name an assembly you need to associate it with a public and private key. This can be either done by the developer when they compile the assembly (signing) or by at a later stage for instance by the QA department (delayed signing).
Public and private keys can be associated with code signing certificates, generated using sn.exe, or they can be generated from within Visual Studio.
Open the project properties for the assembly you want to sign and check ‘sign the assembly’ on the signing tab. To create a key file, select New from the drop down and follow the directions or Browse for an existing key file. If you create a key file it is worth moving it from the bin directory as you should be using it across multiple projects.
Once compiled you will have a signed assembly with a strong name.
User Commands in Xplorer2
When I am doing anything more complicated than simple file browsing I use Xplorer2 rather than the windows file browser.
One of the features of Xplorer2 is the ability to run commands on files you have selected.
These are the commands I currently have set up:
- VS Cmd
$”C:\Program Files\Microsoft Visual Studio 8\SDK\v2.0\Bin\sdkvars.bat”
Sets the console to act like a Visual Studio command prompt - Get Public Key
$sn -Tp “$F”
Gets an assemblies public key (requires vs Cmd to be run first). - DiffMerge Files
>”C:\Program Files\SourceGear\DiffMerge\DiffMerge.exe” “$F” “$G”
Uses Source Gear’s DiffMerge to diff files from each pane. - DiffMerge Folders
>”C:\Program Files\SourceGear\DiffMerge\DiffMerge.exe” “$P” “$I”
Uses Source Gear’s DiffMerge to diff folders from each pane (although Xplorer2 does this well enough itself).