SSIS脚本任务检查文件夹是否为空



在SSIS脚本任务中,我有下面的代码来检查文件夹是否为空。然后,我想将它是否传递到变量"Dim_File_Count"中,如果成功,则使用优先级约束转移到下一个任务。然而,我的代码一直声明文件夹是空的,即使不是:

public void Main()
{
//string FolderName = Dts.Variables["User::Tech_Dim"].Value.ToString();
if (File.Exists(Dts.Variables["User::Tech_Dim"].Value.ToString())==false)
{
Dts.Variables["User::Dim_File_Count"].Value = 0;
MessageBox.Show("folder empty");
}
else
{
Dts.Variables["User::Dim_File_Count"].Value = 1;
MessageBox.Show("folder is not empty");
}

Dts.TaskResult = (int)ScriptResults.Success;
}

您可以使用Directory类的GetFiles方法的Length属性来检查指定文件夹中是否有任何文件。如果需要搜索子目录,即SearchOption.AllDirectories,则可以使用GetFiles的可选第三个SearchOption参数,默认情况下只检查父文件夹。

if (Directory.GetFiles(Dts.Variables["User::Tech_Dim"].Value.ToString(), "*").Length > 0)
{
Dts.Variables["User::Dim_File_Count"].Value = 0;
MessageBox.Show("folder empty");
}
else
{
Dts.Variables["User::Dim_File_Count"].Value = 1;
MessageBox.Show("folder is not empty");
}

相关内容

最新更新