c# file\directory check

Spunny 366 Reputation points
2022-12-14T14:21:28.49+00:00

Hi,
I need to determine if passed in value of path is for file or directory and then determine if it exist or not.
Example value:
Pattern 1 - c:\ParentFolder\ChildFolder\
Pattern 2 - C:\ParentFolder\ChildFolder\Houston_. (Houston_ClearLake_1.pdf, Houston_ClearLake_2.pdf, Houston_Katy_1)
Pattern 3 - C:\ParentFolder\ChildFolder\Houston_ClearLake*.*

This pattern will be sent as string to webservice to check

  1. if it is folder or file
  2. If this folder\file exist or not
    the function or method should return type(folder or file) and exist as boolean. So, result set should be
    2 columns : 1 for file or folder value and 2nd will be whether it exist or not

How can I do this in c#

Thank You

Developer technologies | C#
{count} votes

3 answers

Sort by: Most helpful
  1. Jack J Jun 25,296 Reputation points
    2022-12-15T05:54:36.197+00:00

    @Spunny , Welcome to Microsoft Q&A, based on your description, you want to make some checking and then return the correspond result.

    I make a code example and you could have a look.

     internal class Program  
        {  
            static void Main(string[] args)  
            {  
                string path1 = "C:\\Temp\\test.txt;  
                string path2 = "C:\\ParentFolder\\ChildFolder";  
                Checkpath check1 = GetInfo(path1);  
                Checkpath check2 = GetInfo(path2);  
      
      
            }  
            static Checkpath GetInfo(string path)  
            {  
                Checkpath checkpath = new Checkpath();  
                try  
                {  
                    FileAttributes attr = File.GetAttributes(path);  
      
                    //detect whether its a directory or file  
                    if ((attr & FileAttributes.Directory) == FileAttributes.Directory)  
                        checkpath.type = Filetype.Dir;  
                    else  
                        checkpath.type = Filetype.File;  
                    checkpath.Ifexists= true;  
                }  
                catch  
                {  
                    bool t = Path.HasExtension(path);  
                    if (t)  
                    {  
                        checkpath.type = Filetype.File;  
                    }  
                    else  
                    {  
                        checkpath.type = Filetype.Dir;  
                    }  
      
                    checkpath.Ifexists = false;   
                }  
                return checkpath;  
            }  
        }  
        public class Checkpath  
        {  
            public bool Ifexists { get; set; }  
      
            public Filetype type { get; set; }  
        }  
      
        public enum Filetype  
        {  
            File = 0,  
            Dir=1  
        }  
    

    Note: I also agree with Viorel-1's advice, the above works in most cases but not in special cases.

    Hope my code could help you.

    Best Regards,
    Jack


  2. Spunny 366 Reputation points
    2022-12-15T18:32:38.057+00:00

    I did following way and it works with all.

    // following options I tried
    //1. I created only ParentFolder and NOT childFolder directory. Then passed @"C:\ParentFolder\ChildFolder\" as value - worked returned as Folder, not exist and path
    // 2. Created sub folder under ParentFolder. Then passed @"C:\ParentFolder\ChildFolder\" as value - worked returned as Folder, true (exist) and path
    // 3. Passed for file type check and existence of it.

    string path = @"C:\ParentFolder\ChildFolder\";
    //string path = @"C:\ParentFolder\ChildFolder\test*.*";
    //string path = @"C:\ParentFolder\ChildFolder\test.txt";

    bool isFolder = System.IO.Path.GetExtension(path) == "";

    string[]? files = null;

    //not a folder, then check for file or files
    if (!isFolder)
    {
    string directory = path.Substring(0, (path.LastIndexOf("\")) + 1);
    string extension = path.Substring((path.LastIndexOf("\") + 1));
    files = Directory.GetFiles(directory, extension);
    }

    // folder or file type

    pragma warning disable CS8602 // Dereference of a possibly null reference.

    Console.WriteLine (isFolder ? "folder" : files.Length > 1 ? "files" : "file");

    pragma warning restore CS8602 // Dereference of a possibly null reference.

    // directory or file exist or not
    Console.WriteLine((isFolder && Directory.Exists(path) ? true : false) || (!isFolder && files.Length > 0 ? true : false));
    //passed in path parameter value
    Console.WriteLine(path);

    Thank you

    0 comments No comments

  3. Karen Payne MVP 35,586 Reputation points Volunteer Moderator
    2022-12-15T20:31:33.033+00:00

    Try the following

    public class Helpers  
    {  
        public static (bool isFolder, bool success) IsFileOrFolder(string path)  
        {  
            try  
            {  
                var attr = File.GetAttributes(path);  
                return attr.HasFlag(FileAttributes.Directory) ? (true, true)! : (false, true)!;  
            }  
            catch (FileNotFoundException)  
            {  
                return (false, false);  
            }  
        }  
    }  
    

    Usage

    var (isFolder, success) = Helpers.IsFileOrFolder("Place what to check here");  
    
    0 comments No comments

Your answer

Answers can be marked as Accepted Answers by the question author, which helps users to know the answer solved the author's problem.