Partager via


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.

Comments

  • Anonymous
    December 21, 2006
    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

  • Anonymous
    December 21, 2006
    Is it really this simple? Is this creating a directory "if" it exists, rather than if it does not?

  • Anonymous
    December 21, 2006
    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.

  • Anonymous
    December 21, 2006
    The comment has been removed

  • Anonymous
    December 21, 2006
    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.

  • Anonymous
    December 22, 2006
    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")

  • Anonymous
    December 22, 2006
    Config.SettingsDirectory just returns where the folder containing the settings for the application is located at.

  • Anonymous
    December 25, 2006
    Escaping the backslahes in Config.SettingsDirectory?