示例#1
0
        private void btnTest_Click(object sender, RoutedEventArgs e)
        {
            try
            {
                if (txtIP.Text.Length == 0 || txtPort.Text.Length == 0 || txtWorldsPath.Text.Length == 0 || txtUsername.Text.Length == 0 || txtPassword.Text.Length == 0)
                {
                    ModernMessageBox mmb = new ModernMessageBox(this);
                    mmb.Show("All fields are required.", "Missing FTP information", MessageBoxButton.OK, MessageBoxImage.Warning);
                    return;
                }
                SessionOptions sessionOptions = new SessionOptions
                {
                    Protocol   = Protocol.Ftp,
                    HostName   = txtIP.Text,
                    PortNumber = Int32.Parse(txtPort.Text),
                    UserName   = txtUsername.Text,
                    Password   = txtPassword.Text,
                    FtpMode    = (FtpMode)cmbFtpMode.SelectedIndex
                };

                using (Session session = new Session())
                {
                    // Connect
                    try
                    {
                        session.Open(sessionOptions);
                        if (session.FileExists(txtWorldsPath.Text))
                        {
                            var dbFound = false;
                            foreach (var file in session.ListDirectory(txtWorldsPath.Text).Files)
                            {
                                if (file.ToString().EndsWith(".db"))
                                {
                                    dbFound = true;
                                }
                            }
                            if (dbFound)
                            {
                                ModernMessageBox mmb = new ModernMessageBox(this);
                                mmb.Show("Connection successful.", "Connection Successful", MessageBoxButton.OK, MessageBoxImage.Information);
                            }
                            else
                            {
                                ModernMessageBox mmb = new ModernMessageBox(this);
                                mmb.Show($"Connected successfully, but no world saves found at {txtWorldsPath.Text}.", "No Worlds Found", MessageBoxButton.OK, MessageBoxImage.Warning);
                            }
                        }
                        else
                        {
                            ModernMessageBox mmb = new ModernMessageBox(this);
                            mmb.Show($"Connected successfully to FTP server, but path {txtWorldsPath.Text} does not exist.", "Path Not Found", MessageBoxButton.OK, MessageBoxImage.Error);
                        }
                    }
                    catch (Exception ex)
                    {
                        ModernMessageBox mmb = new ModernMessageBox(this);
                        mmb.Show($"Error connecting to FTP server: {ex.Message}", "Connection Error", MessageBoxButton.OK, MessageBoxImage.Error);
                    }
                }
            }
            catch (Exception ex)
            {
                Debug.WriteLine($"Error testing FTP connection: {ex.Message}");
            }
        }