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 9290295
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 18, 20262026-06-18T20:23:10+00:00 2026-06-18T20:23:10+00:00

Given a possible full file path, I’ll example with C:\dir\otherDir\possiblefile I’d like to know

  • 0

Given a possible full file path, I’ll example with C:\dir\otherDir\possiblefile I’d like to know of a good approach to finding out whether

C:\dir\otherDir\possiblefile File

or
C:\dir\otherDir Directory

exist. I don’t want to create folders, but I want to create the file if it does not exists.
The file may have an extension or not.
I want to accomplish something like this:

enter image description here

I came up with a solution, but it is a little bit overkill, in my opinion. There should be a simple way of doing it.

Here’s my code:

// Let's example with C:\dir\otherDir\possiblefile
private bool CheckFile(string filename)
{
    // 1) check if file exists
    if (File.Exists(filename))
    {
        // C:\dir\otherDir\possiblefile -> ok
        return true;
    }

    // 2) since the file may not have an extension, check for a directory
    if (Directory.Exists(filename))
    {
        // possiblefile is a directory, not a file!
        //throw new Exception("A file was expected but a directory was found");
        return false;
    }

    // 3) Go "up" in file tree
    // C:\dir\otherDir
    int separatorIndex = filename.LastIndexOf(Path.DirectorySeparatorChar);
    filename = filename.Substring(0, separatorIndex);

    // 4) Check if parent directory exists
    if (Directory.Exists(filename))
    {
        // C:\dir\otherDir\ exists -> ok
        return true;
    }

    // C:\dir\otherDir not found
    //throw new Exception("Neither file not directory were found");
    return false;
}

Any suggestions?

  • 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-18T20:23:12+00:00Added an answer on June 18, 2026 at 8:23 pm

    Your steps 3 and 4 can be replaced by:

    if (Directory.Exists(Path.GetDirectoryName(filename)))
    {
        return true;
    }
    

    That is not only shorter, but will return the right value for paths containing Path.AltDirectorySeparatorChar such as C:/dir/otherDir.

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

Sidebar

Related Questions

Given URL of an archive (e.g. a zip file) Full name (including path) of
Possible Duplicate: <input type='file'> IE Gives full path, FF gives only filename (or directory
Given a FILE*, is it possible to determine the underlying type? That is, is
is it possible to extract 'some' properties from a given file without using System.IO.FileInfo
I want a file selector which gives me the path.Like a file selector selects
So I wrote this function that is given possible numbers, and it has to
So, my idea is quite simple: print all of the possible combinations given X
given an n*m matrix with the possible values of 1, 2 and null: .
Possible Duplicate: substring algorithm Given two strings, A and B, how to find the
Given a certain set of parameters is it possible to stop a set of

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.