Category Archives: C# Snippets

Catch key presses in a Windows Forms application

Simple enough to do. Set the “KeyPreview” property of the form to “True” Create a KeyUp event handler, eg private void Form1_KeyUp(object sender, System.Windows.Forms.KeyEventArgs e) { if(e.KeyCode == Keys.F12) { // Do something here } } Wire the event handler … Continue reading

Posted in C# Snippets | Comments Off

Sharepoint portal connection strings

How to pull connection string details out of a Sharepoint portal site. using System; using Microsoft.SharePoint.Portal; using Microsoft.SharePoint.Portal.Topology; namespace SPConnStrings { /// /// Summary description for Class1. /// class Class1 { /// /// The main entry point for the application. … Continue reading

Posted in C# Snippets | Comments Off

Sharepoint permissions and web parts

There is a strong argument against doing things like this, but there are times when you just have to bite the bullet and grant full access permissions to certain web parts without having to install them to the GAC. Here’s … Continue reading

Posted in C# Snippets | Comments Off

WMI Out Of Memory issues

I have been working on an automated way of deploying BizTalk assemblies, orchestrations and bindings without having to use NAnt or MSBuild this week. It has thrown up a problem that lots of people must have if they are doing … Continue reading

Posted in BizTalk, C# Snippets | Comments Off

Process.Start and required input

I had an issue with Process.Start today. I was trying to call an executable that requires the user to confirm the action with a keystroke, in this case “y”. As far as I am aware, there is no mechanism within … Continue reading

Posted in C# Snippets | Comments Off

Return multiple values from a method

This falls under the category of “I would have known about this if I was not self-taught, and it would have saved me so much time.” In general, the methods I have written in the past have only been able … Continue reading

Posted in C# Snippets | Comments Off

Web Service Portability (part 2)

Another thing that caught me out today… I deployed a couple of web services, and web form consumers of those services to a “Live” environment today, and was getting some strange behaviour. Because of the limited amount of fault tracking … Continue reading

Posted in C# Snippets | Comments Off

Web Service Portability

When you create an application that relies upon web services, the simplest way to define the location of those web services is by specifying them at design time. The problem, of course, is when you want to deploy a development … Continue reading

Posted in C# Snippets | Comments Off

Delete an object from the IIS metabase

Simple enough to do, as long as you remember you have to remove it from the parent item’s children list. public static void DeleteMetabaseObject(string metabasePath) { try { string deleteParent = metabasePath.Substring(0,metabasePath.LastIndexOf("/")); string deleteName = metabasePath.Substring(metabasePath.LastIndexOf("/") + 1); DirectoryEntry parent … Continue reading

Posted in C# Snippets | Comments Off

Create a new Virtual Directory in IIS via C#

The following method will create a new IIS Virtual Folder object, based on a path and a friendly name. ServerID is the ID of the “Web Site” you want to work with in IIS. VirtualDirName is the name of the … Continue reading

Posted in C# Snippets | Comments Off