Created
February 7, 2014 14:55
-
-
Save tugberkugurlu/8864153 to your computer and use it in GitHub Desktop.
C# FtpClient code
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
using System; | |
using System.IO; | |
using System.Net; | |
using System.Net.FtpClient; | |
using System.Threading.Tasks; | |
namespace FtpClientSample | |
{ | |
class Program | |
{ | |
static void Main(string[] args) | |
{ | |
// Install System.Net.FtpClient NuGet package. | |
//string fileToWrite = Path.GetTempFileName(); | |
//Console.WriteLine(fileToWrite); | |
//using (FtpClient client = new FtpClient()) | |
//{ | |
// client.Host = "127.0.0.1"; | |
// client.Credentials = new NetworkCredential("Tugberk", "123456"); | |
// client.Connect(); | |
// bool productsDatExsists = client.FileExists("products.dat"); | |
// using (Stream stream = client.OpenRead("products.dat")) | |
// using (Stream streamToWrite = File.Open(fileToWrite, FileMode.Append)) | |
// { | |
// stream.CopyTo(streamToWrite); | |
// } | |
//} | |
FtpFileManager ftpFileManager = new FtpFileManager("127.0.0.1", "Tugberk", "123456"); | |
string fileName = ftpFileManager.DownloadFileAsync("products2.dat", "foo").Result; | |
Console.WriteLine(fileName); | |
Console.WriteLine("Done"); | |
Console.ReadLine(); | |
} | |
} | |
public class FtpFileManager | |
{ | |
private readonly string host; | |
private readonly string username; | |
private readonly string password; | |
public FtpFileManager(string host, string username, string password) | |
{ | |
this.host = host; | |
this.username = username; | |
this.password = password; | |
} | |
public Task<string> DownloadFileAsync(string fileName) | |
{ | |
return DownloadFileAsync(fileName, "/"); | |
} | |
public async Task<string> DownloadFileAsync(string fileName, string workingDirectory) | |
{ | |
string fileToWrite = Path.GetTempFileName(); | |
await DownloadFileAsync(fileName, workingDirectory, fileToWrite).ConfigureAwait(false); | |
return fileToWrite; | |
} | |
public async Task DownloadFileAsync(string fileName, string workingDirectory, string filePathToWrite) | |
{ | |
using (FtpClient ftpClient = CreateFtpClient()) | |
{ | |
await Task.Factory.FromAsync(ftpClient.BeginConnect, ftpClient.EndConnect, state: null).ConfigureAwait(false); | |
bool doesDirectoryExist = await Task<bool>.Factory.FromAsync<string>(ftpClient.BeginDirectoryExists, ftpClient.EndDirectoryExists, workingDirectory, state: null).ConfigureAwait(false); | |
if (doesDirectoryExist == true) | |
{ | |
await Task.Factory.FromAsync<string>(ftpClient.BeginSetWorkingDirectory, ftpClient.EndSetWorkingDirectory, workingDirectory, state: null).ConfigureAwait(false); | |
bool doesFileExist = await Task<bool>.Factory.FromAsync<string>(ftpClient.BeginFileExists, ftpClient.EndFileExists, fileName, state: null).ConfigureAwait(false); | |
if (doesFileExist == true) | |
{ | |
using (Stream streamToRead = await Task<Stream>.Factory.FromAsync<string>(ftpClient.BeginOpenRead, ftpClient.EndOpenRead, fileName, state: null).ConfigureAwait(false)) | |
using (Stream streamToWrite = File.Open(filePathToWrite, FileMode.Append)) | |
{ | |
await streamToRead.CopyToAsync(streamToWrite).ConfigureAwait(false); | |
} | |
} | |
} | |
} | |
} | |
// privates | |
private FtpClient CreateFtpClient() | |
{ | |
FtpClient ftpClient = new FtpClient(); | |
ftpClient.Host = host; | |
ftpClient.Credentials = new NetworkCredential(username, password); | |
return ftpClient; | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
First line is not returning result so it is not executing remaining three line please help me solve this problem.
string fileName = ftpFileManager.DownloadFileAsync("products2.dat", "foo").Result; // Not returning result
Console.WriteLine(fileName); // Not executing
Console.WriteLine("Done"); // Not executing
Console.ReadLine(); // Not executing