none
Directory Exists method return true even the directory is not present RRS feed

  • Question

  • Hello All,

    Directory.Exists(Path.GetDirectoryName(FilePath);

    It  returns true even if the directory is not present.

    Any idea ?

    Monday, November 6, 2017 7:03 AM

All replies

  • Try write simple console program and feed the path you entered to Path.GetDirectoryName() and you'll see. It is spelt out by the documentation if there aren't "path" component in the parameter supplied (i.e.: strings separated by "\\" or "/"), it'll return String.Empty, which is treated as the current directory.

    To test whether FilePath exists, just put the path to Directory.Exists().



    Monday, November 6, 2017 7:20 AM
    Answerer
  • Hi shital diwate, 

    According to your description, I am not sure what format you used to test whether directory exists or not.

      string FilePath = "C:/Users\\v-wezan\\Desktop";
                Directory.Exists(Path.GetDirectoryName(FilePath));

    As cheong00 said that if you use / in your directory path instead of \\, the directory looks like wrong. But /  will replace the volume specifier with value passed into the last call to SetCurrentDirectory for that volume. The directory.exists invoke GetFullPathName function in deep.

    For more details, you could refer to the following link.

    https://stackoverflow.com/questions/4198050/directoryexistsctemp-foo-returns-true-when-directory-doesnt-exist

    If you use the different format to test the whether the directory exists or not, please feel free to contact us. 

    Best Regards,

    Wendy


    MSDN Community Support
    Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.

    Tuesday, November 7, 2017 6:43 AM
    Moderator
  • Hello Thank you guys for the reply.

    I think you are right , the because of Path.GetDirectoryName(FilePath) operation it is getting set to CurrentDirectory , Now what  I did I removed the Path.GetDirectoryName(FilePath) and write my on function to get the directory path from the file path.

    Now it is working , still it in observation for me. Will let you know on same forum if it fail again.

      

    Thursday, November 9, 2017 2:14 PM
  • Hi Shital,

    We are glad you resolved your issue.

    Please mark the answers that helped you resolve your issue either by voting up if they were helpful or by "marking as an answer" if they answered your question.

    If no answer helped you resolve your question but you resolved it on your own, please describe your solution and mark your post describing it as answer.

    That way others know how the problem was resolved when they reach this post in the future, and people that are trying to help know that this has been resolved.

    Thanks,


    My Technet Articles

    If you like this or another reply, vote it up!
    If you think this or another reply answers the original question, mark it or propose it as an answer.


    Mauricio Feijo
    www.mauriciofeijo.com

    Thursday, November 9, 2017 2:20 PM