C#查看SFTP目录中是否存在文件 [英] C# see if files exist in SFTP directory

查看:925
本文介绍了C#查看SFTP目录中是否存在文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

您好,


我的代码如下:


 static void Main()
{
string userName =" TEST" ;;
string password =" TEST" ;;
string ftpURL =" sftp.iehp.org//Directory1//Director2 // * FileBLahBlah * .csv" ;;

try
{
FtpWebRequest ftpRequest =(FtpWebRequest)WebRequest.Create(ftpURL);
ftpRequest.Method = WebRequestMethods.Ftp.DownloadFile;
ftpRequest.Credentials = new NetworkCredential(userName,password);

使用(FtpWebResponse ftpResponse =(FtpWebResponse)ftpRequest.GetResponse())
{
Console.WriteLine(" FILES EXIST");
}
}
catch
{
Console.WriteLine(" FILES DONT EXIST");
}

Console.ReadLine();

}

然而它似乎没有用,因为我知道该目录中有一个文件,我测试过用户名和密码,但是有一个名为TestFileBlahblah01092019.csv的文件,但它永远不会出现在控制台中。"文件存在",我知道i
我错过了这么简单的东西......任何帮助:(

解决方案

查看异常可能会有所帮助:

 catch (例外e)
{
Console.WriteLine(e);
}


Hello,

i have the following code below:

        static void Main()
        {
            string userName = "TEST";
            string password = "TEST";
            string ftpURL = "sftp.iehp.org//Directory1//Director2//*FileBLahBlah*.csv";
            
            try
            {
                FtpWebRequest ftpRequest = (FtpWebRequest)WebRequest.Create(ftpURL);
                ftpRequest.Method = WebRequestMethods.Ftp.DownloadFile;
                ftpRequest.Credentials = new NetworkCredential(userName, password);

                using (FtpWebResponse ftpResponse = (FtpWebResponse)ftpRequest.GetResponse())
                {
                    Console.WriteLine("FILES EXIST");
                }
            }
            catch
            {
                Console.WriteLine("FILES DONT EXIST");
            }

            Console.ReadLine();

        }

however it doesnt seem to work because i know there is a file in that directory, i tested the username and password, however there is a file called TestFileBlahblah01092019.csv however it never shows up in the console.. "Files Exist", i know i am missing something so simple... any help :(

解决方案

Maybe it would be helpful to see the exception:

            catch(Exception e)
            {
                Console.WriteLine(e);
            }


这篇关于C#查看SFTP目录中是否存在文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆