Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using System;
- using System.Collections.Generic;
- using System.IO;
- using System.Linq;
- using System.Net;
- using System.Text;
- using System.Threading.Tasks;
- namespace ClientFTP
- {
- class FTP
- {
- public string username { get; }
- public string password { get; }
- public string hostname { get; }
- public string currentDirectory { get; private set; }
- public string currentDirectoryAbsolute //tak samo jak public string getCurrentDirectoryAbsolute() {return hostname + currentDirectory;}
- {
- get { return hostname + currentDirectory; }
- }
- public int port { get; }
- public FTP(string username, string password, string hostname, int port)
- {
- this.username = username;
- this.password = password;
- this.port = port;
- int index = hostname.IndexOf('/');
- if (index == -1)
- {
- this.hostname = hostname + "/";
- currentDirectory = "";
- }
- else
- {
- this.hostname = hostname.Substring(0, index);
- currentDirectory = hostname.Substring(index, hostname.Length - index) + "/";
- }
- }
- string updatePath(string oldPath, string newPath)
- {
- string s = "/" + oldPath + "/" + newPath;
- while (s.Contains("//"))
- s = s.Replace("//", "/");
- while (s.Contains(".."))
- {
- int index = s.IndexOf("/..");
- string left = s.Substring(0, index);
- string right = "/" + s.Substring(index + 3, s.Length - (index + 3));
- index = left.LastIndexOf('/');
- left = left.Substring(0, index);
- s = left + right;
- }
- return s;
- }
- public void changeDirectory(string dir)
- {
- if (dir[0] == '/')
- currentDirectory = "%2f" + dir + "/";
- else
- currentDirectory = updatePath(currentDirectory, dir);
- while (currentDirectory.Contains("//"))
- currentDirectory = currentDirectory.Replace("//", "/");
- }
- public string getDirectoryContents(string path = null)
- {
- StringBuilder result = new StringBuilder();
- FtpWebRequest requestDir = (FtpWebRequest)WebRequest.Create("ftp://" + hostname + "/" + (path == null ? currentDirectory : path));
- requestDir.Method = WebRequestMethods.Ftp.ListDirectoryDetails;
- requestDir.Credentials = new NetworkCredential(username, password);
- FtpWebResponse responseDir = (FtpWebResponse)requestDir.GetResponse();
- StreamReader readerDir = new StreamReader(responseDir.GetResponseStream());
- string line = readerDir.ReadLine();
- while (line != null)
- {
- result.Append(line);
- result.Append(Environment.NewLine);
- line = readerDir.ReadLine();
- }
- responseDir.Close();
- return result.ToString();
- }
- public string printDirectoryTree(string path, bool first = true)
- {
- StringBuilder sb = new StringBuilder();
- while (path.Length > 1 && path[path.Length - 1] == '/')
- path = path.Substring(0, path.Length - 1);
- string[] contents = getDirectoryContents(path).Split('\n');
- foreach(string line in contents)
- {
- if (line.Trim() == "")
- continue;
- string tmp = line.Substring(line.LastIndexOf(' '), line.Length - line.LastIndexOf(' ')).Trim();
- if (line[0] == 'd' && tmp != "." && tmp != "..")
- {
- sb.Append((first ? "" : "\t") + tmp + "\n");
- foreach (string l in printDirectoryTree(path + "/" + tmp, false).Split('\n'))
- sb.Append((first ? "" : "\t") + l + "\n");
- }
- else if (tmp == "." || tmp == "..")
- {
- continue;
- }
- else
- {
- sb.Append((first ? "" : "\t") + tmp + "\n");
- }
- }
- return sb.ToString();
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement