Quantcast
Channel: System.Net.FtpClient
Viewing all 741 articles
Browse latest View live

New Post: Download all files from a FTP Directory

$
0
0
May be too late to reply now, but may be helpful for others who search for the same scenario:
using System.Net.FtpClient;

public class FtpUpload
{
    public void UploadFileToFtp
    {
        using (var ftpClient = new FtpClient())
        {
            var destPath = "MyDirectory"
            var sourcePath = @"C:\Files to Upload\MyFile.zip";

            ftpClient.Host = "ftp.schiffhauer.com";
            ftpClient.Credentials = new NetworkCredential("chris", "letmein");

            ftpClient.Connect();

            using (var fileStream = File.OpenRead(sourcePath))
            using (var ftpStream = ftpClient.OpenWrite(string.Format("{0}/{1}", destPath, Path.GetFileName(sourcePath))))
            {
                var buffer = new byte[8 * 1024];
                int count;
                while ((count = fileStream.Read(buffer, 0, buffer.Length)) > 0)
                {
                    ftpStream.Write(buffer, 0, count);
                }
            }
        }
    }
}
I have found that on here

Created Unassigned: Getting a An attempt was made to access a socket in a way forbidden by its access permissions when connection to a clustered FTP server [387]

$
0
0
Getting a "An attempt was made to access a socket in a way forbidden by its access permissions" when connection to a clustered FTP server. I am downloading the files.

I have doing a large amount of processing of files on a FTP server. The FTP server is a cluster of servers which round robin connect on the host name.

At random points I am getting an exception of "An attempt was made to access a socket in a way forbidden by its access permissions".

I am wondering if its something to do with it being clustered arrangement. Note this is connections to a Linux vsftp server and this is a mutli-threaded process but the FTP parts happens on one thread only.

I am using the latest source of System.Net.FtpClient.

For each transfer I am:

```
public byte[] DownloadFileFromFtp(string remoteFile, string volumeServer)
{
using (MemoryStream resultsStream = new MemoryStream())
using (FtpClient ftpClient = new FtpClient())
{
ftpClient.EnableThreadSafeDataConnections = true;
ftpClient.StaleDataCheck = false;

ftpClient.Host = volumeServer;
ftpClient.Credentials = new System.Net.NetworkCredential(FtpUserName, FtpPassword);
ftpClient.EncryptionMode = FtpEncryptionMode.None;
ftpClient.ValidateCertificate += new FtpSslValidation(OnValidateCertificate);
ftpClient.Connect();

try
{
using (var ftpStream = ftpClient.OpenRead(remoteFile, FtpDataType.Binary))
{
var buffer = new byte[8 * 1024];
int count;
while ((count = ftpStream.Read(buffer, 0, buffer.Length)) > 0)
{
resultsStream.Write(buffer, 0, count);
}
}
return resultsStream.ToArray();
}
catch (Exception ex)
{
throw new Exception("Download FTP file from server", ex);
}
}
}
```

If you need any more info let me know.

New Post: Can it be used to connect to SFTP server?

$
0
0
Hi,
I have inherited some code from another team.
they have used this library to connect to FTP servers.
However, the requirement is to connect to SFTP server.
I was wondering if this can be used or not.
if NO, can somebody advise alternatives.

Regards
Praveen.

New Post: Can it be used to connect to SFTP server?

$
0
0
Yes it can. Set EncryptionMode = FtpEncryptionMode.Explicit and supply a security certificate if needed.

Ed

New Post: Can it be used to connect to SFTP server?

$
0
0
Careful, SFTP != FTPS. This project doesn't not do SFTP, there are SSH libraries here on codeplex that can do SFTP.

New Post: Can it be used to connect to SFTP server?

$
0
0
Hi All,
Thank you for your respective replies.
After posting this, I manually searched through the code and got the vibe that this is no SFTP implementation.
I searched for alternatives and got the RENSI SSHNET library.
After using that I was able to connect to the SFTP server.

Regards
Praveen.

New Post: Get accurate modified & created times

$
0
0
Hi,

I am writing a autocleaner and implemented FtpClient. Now I am running into problems when I am trying to compare Modified or Created DateTimes. It looks like FtpClient does not get any correct times from the FTP Server (FileZilla Server), but instead the default DateTime: 01-Jan-01 12:00:00 AM.

How can I get the accurate DateTimes? Here is some Source Code:
// Setup Client
            FtpClient client = new FtpClient();
            FTP.setupClient(servername, port, username, password, encryption, ref client);

            // Get all content
            List<FtpListItem> listFTPItems = client.GetListing(path, FtpListOption.Recursive | FtpListOption.ForceList).ToList();
            List<String> filePathsToDelete = new List<string>();

            if (timeAttribute == HM.timeAttributes.creationDate)
            {
                foreach (var item in listFTPItems)
                {
                    Console.WriteLine("DEBUG:");
                    Console.WriteLine((DateTime.Now - item.Created.ToLocalTime()).Days);
                    if (item.Type == FtpFileSystemObjectType.File && (DateTime.Now - item.Created).Days > deleteOlderThanXDays)
                    {
                        filePathsToDelete.Add(item.FullName);
                    }
                }
            }
            else if (timeAttribute == HM.timeAttributes.lastWriteDate)
            {
                foreach (var item in listFTPItems)
                {
                    if (item.Type == FtpFileSystemObjectType.File && (DateTime.Now - item.Modified).Days > deleteOlderThanXDays)
                    {
                        filePathsToDelete.Add(item.FullName);
                    }
                }
            }

            // Dateien löschen
            foreach (var item in filePathsToDelete)
            {
                client.DeleteFile(item);
            }

New Post: item.Modified or item.Created not returning values


Commented Unassigned: Null reference exception in GetObjectInfo [385]

$
0
0

Null exception is throwed by the FtpClient.GetObjectInfo method calling the FtpListItem.Parse method being path = null:

return FtpListItem.Parse(null, info, Capabilities);

To fix it, is needed a null checking in the "remove globbing/wildcar from path" code;

// remove globbing/wildcard from path
if (path != null && path.GetFtpFileName().Contains("*")) {
path = path.GetFtpDirectoryName();
}

Seems that other parts of the FtpListItem.Parse method need to check null values too.
Comments: Above change will not work - FtpListItem returned back will have a NULL FullName. To fix this properly, tow changes need to made: - In FtpClient.cs file in GetObjectInfo method line 1828 need to be changed from: ``` return FtpListItem.Parse(null, info, Capabilities); ``` to ``` return FtpListItem.Parse(path, info, Capabilities); ``` - In FtpListItem.cs in Parse method line 261 needs to be changed from: ``` item.FullName = path.GetFtpPath(item.Name); //.GetFtpPathWithoutGlob(); ``` to ``` item.FullName = path.GetFtpPath(); //.GetFtpPathWithoutGlob(); ```

New Post: Download ZIP Files

$
0
0
Hello, I'm downloading ZIP file from a ftp server using your library.

The file is downloaded, seem to have the correct size, but non the less it is corrupted and can't be opened.
Neither by the process in which it's running or the windows explorer.

The files i'm downloading are smaller than 200KB

This is my code.
 try
            {
                using (Stream s = _ftpClient.OpenRead(sourceFileFullPath))
                {
                    if (s.Length != 0)
                    {

                        using (FileStream fileStream = System.IO.File.Create(fileFullPath, (int)s.Length))
                        {

                            var bytesInStream = new byte[s.Length];
                            s.Read(bytesInStream, 0, (int)bytesInStream.Length);

                            fileStream.Write(bytesInStream, 0, bytesInStream.Length);

                            newFile = new FileInfo(fileFullPath);
                        }
                    }

                    //s.Close();

                }
            }
            catch (IOException ioException)
            {
                Console.WriteLine("IOException: " + ioException.Message);
            }
            catch (SocketException socketException)
            {
                Console.WriteLine("SocketException: " + socketException.Message);
            }
            catch (FtpCommandException ftpCommandException)
            {
                Console.WriteLine("FtpCommandException: " + ftpCommandException.Message);
            }
            catch (Exception)
            {
                throw;
            }
Did anyone else had this issue?

New Post: Download ZIP Files

$
0
0
Use a loop and a buffer of about 8192 instead of trying to read the whole file in one swoop like you're doing. It doesn't work, it's not even a good idea to blindly allocate a buffer the size of the file like that. See the various other threads where doing that has bitten people.

Created Unassigned: TLSv1.2 support [388]

$
0
0
Currently it will only work with SSLv3, witch is no longer secure due to the POODLE vulnerability.

Edited Unassigned: TLSv1.2 support [388]

$
0
0
Currently it will only work with SSLv3 witch is no longer secure due to the POODLE vulnerability.

Commented Unassigned: TLSv1.2 support [388]

$
0
0
Currently it will only work with SSLv3 witch is no longer secure due to the POODLE vulnerability.
Comments: "it supports SSL/TLS connections for command and data channels and parsing various file listing formats." Guessing it support 1.2, must be the software am using causing the issue.

Commented Unassigned: TLSv1.2 support [388]

$
0
0
Currently it will only work with SSLv3 witch is no longer secure due to the POODLE vulnerability.
Comments: Change the AuthenticateAsClient line to use SslProtocols.Default in place of the other protocols specified in FtpSocketStream.ActivateEncryption and see if it uses the version you're expecting.

Commented Unassigned: TLSv1.2 support [388]

$
0
0
Currently it will only work with SSLv3 witch is no longer secure due to the POODLE vulnerability.
Comments: If you're not targeting .net 4.5 it won't matter; 4.0 only supports TLS 1.0, SSL v2 and SSL v3. This code uses SslStream under the hood.

Source code checked in, #8763c79a7bcd4f1cb18e91ec61fd2460dc844169

$
0
0
Use SslProtocols.Default so the most recent version of SSL or TLS supported by the version of the .net the code was built against is used by SslStream

Source code checked in, #da6e0709928dc54b2972a563d77caacbe2a68edb

$
0
0
Added sublimetext configuration option for OmniSharp; tested autocompletion in test project

New Post: AutoPassive not working?

$
0
0
I've written a simple ftp client that worked fine in testing, then failed in production.

After looking at what the TraceListener provided, I tried setting FtpDataConnectionType explicitly, rather than using the default.

We found that these did not work:
  • AutoPassive
  • AutoActive
  • EPSV
  • EPRT
But these did:
  • PASV
  • EPSV
  • PORT
And that confused me. I thought that AutoPassive was supposed to try EPSV, and then if it failed, try PASV. But in my app, it just fails.

Am I expected to handle the exception and retry in my code?

New Post: Documentation available other than example source?

$
0
0
The website mentions a CHM file, but installing the package using NuGet in VS I can't find any introductory documentation explaining the basic operation, options, etc.. Wading through the examples does a help, but does explain things fully.

Did I miss something? :-)

Otherwise, nice software, it works !! Thanks and keep it going ....
Viewing all 741 articles
Browse latest View live


<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>