/// <summary> /// Determines whether this instance can connect to the FTP server. Run as little as possible, since it blocks the main thread while checking. /// </summary> /// <returns><c>true</c> if this instance can connect to the FTP server; otherwise, <c>false</c>.</returns> public bool CanConnectToFTP() { bool bCanConnectToFTP; string FTPURL = Config.GetFTPUrl(); string FTPUserName = Config.GetFTPUsername(); string FTPPassword = Config.GetFTPPassword(); try { FtpWebRequest plainRequest = (FtpWebRequest)FtpWebRequest.Create(FTPURL); plainRequest.Credentials = new NetworkCredential(FTPUserName, FTPPassword); plainRequest.Method = WebRequestMethods.Ftp.ListDirectory; plainRequest.Timeout = 8000; try { WebResponse response = plainRequest.GetResponse(); plainRequest.Abort(); response.Close(); bCanConnectToFTP = true; } catch (WebException wex) { Console.WriteLine("WebException in CanConnectToFTP(): " + wex.Message); Console.WriteLine(FTPURL); plainRequest.Abort(); bCanConnectToFTP = false; } } catch (WebException wex) { //case where FTP URL in config is not valid Console.WriteLine("WebException CanConnectToFTP() (Invalid URL): " + wex.Message); bCanConnectToFTP = false; return(bCanConnectToFTP); } if (!bCanConnectToFTP) { Console.WriteLine("Failed to connect to FTP server at: {0}", Config.GetFTPUrl()); bCanConnectToFTP = false; } return(bCanConnectToFTP); }
/// <summary> /// Initializes a new instance of the <see cref="Launchpad.SettingsDialog"/> class. /// </summary> public SettingsDialog() { this.Build(); //fill in Local settings GameName_entry.Text = Config.GetGameName(); combobox_SystemTarget.Active = (int)Config.GetSystemTarget(); //fill in remote settings FTPURL_entry.Text = Config.GetBaseFTPUrl(); FTPUsername_entry.Text = Config.GetFTPUsername(); FTPPassword_entry.Text = Config.GetFTPPassword(); progressbar3.Text = Mono.Unix.Catalog.GetString("Idle"); buttonOk.Label = Mono.Unix.Catalog.GetString("OK"); buttonCancel.Label = Mono.Unix.Catalog.GetString("Cancel"); }
/// <summary> /// Reads a text file from a remote FTP server. /// </summary> /// <returns>The FTP file contents.</returns> /// <param name="ftpSourceFilePath">FTP file path.</param> public string ReadFTPFile(string rawRemoteURL) { //clean the input URL first string remoteURL = rawRemoteURL.Replace(Path.DirectorySeparatorChar, '/'); string username = Config.GetFTPUsername(); string password = Config.GetFTPPassword(); int bytesRead = 0; //the buffer size is 256kb. More or less than this reduces download speeds. byte[] buffer = new byte[262144]; FtpWebRequest request = null; FtpWebRequest sizerequest = null; Stream reader = null; try { request = CreateFtpWebRequest(remoteURL, username, password, false); sizerequest = CreateFtpWebRequest(remoteURL, username, password, false); request.Method = WebRequestMethods.Ftp.DownloadFile; sizerequest.Method = WebRequestMethods.Ftp.GetFileSize; string data = ""; reader = request.GetResponse().GetResponseStream(); while (true) { bytesRead = reader.Read(buffer, 0, buffer.Length); if (bytesRead == 0) { break; } FTPbytesDownloaded = FTPbytesDownloaded + bytesRead; data = Encoding.UTF8.GetString(buffer, 0, buffer.Length); } //clean the output from \n and \0, then return return(Utilities.Clean(data)); } catch (WebException wex) { Console.Write("WebException in ReadFTPFileException: "); Console.WriteLine(wex.Message + " (" + remoteURL + ")"); return(wex.Message); } finally { //clean up all open requests //then, the responses that are reading from the requests. if (reader != null) { reader.Close(); } //and finally, the requests themselves. if (request != null) { request.Abort(); } if (sizerequest != null) { sizerequest.Abort(); } } }