Category Programming

Using .NET to Monitor a Directory for Changes

.NET’s FileSystemWatcher class can soothe the paranoid control freak in all of us by monitoring a specified folder for different types of file system changes.

The Code

using System;
using System.Text;
using System.IO;

namespace FolderWatcher {
  class Watcher {
    static void Main(string[] args) {
      FileSystemWatcher watcher = new FileSystemWatcher(@"D:\test");
      watcher.IncludeSubdirectories = true;
      watcher.Filter = "";
      watcher.Renamed += new RenamedEventHandler(renamed);
      watcher.Deleted += new FileSystemEventHandler(changed);
      watcher.Changed += new FileSystemEventHandler(changed);
      watcher.Created += new FileSystemEventHandler(changed);
      watcher.EnableRaisingEvents = true;

      Console.ReadKey();
   }

    private static void renamed(object sender, RenamedEventArgs…

301 Redirect with ASP .NET

When redirecting URL’s it is very important not to lose all of your search engine mojo. The best way to do this, is by using a 301 redirect which notifies the requester of a permanent URL move.

<script runat="server">
private void Page_Load(object sender, System.EventArgs e) {
  Response.Status = "301 Moved Permanently";
  Response.AddHeader("Location","http://www.newdomain.com");
}
</script>

Should you have to redirect a URL to a new location, using a status 301 redirect is the safest way to do it.

Using #if and [Conditional()] to Keep Debug Code Under Control

The #if directive provides a perfect way to ensure your released executable doesn’t execute any code that was only intended for debug mode.

#if (<symbol>) will return true whenever the symbol being checked has been defined by default, or manually using #define.

#if DEBUG
	//Perform debug only code
#else
	//Release-level code
#endif

Remember, this is only one application of #if, you can use it any time you need to check to see if a symbol has been defined.

You can also use the [Conditional("<symbol>")] attribute…

Sending Email in ASP .NET

I can’t think of too many websites that don’t send automated email. Unfortunately, for ASP .NET programmers, there are several incorrect code snippets online which try to explain how to send email.

Here’s the code I use on the contact form for my personal website which does the trick quite nicely.

/* using System.Net.Mail; */
MailMessage newMail = new MailMessage();
newMail.To.Add(toAddress);
newMail.Subject = subject;
newMail.Body = body;
newMail.From = new MailAddress(fromAddress, name);
newMail.IsBodyHtml = true;

SmtpClient SmtpSender = new SmtpClient();
SmtpSender.Port = 25;…

PDF Download: Why’s (poignant) Guide to Ruby

This is one of the best (and most entertaining) introductions to a programming language that I have ever read. If you are even remotely interested in learning Ruby, _why’s guide is a great place to start.

And, wherever you are _why, thanks for such a fantastic read.

PDF download: Why’s (poignant) Guide to Ruby

Honing Your Programming Skills with Project Euler

There’s nothing quite like that feeling you get when you finally figure out the solution to a problem. Whether it is solving a complex math equation, beating your friend at Tic-Tac-Toe, or even successfully programming the DVR for the first time. As programmers, we are constantly looking for new problems to solve and interesting ways to improve our programming skills.

Project Euler is a programming challenge site that hosts (currently) 272 programming problems of varying difficulty. Users can log in, attempt…

3 Ways to Suck Less in 2010

My favorite bit of advice on becoming a better programmer is to suck less every year. Programming is a very complex field that is constantly changing and requires a great deal of dedication from developers. Since 2010 officially started today, I thought it would be useful to share 3 goals which will help improve the programming skills of anyone who follows them.

My 3 ways to suck less by 2011

  1. Read – stuffing your brain with great programming knowledge will be invaluable to…

Getting Started With Git and TortoiseGit on Windows

Version control is essential to the success of any software project. It provides the ability for multiple developers to work on the same codebase simultaneously and allows projects to be versioned for release. However, a great deal of programmers fail to leverage the great benefits of version control for their personal projects, typically due to laziness or complacency (myself included).

Luckily, for us lazy programmers, Git and TortoiseGit are extremely easy to install and configure on Windows. Now, there’s no…

Working With Custom Time Strings in C#

Let’s say we need to process some external data where the format and contents of the files are out of our control. To make matters worse, what if those same external files store tiemstamps in a foreign format that DateTime.Parse() does not understand?

It would be ill-advised to keep the dates in string format due to the level of robust date processing capabilities present in .NET.

Luckily, .NET provides a nice way to create custom date formats so any date string can…

Climategate Code Analysis Part 2

There are three common issues that have been raised in my previous post that I would like to officially address concerning the CRU’s source code.

If you only get one thing from this post, please get this. I am only making a statement about the research methods of the CRU and trying to show proof that they had the means and intent to falsify data. And, until the CRU’s research results can be verified by a 3rd party, they cannot be trusted.

Here…