Cum Grano Salis

Spot The Bug 1: Pocket PC

(Doh! I had a mistake in the code.... Fixed it now. Was supposed to be !Directory.Exists) 

The following code executes at the beginning of a Pocket PC application run. It makes sure that the settings folder exists for that application.

using System;

using System.Collections.Generic;

using System.Text;

using System.IO;

 

namespace ConsoleApplication6

{

       class Program

       {

              static void Main(string[] args)

              {

                     if (!Directory.Exists(Config.SettingsDirectory))

                     {

                           Directory.CreateDirectory(Config.SettingsDirectory);

                     }

              }

       }

}

 

Things to ignore: Process race conditions, file system lock issues.

Published Thursday, December 21, 2006 10:48 AM by Shahar
Filed under:

Comment Notification

If you would like to receive an email when updates are made to this post, please register here

Subscribe to this post's comments using RSS

Comments

 

Dave Crawford said:

Well, seems that it would throw an exception when the Directory.CreateDirectory call is made, in that the it is  called only if the directory for the settings already exists.

The "correct" version might be something like this:

if (!Directory.Exists(Config.SettingsDirectory))

                    {

                          Directory.CreateDirectory(Config.SettingsDirectory);

                    }

DC

December 21, 2006 2:14 PM
 

DarronJ said:

Is it really this simple? Is this creating a directory "if" it exists, rather than if it does not?

December 21, 2006 2:16 PM
 

Darron said:

All potential outcomes are bad for this code. If the directory exists, the code would try to create it again and get an exception. If the directory did not exist, the program would continue to run without having it's settings directory.

A healthy dose of test driven development woud stop this type of error dead in its tracks.

December 21, 2006 2:36 PM
 

jmanning said:

1) you don't get an exception if the dir already exists - that's one of the reasons why this bug can be hard to catch, since if the dir already exists, the code works fine.

2) this is a good reason to just call Directory.CreateDirectory and not bother with the if check, since there's a chance of screwing up said if check.  This is one of the reasons it doesn't throw if the dir already exists, so you can do exactly that.

December 21, 2006 2:59 PM
 

Shahar said:

My mistake. :)

The bug you guys saw is a bug in the example, not the real question.

I fixed it now. It's !Directory.Exists().

Apologies.

December 21, 2006 3:07 PM
 

JamesCurran said:

Well, as jmanning points out, it should be just

static void Main(string[] args)

{

        Directory.CreateDirectory(Config.SettingsDirectory);

}

Otherwise, what do we know about Config.SettingsDirectory?  (Google reports that this is the only page on the internet that contains "Config.SettingsDirectory")

December 22, 2006 1:55 PM
 

Shahar said:

Config.SettingsDirectory just returns where the folder containing the settings for the application is located at.

December 22, 2006 5:15 PM
 

LuisBE said:

Escaping the backslahes in Config.SettingsDirectory?

December 26, 2006 3:11 AM

Leave a Comment

(required) 
(optional)
(required) 

  
Enter Code Here: Required
Submit

© 2009 Microsoft Corporation. All rights reserved. Terms of Use  |  Trademarks  |  Privacy Statement
Microsoft
Page view tracker