Well, that code won’t work the way I want

I was trying to write an automated test recently that would verify our cache optimize functionality. Specifically, the test first needed to copy a .onecache file that was in need of optimization to the machine which would run the test.

So I added a quick routine to copy the file from our repository to the client machine. It was pretty straightforward. The only catch was that I needed to check to see if the folder that holds the cache had already been created or not. If OneNote had previously been started, it would exist, otherwise I would need to create it myself before I copied the file. The code I had was not working and here it is:

string pathToLocalCache = <<invoke our task library to get the cache folder, typically c:\users\johngui\appdata\local\microsoft\onenote\14.0
string localCacheFile = pathToLocalCache + "\\OneNoteOfflineCache.onecache";



//the directory should already exist
if (!System.IO.Directory.Exists(localCacheFile))
this.Log.Warning("Cache folder at {0} not found. Creating…", pathToLocalCache);


Can you spot the error? I was always getting the Warning in my log that the Cache folder was not being found.

The mistake, once I read through what I wrote, was pretty easy to spot. I was passing the path to the file to the command to see if the Directory existed:

if (!System.IO.Directory.Exists(localCacheFile))

This is roughly the equivalent of checking to see if a house has a garage by checking to see if a car is at the address. It simply will not work. The fix will be easy as well -just pass the pathToLocalCache string to the Directory.Exists function.

A silly little typo kept me from getting this test going as quickly as I wanted. Easy enough to fix, though, and the test should be running very soon!  I’ll cover some of the verifications this test will perform next.

Questions, comments, concerns and criticisms always welcome,


Comments (2)

  1. mprobins says:

    string localCacheFile = Path.Combine(pathToLocalCache, "OneNoteOfflineCache.onecache");