2009/10/20

[C#]Simple windows registry manipulation

using Microsoft.Win32;

//Attempt to open the key.
RegistryKey key = Registry.CurrentUser.OpenSubKey(@"Software\Play\WindowPosition");

//The key doesn't exist if it returns null.
if (key == null)
{
//Create & open it.
key = Registry.CurrentUser.CreateSubKey(@"Software\Play\WindowPos");
key.SetValue("PositionX", Location.X);
key.SetValue("PositionY", Location.Y);
}
else
{
//Get value
Location.X = key.GetValue("PositionX");
Location.Y = key.GetValue("PositionY");
}

2009/10/19

GetWords

Name: GetWords
Version: 1.3
Platform: Windows with .Net Framework 2.0 installed
Introduction:
Fetch words from the given article. It will fetch, sort, remove duplicated words. It will ignore the word that less than 3 characters like am, is, as... etc.
How to use:
1.Paste anything you want to parse on the left-hand side textbox.
2.Click the "Get Words!" button, and the result will show on the right-hand side.

2009/10/07

KB: You must call the Bind method before performing this operation.

I got this error message: "You must call the Bind method before performing this operation." when using the UDP protocol to communicate with another application. Here is the simplified code
.......
IPAddress serverIP = IPAddress.Parse("xx.xx.xx.xx");
IPEndPoint serverEP = new IPEndPoint(serverIP, 12345);
UdpClient client = new UdpClient();

//start listening
client.BeginReceive(new AsyncCallback(ReceiveCallback), serverEP); //here causes the problem
//send message to server and wait for response
client.Send(Encoding.UTF8.GetBytes("Hello"), "Hello".Length, serverEP);
.....
After spending so many hours on this issue, I finally figure out what the problem is.

[C#][Winform]Show Confirm window

In ASP.Net you can popup a confirm window easily. See this and this. How to do the same thing in windows form? Here is the easiliest way I know:
if (MessageBox.Show("Are you sure you want to delete it?","Confirm", MessageBoxButtons.YesNo) == DialogResult.Yes)
{
 //Your code here.
}