WindowsPhone Silverlight (8.0 & 8.1) : Upload files to Ftp Server (C# - XAML)

Introduction:I am very happy to write an article about FTP server access from windows phone programming using c#.net, there are no direct APIs available for leveraging FTP services in a Windows phone app.

C# (377.0 KB)
 
 
 
 
 
(0)
229 times
Add to favorites
12/27/2015
E-mail Twitter del.icio.us Digg Facebook

Solution explorer

C#
using Cubisoft.Winrt.Ftp;
using System;
using System.IO.IsolatedStorage;
using System.Linq;
using System.Net;
using System.Runtime.InteropServices.WindowsRuntime;
using System.Text;
using System.Threading.Tasks;
using Windows.Networking;
using Windows.Storage.Streams;

namespace WindowsPhoneFTP
{
    //[TestClass]
    public class FtpClientClass
    {
        private static string m_Host = "Please ENTER your Server Name/IP";
        private static string FtpUserName = "Server UserName";
        private static string FtpPwd = "Server Password";
        private static string FtpPort = "21";
        private readonly NetworkCredential m_Credentials = new NetworkCredential(FtpUserName, FtpPwd);

        private const string PathToSet = "/";

        private const string FolderToCreate = "";//"WindowsPhone";

        private const string FileToCreate = "Temp.csv";

        public FtpClient CreateFtpClient()
        {
          
            var ftpClient = new FtpClient();
            ftpClient.Credentials = m_Credentials;
            ftpClient.HostName = new HostName(m_Host);
            ftpClient.ServiceName = FtpPort;

            return ftpClient;
        }

        //[TestMethod]
        public async Task TestConnectAsync()
        {
          
            var ftpClient = CreateFtpClient();

            await ftpClient.ConnectAsync();

            if (ftpClient.IsConnected)
            {

            }
            else
            {

            }
            //Assert.IsTrue(ftpClient.IsConnected);
            //Assert.IsFalse(ftpClient.Capabilities == FtpCapability.NONE);

            await ftpClient.DisconnectAsync();

            System.Diagnostics.Debug.WriteLine("-----------------------------------");
        }


        //[TestMethod]
        public async Task TestDisconnectAsync()
        {
            System.Diagnostics.Debug.WriteLine("-----------------------------------");
            System.Diagnostics.Debug.WriteLine("TestDisconnectAsync");
            System.Diagnostics.Debug.WriteLine("-----------------------------------");

            var ftpClient = CreateFtpClient();

            await ftpClient.ConnectAsync();

            if (!ftpClient.IsConnected) //Assert.Inconclusive("No connection could be made to the FTP server {0}:{1}", m_Host, "21");

                await ftpClient.DisconnectAsync();

            //Assert.IsFalse(ftpClient.IsConnected);

            System.Diagnostics.Debug.WriteLine("-----------------------------------");
        }


        //[TestMethod]
        public async Task<string> UploadFileAsync(string DataFields, string fileUsername)
        {
            try
            {
                if (fileUsername=="")
                {
                    fileUsername = "NoUserName";
                }
                System.Diagnostics.Debug.WriteLine("-----------------------------------");
                System.Diagnostics.Debug.WriteLine("TestCreateFileAsync");
                System.Diagnostics.Debug.WriteLine("-----------------------------------");

                var ftpClient = CreateFtpClient();

                await ftpClient.ConnectAsync();
                await ftpClient.CreateDirectoryAsync(PathToSet + FolderToCreate);
                string testFilePath = PathToSet + fileUsername + ".txt";

                using (var stream = await ftpClient.OpenWriteAsync(testFilePath))
                {
                    var bytes = Encoding.UTF8.GetBytes(DataFields);

                    await stream.WriteAsync(bytes.AsBuffer());
                    await stream.FlushAsync();
                }
                //await ftpClient.DisconnectAsync();
                return "Success";
            }
            catch(Exception ex)
            {
                return ex.Message;
            }
        }
    }
}