Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/301.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C#-检查unix文件夹是否存在_C#_Unix_Sharpssh - Fatal编程技术网

C#-检查unix文件夹是否存在

C#-检查unix文件夹是否存在,c#,unix,sharpssh,C#,Unix,Sharpssh,我需要使用SharpSsh从我的C#应用程序验证unix文件夹是否存在。 我想试试这个: SshExec.RunCommand("-d " + folder) 但不管文件夹是否存在,结果总是“2”。我可以使用以下方法实现某些功能: Sftp.GetFileList(folder) 但不希望这样做,因为此文件夹可能包含大量文件,并且在检索所有文件时会导致延迟,这并不优雅。 有什么想法吗 编辑: 我试过这个: string folder = "/foldername"; string resul

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

使用

运行命令(“ls-la”+文件夹)

SshExec.RunCommand(“ls”+文件夹)

如果文件夹未隐藏且不需要大小信息


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

执行bash脚本怎么样:
if[-d{folder}];然后回应“是”;否则回应“否”;fi
?是这样使用的:SshExec.RunCommand(如果[-d{folder}];那么echo“yes”;否则echo“no”;fi?)是的,没错,您应该返回yes或no。OP说:“文件夹可能包含许多文件”。如果你在那个文件夹里有几十万个文件。。。这不是很有效。你传递文件夹名,这样它就会过滤掉它。如果它不存在,那么“ls-la foldername”结果应该是一行或者什么都没有。这个人说:“-a Include目录条目,其名称以点(.)开头。”所以,
ls-a
对于每个文件名平均约20个字符的100k文件,将返回大约2MB的数据。这不是ls-la。它是ls-la foldername。如果你不写foldername,你会得到全部。用你的名字过滤