我正在两个位置检查文件。如果位置 1 为空,则代码应检查位置 2。
问题是永远不会满足“files == null”条件。
我需要一种更好的方法来检查文件数组是否为空。
这是我的代码:
files = Directory.GetFiles(fileTargetFolder);
if (files == null) // if no files, check 2nd location
{
files = Directory.GetFiles(fileTargetFolder2);
}
我正在两个位置检查文件。如果位置 1 为空,则代码应检查位置 2。
问题是永远不会满足“files == null”条件。
我需要一种更好的方法来检查文件数组是否为空。
这是我的代码:
files = Directory.GetFiles(fileTargetFolder);
if (files == null) // if no files, check 2nd location
{
files = Directory.GetFiles(fileTargetFolder2);
}
检查长度
if (files.Length==0)
您可以检查Length数组的属性。
if(files.Length == 0)
{
files = Directory.GetFiles(fileTargetFolder2);
}
您可以将 p/invoke 与PathIsDirectoryEmpty 函数一起使用
[DllImport("Shlwapi.dll", EntryPoint = "PathIsDirectoryEmpty")]
[return : MarshalAs(UnmanagedType.Bool)]
public static extern bool IsDirectoryEmplty([MarshalAs(UnmanagedType.LPStr)]string directory);