August 26, 2009

Comparing Constant Strings with String Variables

To avoid problems, you should never compare a string variable with a string constant, because string variables may have a null value, in which case your code will throw a NullPointerException.Here’s an example: String strName = null;strName = getName(); //some call which retrives name from DBif(strName.equals(“Ritesh Patel”)) { // some

Using SetForegroundWindow on Windows Owned by Other Processes

In modern versions of Windows (XP, Vista, and beyond), the API call SetForegroundWindow() will bring the specified window to the foreground only if it’s owned by the calling thread. The following code removes this limitation and provides a workaround: void NewSetForegroundWindow(HWND hWnd){ if (GetForegroundWindow() != hWnd) { DWORD dwMyThreadID =

Using the WebClient Class to Download Data to a File

The following code shows how you can use the WebClient class to obtain data from a URL and save it to a file: System.Net.WebClient client = new System.Net.WebClient();byte [] bytedata = client.DownloadData(url);client.DownloadFile(url,downloadPath);

Send Email Over SMTP Aschronously

To send email over SMTP asynchronously, create a SmtpClient, call its SendAsync method, and add a SendCompleted event handler to your code: //Call the SmtpClient SendAsync method SmtpClient client = new SmtpClient(_smtpServer);client.SendCompleted += new SendCompletedEventHandler(client_SendCompleted);client.SendAsync(message, “Sending..”); When the SendAsync method completes, it will fire the SendCompleted event, where you can