Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 9020489
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 16, 20262026-06-16T05:01:45+00:00 2026-06-16T05:01:45+00:00

I want to check if a folder exists and if not then create it.

  • 0

I want to check if a folder exists and if not then create it. But I don’t know if the path supplied will even valid. When the path is not valid the following happens.

string path = "this is an invalid path";

if (!Directory.Exists(path))
    Directory.CreateDirectory(path); //Exception thrown here

If you supply an invalid path, it will throw a DirectoryNotFoundException exception.

How can I stop this exception from occurring? I don’t want to use a try-catch. I want to detect that this exception will occur even before the exception happens.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-16T05:01:46+00:00Added an answer on June 16, 2026 at 5:01 am

    The explanation for the failure of your code is that the path is invalid. The documentation says:

    DirectoryNotFoundException

    The specified path is invalid (for example, it is on an
    unmapped drive).

    Trying to predict in advance whether or not a directory can be created is a devil of a job. You’d need to account for security, OS name rules and limits, file system name rules and limits, and whether or not drives are mapped. And probably lots more concerns. I would not contemplate re-implementing what the system provides for free.

    In any case, whilst you can call Directory.Exists, you do still need to allow for an exception being thrown. If the file system changes between the call to Directory.Exists and the subsequent call to Directory.CreateDirectory, then an exception will be raised. For example, if another process creates the directory that you are trying to create. Granted, this is a rather unlikely event, but it’s perfectly possible.

    In summary, the best option, by a distance, is to catch the exception. As the well known saying goes, it’s better to ask for forgiveness than to ask for permission.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I want to check if a folder exists in sharepoint. But it could be
I want to check, whether a file exists or not in the /assets/ folder.
I don't want check if an item with a value exists in the arraylist,
I want to make a cron job that checks if a folder exists, and
I want to check if a specific apk is present in system app folder.
If I want to check how many files there are in a folder (where
I want to check which port is used, when the port is not explicitly
I want to check checkbox like $('#riderCheck').attr('checked','true') on some condition. but the problem is
I want to check whether any Zip file is present on a specified path.
I created this check: ' Check if windows or mac if not objFSO.FolderExists(\\Client\C$\.) then

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.