Archive

Archive for May, 2011

DNS Suffix Country Codes

May 29th, 2011 Comments off

Have you ever wondered where ‘.bz’ was?

Read more…

Who am I? (Determining the runtime user)

May 29th, 2011 No comments

(One of the last snippets from CodeKeep.)

I’ve got a console app that I am using to test NHibernate.  The app fails to run because I can’t get connected to my local db.  After checking the current user at runtime, I realized there wasn’t any defined user which messes up my db connection. 

Here is a work-around. 

Read more…

Creating a single instance application per machine.

May 29th, 2011 No comments

(Another snippet I originally posted to CodeKeep.)

Read more…

Formatting DateTime inside a GridView

May 29th, 2011 No comments

(Another snippet I originally posted to CodeKeep.)

Format a datetime value within a gridview’s boundfield control.  The default shows MM/dd/yyyy hh:mm:ss.  In my case, I didn’t want/need the time portion.  So…

 

 

<asp:BoundField DataField="timestamp" DataFormatString="{0:MM/dd/yyyy}" HeaderText="Date entered" />

 

 

Reference:

http://forums.microsoft.com/hongkong/ShowPost.aspx?PostID=4003043&SiteID=82

Tags: ,

Reset RichTextBox selection

May 29th, 2011 No comments

(Another snippet I originally posted to CodeKeep.)

After you finish modifying text within a RichTextBox control, you may want to move the cursor to the top of the control.

Code:


richTextBox1.SelectionStart = 0;
richTextBox1.SelectionLength = 0;

 

Modify text (visually) in a RichTextBox

May 29th, 2011 No comments

(Another snippet I originally posted to CodeKeep.)

I needed to create a RichTextBox with a title, menu path, and description.  Here’s how I made customized the visuals to suit the project.  Title is enlarged and made bold.  Menu is reduced and made italics.  The description is ‘standard.’

Code:

Read more…

Removing items from a typed list

May 29th, 2011 No comments

(Another snippet I originally posted to CodeKeep.)

Code:

 
var myList = new List<string>() { "one", "two", "three" }; 

// print out the original list
foreach (string item in myList)
  Console.WriteLine(item); // one two three

myList.Remove("two");

// print out the new list
foreach (string item in myList)
  Console.WriteLine(item); // one three

 

Notes:

You cannot remove items from a list inside of a foreach.  You can set items to null, but you cannot remove the items.

Tags: , ,

Convert Enum to string

May 29th, 2011 No comments

(Another snippet I originally posted to CodeKeep.)


public enum EFileType
{
    Unknown = –1,
    Test,
    Production
}

...

var fileType = EFileType.Production;
var str = Enum.GetName(typeof(EFileType),fileType); // str == 'Production'
Tags: , ,

Using C# to running command-line utility

May 29th, 2011 No comments

(Another snippet I originally posted to CodeKeep.)

I needed to do a secure file copy to a remote server for an activity report.  I chose to use pscp from PuTTY.

Read more…

Overloading a constructor

May 29th, 2011 No comments

(Another snippet I originally posted to CodeKeep.)

Using ‘this’ can make your code simpler and easier to read. 

Here is a hard way to create a pair of constructors:

Read more…

Temp Files & Folders

May 29th, 2011 No comments

(Another snippet I originally posted to CodeKeep.)

Need the path to the local temp folder or a temp file name to store some runtime junk?

Look no further…

 

var tempPath = System.IO.Path.GetTempPath();
var tempFile = System.IO.Path.GetTempFileName();

Tags: ,

Event Logging

May 29th, 2011 No comments

(Another snippet I originally posted to CodeKeep.)

I’d recommend looking at EntLib 5.0 if you are serious about your event logging.  This is but a simple hack…

Read more…

Tags: , ,

Interface Examples

May 29th, 2011 No comments

(Another snippet I originally posted to CodeKeep.)

Code:


interface IPlural
{
  void Load();
  void Load(ELoadOptions option, int id);
}

interface IMembership
{
  void Save();
  void Delete();
  void Load(int id);
}

Usage:

public class CAddresses : List<CAddress> , IPlural
{
  public void Load()
  {
    // do something
  }

  public void Load(ELoadOptions option, int id)
  {
    // do something
  }
}

Read more…