-2

I'm trying to write a program that will automatically download the latest client files from from their FTP site. I can successfully access the site using an FTP client, but when I try to do so programmatically, I run into errors. I've tried multiple FTP clients and I can't get any of them to work.

For example, I use WinSCP to access the site, and it has this handy feature where it generates the code necessary to connect to the current site. Here's an example:

// Set up session options
SessionOptions sessionOptions = new SessionOptions
{
    Protocol = Protocol.Ftp,
    HostName = "ftp.site.com",
    PortNumber = 21,
    UserName = "username",
    Password = "password",
};

using (Session session = new Session())
{
    // Connect
    session.Open(sessionOptions);

    // Your code
}

I can copy that generated code verbatim, put it into a c# program, and I get the error

An attempt was made to access a socket in a way forbidden by its access permissions x.x.x.x:21

Is there something I'm doing wrong, or does the client have something configured on their FTP site that prevents what I'm trying to do?


After something changed on my end, I don't know exactly what, the errors went away and I can now successfully access the FTP site.

greenjaed
  • 589
  • 8
  • 20
  • 2
    Possible duplicate of [an attempt was made to access a socket in a way forbbiden by it's access permissions. why?](https://stackoverflow.com/questions/10461257/an-attempt-was-made-to-access-a-socket-in-a-way-forbbiden-by-its-access-permiss) – Darendal Jun 27 '17 at 23:00
  • For what its worth; I would try the built in classes or FluentFTP. I've never heard of or tried WinSCP – BradleyDotNET Jun 27 '17 at 23:06
  • WinSCP allows access to SFTP sites. That's the only reason I can see for using it with C#. – hatchet - done with SOverflow Jun 27 '17 at 23:08
  • I actually tried FluentFTP first, but was getting the same error. The reason I chose WinSCP as an example was to show that my code wasn't the problem. – greenjaed Jun 27 '17 at 23:29
  • *"I can successfully access the site using an FTP client"* - Are you running the FTP client on **same** machine as your code? Show us a verbose log file of both the FTP client and your code. Once you have the WinSCP code, the best is if you show us WinSCP code log file (`Session.SessionLogPath`) and a corresponding WinSCP GUI log file. – Martin Prikryl Jun 28 '17 at 04:49
  • 1
    Welp, I don't know what changed, but now it's working and I didn't do anything differently. The only things that were different were I tried (unsuccessfully) to run the program with elevated privileges and there was an update pushed by IT. This is super frustrating. So, do I close this question or do I update it and leave it open? – greenjaed Jun 28 '17 at 19:08

2 Answers2

0

I've had success with WebClient of System.Net

using (WebClient client = new WebClient())
{
    client.Credentials = new NetworkCredential("username@somedomain.com", "my-pwd");
    client.DownloadFile("ftp://11.22.33.44/myfile.txt", @"C:\myfile.txt");
}

Make sure your app has access to the output location (@"C:\myfile.txt" in my example).

Telos
  • 1,039
  • 1
  • 12
  • 20
  • I've tried that and get `Unable to connect to the remote server`. I get similar errors when I try to use the related FtpWebRequest. – greenjaed Jun 27 '17 at 23:34
  • @greenjaed - Does your IDE offer any more details on that exception? – Telos Jun 27 '17 at 23:48
  • The only thing in the inner exception is `'($exception).Response.ContentType' threw an exception of type 'System.NotImplementedException'` – greenjaed Jun 28 '17 at 00:16
0

You could always try the FtpWebRequest class from System.Net:

string url = "11.22.33.44";
string user = "username@somedomain.com";
string pwd = "my-pwd";
string fileName = @"C:\myfile.txt";

var request = (FtpWebRequest)WebRequest.Create(String.Format("ftp://{0}/{1}", url, filename));
request.Method = WebRequestMethods.Ftp.DownloadFile;
request.Credentials = new NetworkCredential(user, pwd);

using (var response = (FtpWebResponse)request.GetResponse())
using (var responseStream = response.GetResponseStream())
using (var targetStream = new FileStream(targetPath, FileMode.Create, FileAccess.Write))
        if (responseStream != null)
            responseStream.CopyTo(targetStream);
Parrish Husband
  • 3,148
  • 18
  • 40