Archive for January 2013

Unicode and __FILE__

__LINE__ and __FILE__ allow your code to access its own line number and source file name and is pretty much intended for debugging purposes.

Funny thing is, in a unicode world, __FILE__ stubbornly remains good old fashioned 8-bit char, Read more

“No To All” – and XCopy

So, you use Xcopy to backup the contents of one hard drive onto another, new, hard drive. It’s gonna take a while and you leave it running.

Xcopy  /s  /c <source>\*.*

When you return you find there was a power outage (or, in my case, I missed one of the power management settings that the stupid computer decided to invoke).

How do you restart copying ? You could run the same command again but now you will be prompted on every file that had already and simply saying yes-to-all means re-copying the same files again – in my case that’d waste at least 6 hours.

What you want is to be able to say “no to all” when asked if you wan’t to over-write but alas like so many programs no-one seems to ever supply this as an option.

Fortunately there is non-obvious solution for XCopy – add the /D option which

Copies files changed on or after the specified date.If no date is given, copies only those files whose source time is newer than the destination time.

Since any previously copied file will have a matching date time the file will not be over-written.

That was lucky, but I’ve used other command line utilities in the past where I’ve wanted a “no-to-all” and couldn’t find an obvious workaround. Until I tripped over this link which had one solution that made be laugh. Simply create a text file full of “n” characters, each followed carriage return (you could knock up a quickie app to do this in 5 mins – 4 mins for Visual Studio to start up (why is it so slow these days) and 1 minute to write/compile/run). Then simply pipe that text file into the offending command line app, for example if there hadn’t been another option for XCopy then:

Xcopy  /s  /c <source>\*.*  <MyTextFileFullOfNs.Txt

Funny ? Well, yes ! But it’d get the job done when no other solutions were forthcoming.

How To Delete Duplicate Resources

Well, I guess I wasn’t paying attention when I added an image to my solution that had the same name, but different file extension, to an existing image. Of course, Visual Studio gives the image a default name equal to the file name without extension, but doesn’t bother checking for duplicates names before adding it. Only after it is added does it bother checking, at which point it gives a rather pointless error message:



“There is already another resource with the name,,,” isn’t very helpful when there is no offer of a solution to the problem ! You can no longer access the resources in the normal way and hence can’t delete the offending duplicate.

You need to open your application’s resources.resx directly as text, then search through all the sections that begin  “<data name=” and delete the duplicate – with luck it will be the one most recently added and appear towards the end of the file.

resxJust delete the offending <data> section (s).

Lies, Damn Lies, and Microsoft Function Names

When creating custom controls you may want your control to not do certain things when it is being used inside the Visual Studio forms designer. Perhaps in the constructor for your control you want to connect to a database, or maybe create a window, or whatever but only want that to happen at run time.

Behold, there appears to be just the function you want – DesignMode – a nice little boolean you can query in your code.

But beware ! This little doozey is incorrectly named. It should be named “TheDesignerCanAccessMyPropertiesAndIHaveAnISiteAndImInDesignMode”. You see,

  1. DesignMode isn’t set until an ISite is assigned to your control (its this ISite that actually provides the DesignMode value) and occurs in the background some time after your constructor is called.
  2. It only applies directly to the controls that the designer can modify at that time. For example, if you open a form that has a control (‘A’) on it, both will be in DesignMode. However, if control ‘A’ has any child controls, those child controls will not be in DesignMode ! The way to look at it is the designer can access properties for the form and control ‘A’ – so those will (at some point) have working DesignMode properties, however the designer doesn’t directly access control ‘B’s properties at all so control ‘B’s DesignMode will always false.

Oh cripes ! What are we going to do ?

Forms and User Controls

Well, assuming you can live with point #2 above and if you are deriving from System.Windows.UserControl or System.Windows.Form then perhaps you could override OnCreateControl and do your initialisation there instead, since their Site property will have been initialised before then and hence DesignMode will be reliable. To be honest, OnCreateControl is often a better place to perform initialisation than the constructor.

What if you can’t live with point #2 ? Well, you could rely on whoever designed Control ‘B’ to recurse up through the Parent properties until you either find one with DesignMode set to true, or until you reach the top (Parent = null) in which case you you aren’t design mode.


Typically you derive from System.ComponentModel.Component when designing components you can drop on a form but in fact live on that panel at the bottom of the designer (where you often find your SystemMenu, Timers, or such like]. The clue to the answer is my alternative name for Micro$oft’s DesignMode i.e.


 You can override the Site property and when it is being set to a non-null value you can query the supplied ISite‘s DesignMode and perform your initialisation based on that value.

As for getting around point #2 … well, good luck with that one !

Must Be A Better Way!?

Are there any other ways ? Well, there is System.ComponentModel.LicenseManager.UsageMode (yes, thats right, LicenseManager!) which you can query to see if it’s value is set to “runtime” but if MSDN is to believed (and why not, it is known to be a 100% reliable source of information isn’t it [snigger]) it requires at least Vista SP2. Oh, and it might only work in a constructor – it certainly doesn’t work inside events.

Must Be A Better Way Mk.II ?

Assuming for a moment you only program using VisualStudio under Windows and not some other environment (such as the excellent Mono). Well, you could get the name of the currently running process and see if it is called “devenv” [Hack alert, awooga! awooga!]. One line of code to fix it in all situations and you aren’t limited to forms, user controls and components, any class can do it:



Which, of course, will work right up to the point where Micro$oft find out we are doing this and go and change it in the next Visual Studio service pack just to annoy us.

Must Be A Better Way Mk.III ?

Yep, there is just one more way we can tackle this problem:

Petition Micro$oft to give us a DesignMode property that tells us when we are in design mode !. Sheesh !

But don’t hold your breath …..

Custom Fields in MS Word

Often in a document you need to include text that is repeated throughout a document and just _know_ someone will want to change them later. Or perhaps you have a document template and want to use custom fields that the template user can fill in.
Enter custom fields.
Inserting these fields is a little non-obvious as they are buried under the title “quick parts”.

This brings up a dialog – but we aren’t quite there yet ! You need to select “DocProperty” in the left hand list after which the field properties appear containing the names of the fields. As can be seen, a number of fields have already been defined.


Having finally managed to find these fields wouldn’t it be fantastic if there was an [Add] button so you could add your own field for your own text. But if Micro$oft did that life would be too easy and we can’t have that ! Indeed, even the mighty Google couldn’t find an answer !

Thats because creation is buried in a completely different part of the program. Select the main menu button (top-left of application), select “Prepare” and select “Properties”


When you do this, an additional panel appears just below the menu bar but above the actual document page.


The more observant among you may recognize some of these fields – such as “Author” – from the insert-field dialog.So we are on the right track ! Click on the “Document Properties” drop-down and select “Advanced Properties” and another tabbed dialog appears.

Honest, we are getting close now ! Select the “Custom” tag and voilà ! Enter a name for your field and a value and press the [Add] button.


The field will now appear in the insert-field dialog. (Note that as well as text, you can instead specify a number, date or even Yes/No).

It is apparently possible to have your custom field appear on the document properties panel. For example, every organized organization will number their documents so wouldn’t it make sense to have a “Document number” field [hint-hint Micro$oft - please add one!].Well, get out your cheque book (heh, showing me age!) and pay Micro$oft for InfoPath and apparently you can design your document information panel.