Advertisement
Pi0trek

FTP

Aug 16th, 2017
115
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 4.37 KB | None | 0 0
  1. using System;
  2. using System.Collections.Generic;
  3. using System.IO;
  4. using System.Linq;
  5. using System.Net;
  6. using System.Text;
  7. using System.Threading.Tasks;
  8.  
  9. namespace ClientFTP
  10. {
  11.     class FTP
  12.     {
  13.         public string username { get; }
  14.         public string password { get; }
  15.         public string hostname { get; }
  16.         public string currentDirectory { get; private set; }
  17.         public string currentDirectoryAbsolute //tak samo jak public string getCurrentDirectoryAbsolute() {return hostname + currentDirectory;}
  18.         {
  19.             get { return hostname + currentDirectory; }
  20.         }
  21.         public int port { get; }
  22.  
  23.         public FTP(string username, string password, string hostname, int port)
  24.         {
  25.             this.username = username;
  26.             this.password = password;
  27.             this.port = port;
  28.  
  29.             int index = hostname.IndexOf('/');
  30.             if (index == -1)
  31.             {
  32.                 this.hostname = hostname + "/";
  33.                 currentDirectory = "";
  34.             }
  35.             else
  36.             {
  37.                 this.hostname = hostname.Substring(0, index);
  38.                 currentDirectory = hostname.Substring(index, hostname.Length - index) + "/";
  39.             }
  40.         }
  41.  
  42.         string updatePath(string oldPath, string newPath)
  43.         {
  44.             string s = "/" + oldPath + "/" + newPath;
  45.  
  46.             while (s.Contains("//"))
  47.                 s = s.Replace("//", "/");
  48.  
  49.             while (s.Contains(".."))
  50.             {
  51.                 int index = s.IndexOf("/..");
  52.                 string left = s.Substring(0, index);
  53.                 string right = "/" + s.Substring(index + 3, s.Length - (index + 3));
  54.  
  55.                 index = left.LastIndexOf('/');
  56.                 left = left.Substring(0, index);
  57.                 s = left + right;
  58.             }
  59.             return s;
  60.         }
  61.  
  62.         public void changeDirectory(string dir)
  63.         {
  64.             if (dir[0] == '/')
  65.                 currentDirectory = "%2f" + dir + "/";
  66.             else
  67.                 currentDirectory = updatePath(currentDirectory, dir);
  68.  
  69.             while (currentDirectory.Contains("//"))
  70.                 currentDirectory = currentDirectory.Replace("//", "/");
  71.         }
  72.  
  73.         public string getDirectoryContents(string path = null)
  74.         {
  75.             StringBuilder result = new StringBuilder();
  76.             FtpWebRequest requestDir = (FtpWebRequest)WebRequest.Create("ftp://" + hostname + "/" + (path == null ? currentDirectory : path));
  77.             requestDir.Method = WebRequestMethods.Ftp.ListDirectoryDetails;
  78.             requestDir.Credentials = new NetworkCredential(username, password);
  79.             FtpWebResponse responseDir = (FtpWebResponse)requestDir.GetResponse();
  80.             StreamReader readerDir = new StreamReader(responseDir.GetResponseStream());
  81.  
  82.             string line = readerDir.ReadLine();
  83.             while (line != null)
  84.             {
  85.                 result.Append(line);
  86.                 result.Append(Environment.NewLine);
  87.                 line = readerDir.ReadLine();
  88.             }
  89.  
  90.             responseDir.Close();
  91.             return result.ToString();
  92.         }
  93.  
  94.         public string printDirectoryTree(string path, bool first = true)
  95.         {
  96.             StringBuilder sb = new StringBuilder();
  97.             while (path.Length > 1 && path[path.Length - 1] == '/')
  98.                 path = path.Substring(0, path.Length - 1);
  99.             string[] contents = getDirectoryContents(path).Split('\n');
  100.  
  101.             foreach(string line in contents)
  102.             {
  103.                 if (line.Trim() == "")
  104.                     continue;
  105.  
  106.                 string tmp = line.Substring(line.LastIndexOf(' '), line.Length - line.LastIndexOf(' ')).Trim();
  107.  
  108.                 if (line[0] == 'd' && tmp != "." && tmp != "..")
  109.                 {
  110.                     sb.Append((first ? "" : "\t") + tmp + "\n");
  111.                     foreach (string l in printDirectoryTree(path + "/" + tmp, false).Split('\n'))
  112.                         sb.Append((first ? "" : "\t") + l + "\n");
  113.                 }
  114.                 else if (tmp == "." || tmp == "..")
  115.                 {
  116.                     continue;
  117.                 }
  118.                 else
  119.                 {
  120.                     sb.Append((first ? "" : "\t") + tmp + "\n");
  121.                 }
  122.             }
  123.  
  124.             return sb.ToString();
  125.         }
  126.     }
  127. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement