简体   繁体   中英

How to go back to the previous directory using ftp web request via button back in c# winform?

I am using c# Windows Form Application and ftpWebRequest, I am doing a directory listing. I have a listbox that will display folders, by using the event DoubleClick in my listbox, the double clicked folder or item in my listbox will show its content. And now my problem is I don't know how to go back to the previous directory by using back button.

Here is my Code File:

namespace myFTPClass  
{
    public class myFTP
    {
       public string user;
       public string pass;
    public delegate void cThread1(string thread1);
    public event EventHandler EH;

    public List<string> myDIR = new List<string>();

    public void getDirectoryList(string getDirectory)
    {
        try
        {
            FtpWebRequest fwr = FtpWebRequest.Create(getDirectory) as FtpWebRequest;
            fwr.Credentials = new NetworkCredential(user, pass);
            fwr.UseBinary = true;
            fwr.UsePassive = true;
            fwr.KeepAlive = true;
            fwr.Method = WebRequestMethods.Ftp.ListDirectory;

            StreamReader sr = new StreamReader(fwr.GetResponse().GetResponseStream());
            while (!sr.EndOfStream)
            {
                myDIR.Add(sr.ReadLine());
            }
        }
        catch(Exception we)
        {
            myDIR.Clear();
            string msg = we.Message;
        }
    }

    void myCallBackMethod(IAsyncResult ar)
    {
        cThread1 myThread = (cThread1)((System.Runtime.Remoting.Messaging.AsyncResult)ar).AsyncDelegate;
        myThread.EndInvoke(ar);
        if (EH != null) EH(this, null);
    }

    public void Async_getDirectoryList(string dir)
    {
        AsyncCallback ac = new AsyncCallback(myCallBackMethod);
        cThread1 myThread = new cThread1(getDirectoryList);
        myThread.BeginInvoke(dir, ac, null);
    }
}                                                        

}

And Here is my Form1:

namespace my_ftp_v0._01
{
    public partial class Form1 : Form
    {
        myFTP ftp = new myFTP();
        public Form1()
        {
            InitializeComponent();
            this.Load += new EventHandler(Form1_Load);
            btn_connect.Click += new EventHandler(btn_connect_Click);
            listBox1.DoubleClick += new EventHandler(listBox1_DoubleClick);
            btn_back.Click += new EventHandler(btn_back_Click);
            ftp.EH += new EventHandler(ftp_EH);   
        }
    void btn_back_Click(object sender, EventArgs e)
    {

    }

    void listBox1_DoubleClick(object sender, EventArgs e)
    {
        string forward = "ftp://127.0.0.1/" + listBox1.SelectedItem.ToString();
        listBox1.Items.Clear();
        ftp.myDIR.Clear();
        ftp.Async_getDirectoryList(forward);
    }

    void Form1_Load(object sender, EventArgs e)
    {
        txt_dir.Text = "ftp://127.0.0.1/";
        txt_pass.PasswordChar = '‡';
    }

    void ftp_EH(object sender, EventArgs e)
    {
            if (InvokeRequired)
            {
                EventHandler eh = new EventHandler(ftp_EH);
                this.Invoke(eh, new object[] { sender, e });
                return;
            }

            for (int i = 0; i < ftp.myDIR.Count; i++)
            {
                listBox1.Items.Add(ftp.myDIR[i]);
            }
    }

    void btn_connect_Click(object sender, EventArgs e)
    {

            ftp.Async_getDirectoryList(txt_dir.Text);
            ftp.user = txt_user.Text;
            ftp.pass = txt_pass.Text;   
    }
}

}

  1. Move your SetDirectoryList to its own method
  2. Add a Stack object to your class to track your requests
  3. When the user double clicks add the request to the stack and then set the directory.
  4. When the user hits the back button, check if the stack has a request, if it does, pop it off and call the set directory method.

Something like this...

 public partial class Form1 : Form
  {
      myFTP ftp = new myFTP();
      Stack _requestStack = new Stack();//Stack to store requests
      public Form1()
      {
          InitializeComponent();
          this.Load += new EventHandler(Form1_Load);
          btn_connect.Click += new EventHandler(btn_connect_Click);
          listBox1.DoubleClick += new EventHandler(listBox1_DoubleClick);
          btn_back.Click += new EventHandler(btn_back_Click);
          ftp.EH += new EventHandler(ftp_EH);   
      }
      void btn_back_Click(object sender, EventArgs e)
      {
          if(_requestStack.Count > 0)
          {
              string directoryPath = (string)_requestStack.Pop();
              SetDirectoryList(directoryPath);
           }

     }

    void listBox1_DoubleClick(object sender, EventArgs e)
    {
       string  directoryPath = listBox1.SelectedItem.ToString();
       _stack.Push(directoryPath);
       SetDirectoryList(directoryPath);
    }

    void SetDirectoryList(string directoryPath)
    {
             string forward = "ftp://127.0.0.1/" + directoryPath;
            listBox1.Items.Clear();
            ftp.myDIR.Clear();
            ftp.Async_getDirectoryList(forward);
    }

void btn_back_Click(object sender, EventArgs e)

{

create.server = create.server.TrimEnd('/');

create.server = create.server.Remove(create.server.LastIndexOf('/')+1);

        listBox1.Items.Clear();
        ftp.myDIR.Clear();
        ftp.Async_getDirectoryList("");
    }

I've already done this code to my back button and it works properly.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM