tags:

views:

1439

answers:

5

I am processing a TreeView of directories and files, users can select either a file, or a directory and then do something with it. This requires me to have a method which performs different actions if they have selected a file, or a directory.

At the moment I am doing something like this to determine if 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 cannot help to feel that there is a better way to do this! But I cannot find any .Net methods which I can ask "Is this path a directory or a file ?".

Any suggestions ?

Thanks!

+9  A: 

How about using these?

File.Exists();
Directory.Exists();
llamaoo7
+17  A: 

from http://bytes.com/topic/c-sharp/answers/248663-how-tell-if-path-file-directory:

// get the file attributes for file or directory
FileAttributes attr = File.GetAttributes(@"c:\Temp");

//detect whether its a directory or file
if((attr & FileAttributes.Directory) == FileAttributes.Directory)
    MessageBox.Show("Its a directory");
else
    MessageBox.Show("Its a file");
Quinn Wilson
+1 This is the better approach and is significantly faster than the solution I have proposed.
Andrew Hare
+1  A: 

The most accurate approach is going to be using some interop code from the shlwapi.dll

    [DllImport(SHLWAPI, CharSet = CharSet.Unicode)]
    [return: MarshalAsAttribute(UnmanagedType.Bool)]
    [ResourceExposure(ResourceScope.None)]
    internal static extern bool PathIsDirectory([MarshalAsAttribute(UnmanagedType.LPWStr), In] string pszPath);

You would then call it like this:

    #region IsDirectory
    /// <summary>
    /// Verifies that a path is a valid directory.
    /// </summary>
    /// <param name="path">The path to verify.</param>
    /// <returns><see langword="true"/> if the path is a valid directory; 
    /// otherwise, <see langword="false"/>.</returns>
    /// <exception cref="T:System.ArgumentNullException">
    /// <para><paramref name="path"/> is <see langword="null"/>.</para>
    /// </exception>
    /// <exception cref="T:System.ArgumentException">
    /// <para><paramref name="path"/> is <see cref="F:System.String.Empty">String.Empty</see>.</para>
    /// </exception>
    public static bool IsDirectory(string path)
    {
        return PathIsDirectory(path);
    }
Scott Dorman
+1  A: 

As an alternative to Directory.Exists(), you can use the File.GetAttributes() method to get the attributes of a file or a directory, so you could create a helper method like this:

    private static bool IsDirectory(string path)
    {
        System.IO.FileAttributes fa = System.IO.File.GetAttributes(path);
        bool isDirectory = false;
        if ((fa & FileAttributes.Directory) != 0)
        {
            isDirectory = true;
        }
        return isDirectory;
    }

You could also consider adding an object to the tag property of the TreeView control when populating the control that contains additional metadata for the item. For instance, you could add a FileInfo object for files and a DirectoryInfo object for directories and then test for the item type in the tag property to save making additional system calls to get that data when clicking on the item.

Michael McCloskey