0

我需要使用 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"。如果我跳过检查,我可以毫无问题地使用该目录。

4

1 回答 1

2

利用

SshExec.RunCommand("ls -la" + 文件夹)

或者

SshExec.RunCommand("ls" + 文件夹)

如果您的文件夹没有隐藏并且您不需要大小信息

如果存在,将使用权限和文件夹信息填充您的输出字符串的廉价命令

于 2012-12-03T16:30:51.557 回答