How To Check If A File Is A Directory
#include <stdio.h>
#include <sys/types.h>
#include <dirent.h>
#include <errno.h>
int isFile(const char* name)
{
DIR* directory = opendir(name);
if(directory != NULL)
{
closedir(directory);
return 0;
}
if(errno == ENOTDIR)
{
return 1;
}
return -1;
}
int main(void)
{
const char* file = "./testFile";
const char* directory = "./";
printf("Is %s a file? %s.\n", file,
((isFile(file) == 1) ? "Yes" : "No"));
printf("Is %s a directory? %s.\n", directory,
((isFile(directory) == 0) ? "Yes" : "No"));
return 0;
}Better way to check if Path is File or Directory?
Read more:

I am processing a directory and a TreeView file. User can select a file or a directory and then do something with it. This requires me to have a method of performing different actions based on the user's choice.
Currently I'm doing something like this to determine whether the path is a file or a directory:
bool bIsFile = false;
bool bIsDirectory = false;
try
{
string[] subfolders = Directory.GetDirectories(strFilePath);
bIsDirectory = true;
bIsFile = false;
}
catch(System.IO.IOException)
{
bIsFolder = false;
bIsFile = true;
}I am processing a directory and a TreeView file. User can select a file or a directory and then do something with it. This requires me to have a method of performing different actions based on the user's choice.
See more:
Currently I'm doing something like this to determine whether the path is a file or a directory:I can't help feeling that there's a better way to do this! I was hoping to find a standard .NET method to handle this, but I couldn't. Does such a method exist, and if not, the simplest means of determining whether a path is a file or directory
Source: https://blebees.com
source https://blebees.com/how-to-check-if-a-file-is-a-directory/
Nhận xét
Đăng nhận xét