A very simple C# program to change a registry value

Yet another grace-be-damned solution: I needed a C# .exe file that when run changes the value of one specific registry key to a hard coded value specified within the code of the .exe file.

I make no excuses for the lack of elegance or thoroughness, but pass it on for what it is – a truly basic applet that does one thing, and if for whatever reason it doesn’t succeed it, it will throw a generic error message.  Opposed to a lot of samples I found on the web,  this one  should work as is, and should be fairly extendable- say if you wanted to check for specific errors, or provide confirmation boxes indicating existing registry key values and what it will change to before you run it, and so forth.

If you’re like me, and asked a bunch of incredulous questions such as ‘why do you want to do this?’ then look at the bottom of the post for the dirt.

For those who were in a similar boat (the ‘just get it done’ boat), here’s the code:


using System;
using System.Windows.Forms;
using Microsoft.Win32;

namespace SwitchRegistryKey
{
static class Program
{
static void Main()
{
try
{
RegistryKey myKey = Registry.LocalMachine.OpenSubKey("SOFTWARE\MyTestKey", true);
myKey.SetValue("MyTestSubKey", "3", RegistryValueKind.DWord);
MessageBox.Show("The registry key has been switched","Title Goes Here");
}
catch
{
MessageBox.Show("There was an error switching the registry key","Title Goes Here");
}

}
}
}

  • Registry.LocalMachine means it is looking in ComputerHKEY_LOCAL_MACHINE
  • Make sure you escape your paths as \ inside your C# code.  That is use SOFTWARE\MyTestKey as opposed to SOFTWAREMyTestKey
  • RegistryValueKind : DWord is just one of the types available, such as String.  Make sure you choose the appropriate kind for the key you are updating
  • The try statement only updates an existing key.  If the program cant find that key, or can’t update it for any reason it immediately will throw the MessageBox message in the catch statement.
  • To make a distributable, create a new C# project
  • save this as the program.cs code
  • add a nifty .ico on the project properties page
  • build the program
  • grab the .exe file (with it’s nifty .ico file you added) from your debug folder

So yea – I was approached by someone at work who needed this out ‘today’.  Apparently there was a buggy Office add-in that we rolled out that constantly keeps getting ‘disabled’ when it crashes.  When it’s disabled, there’s a specific key that flips from 3 to 2, and in order to re-enable it for ‘testing purposes’ they wanted to distribute an exe file users could double click from their desktop.

Yea ok, try fixing the buggy addin instead of asking me to compensate with this app- but seeing as it’s the real world, this interim solution will have to make do until the real devs (not some Project Manager) has free time.

A simple batch script to delete Folders older than x


I had a very simple task I wanted automated:

Delete all folders in a directory older than 4 days.

As it was a simple task I was looking for an EQUALLY simple solution to run via a batch script in Windows, and was surprised how many different approaches, comments, solutions and semi-solutions are out there.  And man, just google around and see how many people were writing complicated or infinitely configurable solutions.  Great for them, but really I just want to trash these old folders.

So, even though I could, I didn’t want to install any third party tools (no matter how “COOL”), write a C# program, delve into VBScript – as all this was way more complicated than this job needed.  Basically just wanted to create a one or 2 line .bat file that would delete the folders without any elaborate conditionals on empty folders, file extension filtering…  just a single damn hard coded “nuke the old folders” script.

What I found was that a DOS script really doesn’t seem to provide easy ways to do conditionals around timestamps.  There are a lot of resources on the web where people are attempting to achieve this, and I spent way too much time tracking their progress across multiple threads and blogs.

So I hunkered down and just wrote something with forfiles.  It’s is available on my Windows 2007 server, and was able to handle the nuking in a very simple one line statement.

ForFiles /P C:backups  /D -4 /C “CMD /C if @ISDIR==TRUE echo RD /Q @FILE &RD /Q /S @FILE”

I saved this into a batch file deleteOldBackups.bat and scheduled as a daily system task.

Some notes:

  • you can change the path: /P C:backups
  • set the date interval (here it’s set to delete anything more than 4 days old) : /D -4  
  • Forfiles date functions appear to work off the modified date rather than the created date, but this works fine for me as the nightly batch script that copies folder into C:backups always sets the modified dates for those folders to the date that it was copied.

There are a lot of switches and parameters that you can use to apply conditional logic to. Check out msdn here, http://technet.microsoft.com/en-us/library/cc753551(WS.10).aspx

Photoshop? Gimp?? Nope. Paint.NET!

I was never really a designer, but I always had to use Photoshop in support of web development I was working on.  Over the years, Photoshop (as have most Adobe products) became more and more bloated, slower and complicated to use.  I honestly, can’t even tell you what version of CS it’s on by now.  It simply became too unwieldy for the simple tasks of cropping, resizing, optimizing that I was doing.  About a year or two ago I stumbled across the free Paint.net .

I seriously think most web developers and also probably a good deal of hobby Photoshop user will find this great alternative software handles just about everything you need. Now, since it’s written in .Net it will only work on Microsoft operating systems, furthermore you will need to have the appropriate .Net libraries installed as well, but for most Windows operating systems they are installed by default. 

Since Paint.net leverages the .Net libraries on your PC, the installer is incredibly small (I think its less than 4mb!).

Version 3.5.5 which I’m running has a feature set that I reminds me of Photoshop circa 5 or 6; things like Layers, a very familiar Tool palette, Effects, Filters, History, Lighting adjustments.  Honestly, it would take me longer to list them all, then for you to download and try it!  One downside is that it doesnt have as many filters and effects as Photoshop, but it does appear to have some way to add plugins (haven’ tried though).

The last thing I want to say is that it is SUPER FAST – startup, in use – shutdown.  It’s simple, intuitive and gets the job done with none of the bloat.