void Connect(string urlStr)
        {
            try
            {
                Uri url = new Uri(urlStr);

                // The schema of url must be ftp.
                if (!url.Scheme.Equals("ftp", StringComparison.OrdinalIgnoreCase))
                {
                    throw new ApplicationException("The schema of url must be ftp. ");
                }

                // Set the url to the folder that contains this file.
                if (url.IsFile)
                {
                    url = new Uri(url, "..");
                }

                // Show the Form UICredentialsProvider to get new Credentials.
                using (UICredentialsProvider provider =
                           new UICredentialsProvider(this.currentCredentials))
                {
                    // Show the Form UICredentialsProvider as a dialog.
                    var result = provider.ShowDialog();

                    // If user typed the Credentials and pressed the "OK" button.
                    if (result == System.Windows.Forms.DialogResult.OK)
                    {
                        // Reset the current Credentials.
                        this.currentCredentials = provider.Credentials;
                    }
                    else
                    {
                        return;
                    }
                }

                // Initialize the FTPClient instance.
                client = new FTPClientManager(url, currentCredentials);

                client.UrlChanged          += new EventHandler(client_UrlChanged);
                client.StatusChanged       += new EventHandler(client_StatusChanged);
                client.ErrorOccurred       += new EventHandler <ErrorEventArgs>(client_ErrorOccurred);
                client.FileUploadCompleted +=
                    new EventHandler <FileUploadCompletedEventArgs>(client_FileUploadCompleted);
                client.NewMessageArrived +=
                    new EventHandler <NewMessageEventArg>(client_NewMessageArrived);

                // Refresh the UI and list the sub directories and files.
                RefreshUI();
            }
            catch (System.Net.WebException webEx)
            {
                if ((webEx.Response as FtpWebResponse).StatusCode == FtpStatusCode.NotLoggedIn)
                {
                    // Reconnect the server.
                    Connect(urlStr);

                    return;
                }
                else
                {
                    MessageBox.Show(webEx.Message);
                }
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
            }
        }
Esempio n. 2
0
        void Connect(string urlStr)
        {
            try
            {
                Uri url = new Uri(urlStr);

                //地址必须是ftp格式。
                if (!url.Scheme.Equals("ftp", StringComparison.OrdinalIgnoreCase))
                {
                    throw new ApplicationException("地址格式必须为 ftp. ");
                }

                //设置包含文件的文件夹地址。
                if (url.IsFile)
                {
                    url = new Uri(url, "..");
                }

                //显示UICredentialsProvider窗体得到新认证。
                using (UICredentialsProvider provider =
                    new UICredentialsProvider(this.currentCredentials))
                {

                    //显示UICredentialsProvider窗体为一个对话框
                    var result = provider.ShowDialog();

                    //假如用户输入认证 ,按“OK”按钮
                    if (result == System.Windows.Forms.DialogResult.OK)
                    {

                        //重设当前认证。
                        this.currentCredentials = provider.Credentials;

                    }
                    else
                    {
                        return;
                    }
                }

                //初始化FTP客户端实例。
                client = new FTPClientManager(url, currentCredentials);

                client.UrlChanged += new EventHandler(client_UrlChanged);
                client.StatusChanged += new EventHandler(client_StatusChanged);
                client.ErrorOccurred += new EventHandler<ErrorEventArgs>(client_ErrorOccurred);
                client.FileUploadCompleted +=
                    new EventHandler<FileUploadCompletedEventArgs>(client_FileUploadCompleted);
                client.NewMessageArrived +=
                    new EventHandler<NewMessageEventArg>(client_NewMessageArrived);

                //刷新UI和列出子目录和文件
                RefreshUI();
            }

            catch (System.Net.WebException webEx)
            {
                if ((webEx.Response as FtpWebResponse).StatusCode == FtpStatusCode.NotLoggedIn)
                {
                       //重新连接服务器
                        Connect(urlStr);

                        return;
                }
                else
                {
                    MessageBox.Show(webEx.Message);
                }
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
            }
        }
Esempio n. 3
0
        void Connect(string urlStr)
        {
            try
            {
                Uri url = new Uri(urlStr);


                //地址必须是ftp格式。
                if (!url.Scheme.Equals("ftp", StringComparison.OrdinalIgnoreCase))
                {
                    throw new ApplicationException("地址格式必须为 ftp. ");
                }


                //设置包含文件的文件夹地址。
                if (url.IsFile)
                {
                    url = new Uri(url, "..");
                }


                //显示UICredentialsProvider窗体得到新认证。
                using (UICredentialsProvider provider =
                           new UICredentialsProvider(this.currentCredentials))
                {
                    //显示UICredentialsProvider窗体为一个对话框
                    var result = provider.ShowDialog();


                    //假如用户输入认证 ,按“OK”按钮
                    if (result == System.Windows.Forms.DialogResult.OK)
                    {
                        //重设当前认证。
                        this.currentCredentials = provider.Credentials;
                    }
                    else
                    {
                        return;
                    }
                }


                //初始化FTP客户端实例。
                client = new FTPClientManager(url, currentCredentials);

                client.UrlChanged          += new EventHandler(client_UrlChanged);
                client.StatusChanged       += new EventHandler(client_StatusChanged);
                client.ErrorOccurred       += new EventHandler <ErrorEventArgs>(client_ErrorOccurred);
                client.FileUploadCompleted +=
                    new EventHandler <FileUploadCompletedEventArgs>(client_FileUploadCompleted);
                client.NewMessageArrived +=
                    new EventHandler <NewMessageEventArg>(client_NewMessageArrived);

                //刷新UI和列出子目录和文件
                RefreshUI();
            }


            catch (System.Net.WebException webEx)
            {
                if ((webEx.Response as FtpWebResponse).StatusCode == FtpStatusCode.NotLoggedIn)
                {
                    //重新连接服务器
                    Connect(urlStr);

                    return;
                }
                else
                {
                    MessageBox.Show(webEx.Message);
                }
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
            }
        }