Directory.Exists SUCKS!

Alright so I am trying to detect if a directory named "C:\\A&B" exists and low and behold, Directory.Exists will not work. Why you ask Well, when I step through the code it changes the string's value to "C:\\AB". Apparently Microsoft thought it would be a good idea to just remove any abnormal characters. To work around this, I just get the directories last modified time and if it exists it returns something otherwise it throws and exception. How do I just discard this IOException


Answer this question

Directory.Exists SUCKS!

  • watch is

    to discard the IOException, use a try catch block:

     

    try

    {

       //your stuff

    }

    catch (IOException ex)

    {

       //handle it

    }

     

    however I am unable to reproduce the problem, I've been able to do it just fine. Something wrong in your code somewhere :-)



  • Nitesh Ambastha

    Catch it and ignore it. You don't have to actually do anything in the catch block.

    try {
    //something that throws IOException
    } catch (IOException ex) {
    }

    GL,
    Jasmine


  • BradyGuy

    Directory.Exists work perfectly, the condition that this method check is so simple that must work. What is giving you a problem is a special character '&' for C#. You need to either concatenate 'at' sign '@' with your string or replace and character with two and's & --> &&. But i beleive that will work with your example also.

  • albidochon

    The line:

    Console.WriteLine(System.IO.Directory.Exists("C:\\A&B"));

    works perfectly fine for me. (I've tried it both with & without the directory existing)

    Where is it removing the "&" (Note that in many places in the Windows UI an "&" means "underscore the next character to indicated that it's a hotkey", and you have to write "C:\\A&&B" to get what you want.)



  • Directory.Exists SUCKS!