コガネブログ

平日更新を目標に Unity や C#、Visual Studio、ReSharper などのゲーム開発アレコレを書いていきます

【C#】フォルダが空かどうか調べる関数

ソースコード

using System.IO;

public static class DirectoryUtils
{
    public static bool IsEmptyDirectory( string path )
    {
        if ( !Directory.Exists( path ) ) return false;

        try
        {
            var entries = Directory.GetFileSystemEntries( path );
            return entries == null || entries.Length == 0;
        }
        catch
        {
            return false;
        }
    }
}

使い方

if ( DirectoryUtils.IsEmptyDirectory( "@"C:\temp" ) )
{
}