public bool SaveConnection(FtpHandler toSave) { //Allow user to save connection that current FtpHandler is connected to. Append saved connection info, and write to text file. StreamWriter sw; if (connectionList == null) { if (!File.Exists("SavedConnections.txt")) { File.Create("SavedConnections.txt"); } } sw = File.AppendText("SavedConnections.txt"); sw.WriteLine(toSave.url); sw.WriteLine(toSave.userName); sw.Flush(); sw.Close(); Console.WriteLine("Connection saved!"); return(true); }
public bool Connect(FtpHandler toConnect) { //Allow user to select from array of SavedConnections, and feed information into appropriate FtpHandler fields. //prompt for password try { if (connectionList == null) { Console.WriteLine("No information in SavedConnections.txt"); return(false); } //print out all possible connections for (int i = 0; i < connectionList.Count; i++) { Console.WriteLine(i); Console.WriteLine("url: " + connectionList[i].url); Console.WriteLine("username: "******"Make a selection: "); string userInput = Console.ReadLine(); int userResult; try { userResult = Int32.Parse(userInput); if (userResult < 0 || userResult > connectionList.Count) { throw new Exception(); } //feed connectionList result into our FtpHandler toConnect.url = connectionList[userResult].url; toConnect.userName = connectionList[userResult].userName; break; } catch (FormatException) { Console.WriteLine("Cannot parse. Please try again!"); Console.WriteLine(": "); } catch (Exception) { Console.WriteLine("Input out of range. Try again!"); Console.WriteLine(": "); } } //the rest is copy paste from logonunsaved toConnect.mainRequest = (FtpWebRequest)WebRequest.Create(toConnect.url); toConnect.securePwd = new SecureString(); ConsoleKeyInfo key; Console.Write("Enter password: "******"*"); } // Exit if Enter key is pressed. } while (key.Key != ConsoleKey.Enter); Console.WriteLine(); //Now, we feed all of these into a NetworkCredentials class, and feed that into our FtpWebRequest toConnect.mainRequest.Credentials = new NetworkCredential(toConnect.userName, toConnect.securePwd); //Other parameters that are important to have toConnect.mainRequest.KeepAlive = false; toConnect.mainRequest.UseBinary = true; toConnect.mainRequest.UsePassive = true; //List all files in directory, for testing, and to make sure we're connected toConnect.mainRequest.Method = WebRequestMethods.Ftp.ListDirectoryDetails; //Assign to mainResponse toConnect.mainResponse = (FtpWebResponse)toConnect.mainRequest.GetResponse(); //Read the response stream Stream responseStream = toConnect.mainResponse.GetResponseStream(); StreamReader responseRead = new StreamReader(responseStream); //Print Console.WriteLine(responseRead.ReadToEnd()); //gracefully exit reader and response responseRead.Close(); toConnect.mainResponse.Close(); return(true); } catch (Exception bruh) { Console.WriteLine(bruh.Message); return(false); } }
static void Main(string[] args) { FtpHandler mainHandler = new FtpHandler(); RemoteLS listRemote = new RemoteLS(); RemoteMkDir mkDirRemote = new RemoteMkDir(); Upload upload = new Upload(); Download download = new Download(); Rename rename = new Rename(); Delete delete = new Delete(); mainHandler.LogOnInitial(); try { bool loop = true; while (loop) { Console.WriteLine("\nEnter a command:\nls = List Directory Details\nupload = UpLoad File\nupload-m = Upload Multiple files\ndownload = DownLoad File\ndelete = Delete a File\nrr = Rename a Remote File\nmkdir = Make a Remote Directory\nsave = Save currenly connected server \nd = Disconnect\n"); timer = new System.Timers.Timer(); timer.Interval = 10000; timer.Elapsed += OnTimedEvent; timer.AutoReset = true; timer.Enabled = true; string command = Console.ReadLine(); timer.Enabled = false; switch (command.ToLower()) { case "upload": Console.WriteLine("Enter the filename to put on the ftp.\n"); string upFile = Console.ReadLine(); upload.UploadToRemote(mainHandler, upFile); break; case "upload-m": Console.WriteLine("Enter the filename to put on the ftp seperated by \";\"'s.\n"); string upFiles = Console.ReadLine(); upload.UploadMultipleToRemote(mainHandler, upFiles); break; case "download": Console.WriteLine("Enter filename to take from the ftp.\n"); string downFile = Console.ReadLine(); download.DownloadFromRemote(mainHandler, downFile); break; case "delete": Console.WriteLine("Enter filename to DELETE from the ftp.\n"); string deleteFile = Console.ReadLine(); delete.DeleteRemoteFile(mainHandler, deleteFile); break; case "ls": listRemote.ListRemote(mainHandler); break; case "rr": Console.WriteLine("Enter filename to Rename from the ftp.\n"); string currnetFile = Console.ReadLine(); Console.WriteLine("Enter the new name.\n"); string newFileName = Console.ReadLine(); rename.RenameRemoteFile(mainHandler, currnetFile, newFileName); break; case "d": loop = false; break; case "mkdir": Console.WriteLine("Enter directory name. \n"); string dir = Console.ReadLine(); mkDirRemote.MkDirRemote(mainHandler, dir); break; case "save": mainHandler.SaveInfo(); break; default: Console.WriteLine("Wrong Command.\n"); break; } command = string.Empty; } Console.WriteLine("Press enter to disconnect"); Console.ReadLine(); mainHandler.LogOff(); } catch (Exception ex) { Console.WriteLine(ex); File.WriteAllText("log.txt", ex.ToString()); } }