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

Extending the usefulness of an OLD snowblower

Frankenblower vs Mother Earth- season 2.

SNOW 1  –  FRANKENBLOWER 0

I made it through last winter with the Frankenblower as is.  “As is” from what I can tell is 3rd or 4th hand.  As you can see from the pic, it’s pretty damn old, and appears to be made out of metal salvaged from a WWII heavy crusier…  But, the neighbor I bought it from is an engineer extraordinaire, so  despite its obvious age and jerry-rigging, I’m pretty confident a strong machine.

A couple weeks ago, we get about 16″ of heavy wet snow.  Now, the Honda engine started fairly decently despite sitting neglected almost a year, and I really didnt do any maintenance to it at all, my bad.  Only problem with starting was that the choke lever got a little bent in one part so I had to trace the movement along until I found a lever that appeared like it wanted to slide a certain way.  O_o   Well, I slid the lerver in that certain way, yanked the starter cord, and voilla, I had a running engine.  Impressive. 

Yes, this is the extent of my troubleshooting skills, trace and fiddle.  It works with programming and apparently with small engines as well.

But things just got worse.  Me and Frankenblower got our butts kicked by this storm.  

 

PROBLEMS

1) My right tire had completely deflated over the year… (Mental note, don’t be a lazy sod and at least check on the snowblower once during the summer).  So, you can picture this heavy beast continuously pushing to the right, with me fighting it to stay straight, all in heavy wet snow.

2) The chute kept CLOGGING!! wtf!?! didnt happen at all last winter.  was there no what snow last year?? Possibly.  

SOLUTIONS 

1) have a deflated tire??  don’t want to invest in a heavy duty air pump yet?

I bought this baby off of amazon.  For a small portable air compressor it really kicks @ss. It’s cheap ($20), lightweight, small and comes with a long cable, maybe 15′.  Only drawback is it needs to plug into a car cigarette lighter outlet.  Not a problem, and since it was cheap, I consider it a very easy experiment.  It filled the completely deflated tire in less than a minute, and topped off the left one as well.  There is no air tank, so it has to be running to pump air – it won’t be a problem for the intended use.  Not sure if I can somehow rig it up to my spray gun for working on models though.  That would just be the ultimate victory.

Hopefully the compressor lasts a while, and I get some use on the car and bycicle tires as well.  It comes with some nozzles for inflating kids toys, footballs etc as well… no chance to use them yet

      http://www.amazon.com/gp/product/B000642GAW/ref=oss_product

Also, just bit the bullet and bought some snowblower tire chains as well.  The defeat at the hands of the last storm put me in such a fould mood that I am willing to ‘splurge’ and give the chains a try – from what the neighbor says it makes a huge difference, especially on slopes… which I have

      http://www.amazon.com/gp/product/B001M1WQBM/ref=oss_product 

2) Snow chute keeps getting clogged with snow?  They say spray the chute with WD40, or PAM cooking spray of all things.  Can’t say for sure, but will give it a try during the next storm.

 

No longer using Somee.com’s free web hosting, but it wasn’t so bad.

So, I signed up for a plan on m6.net that allows me to host 12 different sites for $7.75 a month.  Everything from one host? much easier for me, and a good price too.

So, Somee.com’s free service was ok, the biggest thing I didn’t like was their banner ads enforced on the top of the page.  Other than that there was uptime that constantly hovered around 92%, something I probably could’ve achieved hosting at home on a Virtual Machine server, or an old desktop.    

The other odd thing was the fact that they will shut off your site if the average of all the files in your webroot is over 150kb per file.  Add a few movies or pdfs, and yolu’re pretty much not going to stay live on their free site long.

BUT – AS ALWAYS – any free web hosting service is a great thing, and Somee.com was ok if you can live in with those 3 drawbacks.

 

The 150kb policy email:

 

"The service: website mojobudgie.somee.com,
Has been disabled for the following reason: Website's average, per 
request bandwidth have exceeded a limit that is set for the website 
hosted with a free hosting package! Although we do not limit the file 
size that can be uploaded or downloaded from your website, we set a 
limit of 150KByte on average file size that can be downloaded from the 
website hosted with free package. The average is calculated every 
several hours for all files together, including *.html, *.asp, *.aspx,  
*.jpg, *.png, *.gif, *.mpg and other text and media files. Your website 
is scheduled to start again in about 12 hours, or you can start it 
manually  from our control panel any time. But if you will not take 
actions to decrease the average file size that is sent from your website
 it will be stopped again and you are allowed to start it 5 times only. 
You can also switch to any another hosting package where such 
maintenance is not performed.! "

 

 

A sampling of the uptime graph

 

www.mojobudgie.com
----------------------------------------------------------------------
date          uptime       dns   connect   request      ttfb      ttlb

2010-10-22     98.97     0.159     0.206     0.206     3.087     3.090
2010-10-21    100.00     0.279     0.326     0.326     3.083     3.087
2010-10-20     94.95     0.104     0.333     0.333     3.442     3.443
2010-10-19     94.95     0.155     0.417     0.417     3.142     3.146
2010-10-18     95.88     0.062     0.263     0.263     3.222     3.224
2010-10-17    100.00     0.116     0.164     0.164     2.982     2.986
2010-10-16     49.48     0.155     0.202     0.202     1.675     1.677
2010-10-15     95.92     0.050     0.131     0.131     3.330     3.382
2010-10-14     94.95     0.045     0.121     0.121     3.375     3.378
2010-10-13     98.96     0.115     0.288     0.288     2.863     2.865
2010-10-12     95.92     0.111     0.279     0.279     2.780     2.784
2010-10-11     77.00     0.432     0.601     0.601     2.772     2.775
2010-10-10     95.96     0.036     0.205     0.205     3.039     3.042
2010-10-09     96.94     0.100     0.302     0.302     3.037     3.097
----------------------------------------------------------------------
minimum        49.48     0.036     0.121     0.121     1.675     1.677
maximum       100.00     0.432     0.601     0.601     3.442     3.443
average        92.13     0.137     0.274     0.274     2.988     2.998

 

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.

 

BlogEngine.net and Somee.com’s free ASP.Net 4, MSSQL 2008 hosting

After months of being too busy, I got a chance to rehost the blog.  This time I’m trying out a free ASP.net & Microsoft SQL Server hosting service from Somee.com.  I also switched from the XML powered version of BlogEngine.net to use a SQL server backend.  I needed to do a find and replace, removing all [dbo]. and GO references in the installation script, however, after that the BlogEngine setup SQL worked flawlesly and within a few moments I had a partially working site.

the hosting specs;

when you setup your Somee account you choose the .Net flavor you want 2, 3.5 or 4

  • you can also choose MS SQL2005, or 2008R2.  Both are Express editions (a big reason I dont want to use Brinkster.com, as they only allow Access databases)
  • you get 150mb of storage space
  • you get 15mb of SQL data storage, and 20mb of SQL log storage
  • you get a FTP login (another big reason I dont want to use Brinkster.com)
  • you can host on your own domain (again, Brinkster only allows subdomain hosting)
  • they do place a banner of ads at the top
  • and a link back to somee.com down the bottom.  (Though Brinkster has no ads, so far I’m ok with it)

The site management tool is basic and as a free customer you only have a few things you can do; custom errors, report on storage space, setup SQL login, backup your databases, do restores… things like this.
You cant set granular file permissions or create pw protected directories.

Still getting a few odd errors saving certain types of data into BE’s tinyMCE editor windows now and then. will work it out and post the fix.

M6.Net review, and “to self host or not to self host”

“Hi – I saw your blog posting about WinHost and M6. I’ve been running my own web server for more than a couple of years but costs of hardward and bandwidth are (as you say) forcing me to frugally review my arrangements – I wanted to know your thoughts on M6.net now that you’ve been with them a few months. Thanks – I look forward to hearing from you”

This was a question I got a couple days ago from a reader, thought I’d share the response with all.

So, yea, this site is hosted on M6.net – on their basic plan and at their cheapest rate.  At the time I signed up I took a chance and bought the 2 year contract (which is VERY uncharacteristic of me, as I like to sample many hosting co’s).  I took the 2 year plan so I could get the lowest per month rate, as they offer multi year discounts.  Now in all honesty I was really making a big deal out of a little deal, because the amount of money I saved over 2 years wasn’t so great, maybe $50, and  I’m sure I could blow that on a beer night with some buddies real easy..  But with hosting and me, it’s all about the principle.

So I paid $90 for 2 years of hosting, for 2 sites on my one plan – with some gigabit of space.. and 1 MSSQL 2k8 db (which I haven’t really used yet).  That winds up being…. hmm $1.88 per site, per month.  For .Net 3.5, MVC and so forth

Yea, as much as I WANTED to justify macking together some uber small fanless pc bought in parts from Newegg.com with some scrounged frankenstein drives, maybe even with USB to IDE converters… tack it up under a desk and run my MSDN Win2k8 web, and make pretend I love sys admin stuff  (which is a lie, I only like it  when it works)…But, there was no way I could make the math work. 

Here in the Northeast USA kilowatt are like 15 cents sometimes more..

I actually made the following spreadsheet to help visualize my own subjective pros and cons of self hosting;

Now, it shows I’m a total PM & analyst, and a real dork.  But whatever, it helps me sleep at night.  I reviewed the cost & ‘feature set’ (to me this were things like what I can learn, what can annoy me. I gave 1 as the lowest score, and 4 as the highest ) for three self hosting options and two web hosting companies.
The 3 self hosting options were

  1. building a new mini pc like the MSI’s of Foxconns with an Intel Atom 330 dual core 1.6, which I honestly wanted to do…
  2. building a frankenserver out of  a cheap ebay pentium, and
  3. hosting it on the kids imac – running a parallels VM. 

The 2 hosting companies I considered were M6.Net and Mochahost.  I only considered these Asp.net hosts because they allow multiple sites per account, unlike Winhost.com, or PlanetSMB.com which only allow 1 site per account.  Btw, I use to love PlanetSMB and used them for 2 clients… that is until a few days ago.  I’m gonna bitch that one out in a week or two when I chill about their 2 days of f*!@#king downtime.

I digress.  So assuming Mocha and M6 had roughly similar feature sets – .Net 3.5, MVC, SQL2k8, multiple domains, and some gigabyte (will I ever even use 1GB?) of storage and bandwidth, it wound up being the most COST effective, and LEAST STRESS inducing to go for a 2 year deal plan with M6.Net. Mocha was pretty close, as suprisingly was the VM on the iMac, which I am SO glad I didn’t do, I just can’t imagine the amount of crashing it would run into… with all the legos starwars games running as a the kids user. 

My analysis was I would learn more and have way more flexibility with my own server (of any kind), but it would take more of my time and interrupt me at odd times, as well as cost more.  I just thought do I want to deal with this??  No.

Now…. Do I regret going with M6?  Absolutely not. 
While they’ve been running about 99.5% uptime (the stats below show a bad couple weeks, they’re usually better) it’s absolutely fine for what I do.  The support is prompt and courteous when I ask the odd question.  And that price is sure right.. and furthermore it’s just not interfering with my life.

4 out of 5 stars for now.

Typical uptime stats for this site:

www.mojobudgie.com
——————————

—————————————-
date          uptime       dns   connect   request      ttfb      ttlb

2010-04-10    100.00     0.003     0.059     0.059     0.330     0.390
2010-04-09    100.00     0.003     0.065     0.065     0.379     0.442
2010-04-08    100.00     0.003     0.059     0.059     0.332     0.387
2010-04-07    100.00     0.003     0.059     0.059     0.372     0.427
2010-04-06     93.55     0.003     0.061     0.061     0.408     0.468
2010-04-05     93.48     0.003     0.060     0.060     0.396     0.450
2010-04-04    100.00     0.007     0.063     0.063     0.333     0.390
2010-04-03    100.00     0.003     0.058     0.058     0.325     0.384
2010-04-02    100.00     0.004     0.060     0.060     0.382     0.442
2010-04-01    100.00     0.020     0.077     0.077     0.375     0.436
2010-03-31    100.00     0.008     0.064     0.064     0.560     0.617
2010-03-30     96.70     0.010     0.066     0.066     0.383     0.445
2010-03-29    100.00     0.003     0.061     0.061     0.341     0.404
2010-03-28    100.00     0.005     0.062     0.062     0.361     0.487

——————————
—————————————-
minimum        93.48     0.003     0.058     0.058     0.325     0.384
maximum       100.00     0.020     0.077     0.077     0.560     0.617
average        98.84     0.006     0.062     0.063     0.377     0.441

——————————
—–————————-———-