none
C# see if files exist in SFTP directory RRS feed

  • Question

  • 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 :(

    Wednesday, January 9, 2019 9:57 PM

All replies

  • Maybe it would be helpful to see the exception:

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

    Wednesday, January 9, 2019 10:01 PM
  • Or maybe the Url is wrong:

    string ftpURL = "sftp://sftp.iehp.org//Directory1//Director2//*FileBLahBlah*.csv";

    But please tell us the exception you are getting, it is helpful to see what is going wrong.

    Greetings, Chris

    Wednesday, January 9, 2019 10:04 PM
  • i put now:

                catch (Exception e)
                {
                    Console.WriteLine(e);
                    Console.WriteLine("FILES DONT EXIST");
                }

    it says:

    System.UriFormatException: Invalid URI: The format of the URI could not be determined.
       at System.Uri.CreateThis(String uri, Boolean dontEscape, UriKind uriKind)
       at System.Net.WebRequest.Create(String requestUriString)

    at FTP_TEST.Program.Main() in c:\users\dude\documents\visual studio 2015\Projects\FTP_TEST\FTP_TEST\Program.cs:line 20

    not sure, it says though its line 20, which is on:

    FtpWebRequest ftpRequest = (FtpWebRequest)WebRequest.Create(ftpURL);

    but not sure why its not liking this?

    Wednesday, January 9, 2019 10:16 PM
  • thank you for your help, i did try adding "SFTP" in front of the step like you suggested, however i get this message:

    System.NotSupportedException: The URI prefix is not recognized.
       at System.Net.WebRequest.Create(Uri requestUri, Boolean useUriBase)
       at FTP_TEST.Program.Main() in c:\users\sqladmin1\documents\visual studio 2015\Projects\FTP_TEST\FTP_TEST\Program.cs:line 20


    Wednesday, January 9, 2019 10:17 PM
  • It doesn't like ftpURL or better said the value...

    It looks like the URL format is wrong.

    Have you tried to add sftp:// to the url like in my second post?

    Or maybe you can set a breakpoint to this line and tell us whats the value of ftpURL.

    Wednesday, January 9, 2019 10:19 PM
  • I'm not sure if sftp is supported :/
    Wednesday, January 9, 2019 10:23 PM
  • Hey thanks again for helping me out DerChris88, so i did try adding SFTP in front, example: 

    sftp://sftp.iehp.org//Directory1//Director2//*FileBLahBlah*.csv";

    however i get the following error:

    System.NotSupportedException: The URI prefix is not recognized.
       at System.Net.WebRequest.Create(Uri requestUri, Boolean useUriBase)
       at FTP_TEST.Program.Main() in c:\users\sqladmin1\documents\visual studio 2015\Projects\FTP_TEST\FTP_TEST\Program.cs:line 20
    FILES DONT EXIST

    so still not right :(, i also tried sftp.iehp.org//directory1 etc. but still same error. i tried just a single / and not use double // but nothing :(

    Wednesday, January 9, 2019 10:26 PM
  • I'm sorry but I think sftp is not working with this class. You may need a library for it like ssh.net
    Wednesday, January 9, 2019 10:27 PM
  • Hi StevenlB,

    Thank you for posting here.

    For your question, you could try the examples in the link below.

    https://www.example-code.com/csharp/sftp.asp

    Best Regards,

    Wendy

    Note: This response contains a reference to a third-party World Wide Web site. Microsoft is providing this information as a convenience to you.
    Microsoft does not control these sites and has not tested any software or information found on these sites; Therefore, Microsoft cannot make any representations regarding the quality, safety, or suitability of any software or information found there.
    There are inherent dangers in the use of any software found on the Internet, and Microsoft cautions you to make sure that you completely understand the risk before retrieving any software from the Internet.


    MSDN Community Support
    Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.

    • Proposed as answer by DerChris88 Thursday, January 10, 2019 8:57 AM
    Thursday, January 10, 2019 8:14 AM
    Moderator