file360

Log | Files | Refs

FTPServerPage.xaml.cs (7172B)


      1 using System;
      2 using System.Collections.Generic;
      3 using System.Linq;
      4 using System.Net;
      5 using System.IO;
      6 using System.Windows;
      7 using System.Windows.Controls;
      8 using System.Windows.Navigation;
      9 using Microsoft.Phone.Controls;
     10 using Microsoft.Phone.Shell;
     11 using Windows.Storage;
     12 using Windows.System;
     13 using System.IO.IsolatedStorage;
     14 using WinPhoneFtp.FtpService;
     15 using System.Threading.Tasks;
     16 using Windows.Networking.Sockets;
     17 using Windows.UI.Core;
     18 
     19 namespace File360
     20 {
     21     public partial class FTPServerPage : PhoneApplicationPage
     22     {
     23         public FTPServerPage()
     24         {
     25             InitializeComponent();
     26         }
     27 
     28         FtpClient ftpClient = null;
     29         Logger logger = null;
     30 
     31 
     32         async private void Connect_Click(object sender, RoutedEventArgs e)
     33         {
     34             logger = Logger.GetDefault(this.Dispatcher);
     35             lstLogs.ItemsSource = logger.Logs;
     36             ftpClient = new FtpClient(txtIp.Text, this.Dispatcher);
     37             //ftpClient = new PqaFtpClient(txtIp.Text);
     38             ftpClient.FtpConnected += ftpClient_FtpConnected;
     39             ftpClient.FtpDisconnected += ftpClient_FtpDisconnected;
     40             ftpClient.FtpAuthenticationSucceeded += ftpClient_FtpAuthenticationSucceeded;
     41             ftpClient.FtpAuthenticationFailed += ftpClient_FtpAuthenticationFailed;
     42             ftpClient.FtpDirectoryChangedFailed += ftpClient_FtpDirectoryChangedFailed;
     43             ftpClient.FtpDirectoryChangedSucceded += ftpClient_FtpDirectoryChangedSucceded;
     44             ftpClient.FtpPresentWorkingDirectoryReceived += ftpClient_FtpPresentWorkingDirectoryReceived;
     45             ftpClient.FtpFileUploadSucceeded += ftpClient_FtpFileUploadSucceeded;
     46             ftpClient.FtpFileDownloadSucceeded += ftpClient_FtpFileDownloadSucceeded;
     47             ftpClient.FtpFileUploadFailed += ftpClient_FtpFileUploadFailed;
     48             ftpClient.FtpFileDownloadFailed += ftpClient_FtpFileDownloadFailed;
     49             ftpClient.FtpDirectoryListed += ftpClient_FtpDirectoryListed;
     50             ftpClient.FtpFileTransferProgressed += ftpClient_FtpFileTransferProgressed;
     51             await ftpClient.ConnectAsync();
     52         }
     53 
     54         void ftpClient_FtpDisconnected(object sender, FtpDisconnectedEventArgs e)
     55         {
     56             logger.AddLog(String.Format("FTP Disconnected. Reason: {0}", e.DisconnectReason));
     57         }
     58 
     59         void ftpClient_FtpFileTransferProgressed(object sender, FtpFileTransferProgressedEventArgs e)
     60         {
     61             logger.AddLog(String.Format("File {0}, Data Transferred: {1}", e.IsUpload ? "Upload" : "Download", e.BytesTransferred));
     62         }
     63 
     64         void ftpClient_FtpDirectoryListed(object sender, FtpDirectoryListedEventArgs e)
     65         {
     66             logger.AddLog("Directory Listing");
     67 
     68             foreach (String filename in e.GetFilenames())
     69             {
     70                 logger.AddLog("file: " + filename);
     71             }
     72 
     73             foreach (String dir in e.GetDirectories())
     74             {
     75                 logger.AddLog("directory: " + dir);
     76             }
     77         }
     78 
     79         async private void Command_Click(object sender, RoutedEventArgs e)
     80         {
     81             if (txtCmd.Text.StartsWith("STOR"))
     82             {
     83                 logger.Logs.Clear();
     84                 String Filename = txtCmd.Text.Split(new char[] { ' ', '/' }, StringSplitOptions.RemoveEmptyEntries).Last();
     85                 StorageFile file = await Windows.Storage.ApplicationData.Current.LocalFolder.GetFileAsync(txtCmd.Text.Split(new char[] { ' ' }, StringSplitOptions.RemoveEmptyEntries)[1]);
     86                 await ftpClient.UploadFileAsync(await file.OpenStreamForReadAsync(), "123.wmv");
     87                 return;
     88             }
     89 
     90             if (txtCmd.Text.StartsWith("RETR"))
     91             {
     92                 logger.Logs.Clear();
     93                 String Filename = txtCmd.Text.Split(new char[] { ' ', '/' }, StringSplitOptions.RemoveEmptyEntries).Last();
     94                 StorageFile file = await Windows.Storage.ApplicationData.Current.LocalFolder.CreateFileAsync(txtCmd.Text.Split(new char[] { ' ' }, StringSplitOptions.RemoveEmptyEntries)[1], CreationCollisionOption.OpenIfExists);
     95                 await ftpClient.DownloadFileAsync(await file.OpenStreamForWriteAsync(), Filename);
     96                 return;
     97             }
     98 
     99             if (txtCmd.Text.Equals("PWD"))
    100             {
    101                 logger.Logs.Clear();
    102                 await ftpClient.GetPresentWorkingDirectoryAsync();
    103                 return;
    104             }
    105 
    106             if (txtCmd.Text.Equals("LIST"))
    107             {
    108                 logger.Logs.Clear();
    109                 await ftpClient.GetDirectoryListingAsync();
    110                 return;
    111             }
    112 
    113             if (txtCmd.Text.StartsWith("CWD"))
    114             {
    115                 logger.Logs.Clear();
    116                 await ftpClient.ChangeWorkingDirectoryAsync(txtCmd.Text.Split(new char[] { ' ' }, StringSplitOptions.RemoveEmptyEntries)[1]);
    117                 return;
    118             }
    119 
    120             if (txtCmd.Text.Equals("QUIT"))
    121             {
    122                 logger.Logs.Clear();
    123                 await ftpClient.DisconnectAsync();
    124                 return;
    125             }
    126         }
    127 
    128         void ftpClient_FtpFileDownloadFailed(object sender, FtpFileTransferFailedEventArgs e)
    129         {
    130             //e.LocalFileStream.Close();
    131             //e.LocalFileStream.Dispose();
    132             logger.AddLog("download failed");
    133         }
    134 
    135         void ftpClient_FtpFileUploadFailed(object sender, FtpFileTransferFailedEventArgs e)
    136         {
    137             //e.LocalFileStream.Close();
    138             //e.LocalFileStream.Dispose();
    139             logger.AddLog("upload failed");
    140         }
    141 
    142         void ftpClient_FtpFileDownloadSucceeded(object sender, FtpFileTransferEventArgs e)
    143         {
    144             //e.LocalFileStream.Close();
    145             //e.LocalFileStream.Dispose();
    146             logger.AddLog("download done");
    147         }
    148 
    149         void ftpClient_FtpFileUploadSucceeded(object sender, FtpFileTransferEventArgs e)
    150         {
    151             //e.LocalFileStream.Close();
    152             //e.LocalFileStream.Dispose();
    153             logger.AddLog("upload done");
    154         }
    155 
    156         async void ftpClient_FtpConnected(object sender, EventArgs e)
    157         {
    158             await (sender as FtpClient).AuthenticateAsync("admin","admin");
    159         }
    160 
    161         void ftpClient_FtpPresentWorkingDirectoryReceived(object sender, FtpPresentWorkingDirectoryEventArgs e)
    162         {
    163             logger.AddLog(String.Format("Present working directory is: {0}", e.PresentWorkingDirectory));
    164         }
    165 
    166         void ftpClient_FtpDirectoryChangedSucceded(object sender, FtpDirectoryChangedEventArgs e)
    167         {
    168             logger.AddLog(String.Format("Directory {0} change succeeded", e.RemoteDirectory));
    169         }
    170 
    171         void ftpClient_FtpDirectoryChangedFailed(object sender, FtpDirectoryChangedEventArgs e)
    172         {
    173             logger.AddLog("Directory change failed");
    174         }
    175 
    176         void ftpClient_FtpAuthenticationFailed(object sender, EventArgs e)
    177         {
    178             logger.AddLog("Authentication failed");
    179         }
    180 
    181         void ftpClient_FtpAuthenticationSucceeded(object sender, EventArgs e)
    182         {
    183             logger.AddLog("Authentication succeeded");
    184         }
    185     }
    186 }