BeginSetWorkingDirectory.cs 1.5 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344
  1. using System;
  2. using System.Net;
  3. using System.Net.FtpClient;
  4. using System.Threading;
  5. namespace Examples {
  6. public static class BeginSetWorkingDirectoryExample {
  7. static ManualResetEvent m_reset = new ManualResetEvent(false);
  8. public static void BeginSetWorkingDirectory() {
  9. // The using statement here is OK _only_ because m_reset.WaitOne()
  10. // causes the code to block until the async process finishes, otherwise
  11. // the connection object would be disposed early. In practice, you
  12. // typically would not wrap the following code with a using statement.
  13. using (FtpClient conn = new FtpClient()) {
  14. m_reset.Reset();
  15. conn.Host = "localhost";
  16. conn.Credentials = new NetworkCredential("ftptest", "ftptest");
  17. conn.BeginSetWorkingDirectory("/", new AsyncCallback(BeginSetWorkingDirectoryCallback), conn);
  18. m_reset.WaitOne();
  19. conn.Disconnect();
  20. }
  21. }
  22. static void BeginSetWorkingDirectoryCallback(IAsyncResult ar) {
  23. FtpClient conn = ar.AsyncState as FtpClient;
  24. try {
  25. if (conn == null)
  26. throw new InvalidOperationException("The FtpControlConnection object is null!");
  27. conn.EndSetWorkingDirectory(ar);
  28. }
  29. catch (Exception ex) {
  30. Console.WriteLine(ex.ToString());
  31. }
  32. finally {
  33. m_reset.Set();
  34. }
  35. }
  36. }
  37. }