我需要使用 SharpSsh 从我的 C# 应用程序中验证是否存在 unix 文件夹。
我想试试这个:
SshExec.RunCommand("-d " + folder)
但无论文件夹是否存在,结果始终为“2”。我可以使用以下方法实现一些东西:
Sftp.GetFileList(folder)
但不要这样做,因为该文件夹可能包含大量文件,并且在检索所有文件时会导致延迟,这并不优雅。
有任何想法吗?
编辑: 我试过这个:
string folder = "/foldername";
string result = sshExec.RunCommand("[ -d " + folder + "] && echo 'true' || echo 'false'");
if (result == "false")
throw new Exception("Directory " + foldername+ " + is not found.");
即使目录存在,字符串 'result' 也设置为 "false\n"。如果我跳过检查,我可以毫无问题地使用该目录。