Category Archives: C# Snippets

Split a Camel-Cased word into its components

I needed to split a camel-cased word into it’s component parts, and found the following extension method to help out.  It works pretty well. public static string SplitCamelCase( this string str ) { return Regex.Replace( Regex.Replace( str, @"(\P{Ll})(\P{Ll}\p{Ll})", "$1 $2" … Continue reading

Posted in .NET, C# Snippets, Extension Methods, Uncategorized | Leave a comment

Apostrophe In RowFilter

According to ASP.NET Resources, there is a problem when using apostrophes in a RowFilter expression in a DataView. The fix seems to be to double the apostrophes (“O”Mally”) rather than escaping them (“O\’Mally”)

Posted in C# Snippets | Comments Off

Iterate through enum values

A quick example of how to iterate through the values in an enum… enum myEnum { one, two, three, four, five, }; foreach (int i in Enum.GetValues(typeof(myEnum ))) { myEnum myItem = (myEnum )Enum.ToObject(typeof(myEnum ), i); System.Diagnostics.Debug.WriteLine(myItem.ToString()); }

Posted in .NET, C# Snippets, General, Software | Comments Off

Determine if an assembly is a debug or release build

*This code originally came from another blog, although it was written in VB over there. It has lost some elegance in the translation, but I needed a very quick solution this afternoon, and I needed it in C#, so here … Continue reading

Posted in .NET, C# Snippets, Software | 1 Comment

Sending an HTTP POST request to a web server

ASCIIEncoding encoding = new ASCIIEncoding(); string post_url = “http://172.16.24.44/postdata.php”; string poststring = “monkeys=blue\&trousers=black”; byte[] data = encoding.GetBytes(poststring); HttpWebRequest httpRequest = (HttpWebRequest)WebRequest.Create(post_url); httpRequest.Method = “POST”; httpRequest.ContentType=”application/x-www-form-urlencoded”; httpRequest.ContentLength = data.Length; Stream newStream=httpRequest.GetRequestStream(); newStream.Write(data,0,data.Length); newStream.Close(); HttpWebResponse response = (HttpWebResponse)httpRequest.GetResponse (); Stream receiveStream = … Continue reading

Posted in C# Snippets | Comments Off

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