2009/11/09

[C#]My simple version of Captcha

This is my own version of Captcha. It's easy and simple. You can improve it with your ideas or needs.

2009/11/06

Allowing only one instance: a simple approach

This is a simple and easy way to do it if your requirement is simple. Otherwise you should use Mutex.
using System.Diagnostics;

private void Form1_Load(object sender, EventArgs e)
{
 try
 {
  string moduleName = Process.GetCurrentProcess().MainModule.ModuleName;
  string processName = Path.GetFileNameWithoutExtension(moduleName);
  Process[] aryProcess = Process.GetProcessesByName(processName);

  if (aryProcess.Length > 1)
  {
   MessageBox.Show("This program is running already!");
   this.Close();
  }

  //...
 }
 catch (Exception ex)
 {
  //...
 }
}
Note: This will check the process based on the filename. It will not work if the filename is different. For example, if you make a copy of your program (let's say FindMP3.exe) and rename it to a different name like FindMP3_1.exe. People can run FindMP3.exe and FindMP3_1.exe at the same time. You can fix this by checking the filename at program startup.

You can find other approaches from here.

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.