forked from robinrodricks/FluentFTP
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathBeginDereferenceLink.cs
61 lines (53 loc) · 2.24 KB
/
BeginDereferenceLink.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
using System;
using System.Net;
using System.Threading;
using FluentFTP;
namespace Examples {
/// <summary>
/// This example illustrates how to dereference a symbolic link asyncrhonously. The
/// code bollow takes a FtpListItem object and checks if it is a symbolic link and
/// that the LinkTarget property has been initalized before executing the method. Not
/// doing so can result in a FtpException being thrown.
///
/// Also see the DerefenceLink() example! There is lots of information
/// not mentioned here!
/// </summary>
static class BeginDereferenceLink {
static ManualResetEvent m_reset = new ManualResetEvent(false);
public static void BeginDereferenceLinkExample(FtpListItem item) {
// The using statement here is OK _only_ because m_reset.WaitOne()
// causes the code to block until the async process finishes, otherwise
// the connection object would be disposed early. In practice, you
// typically would not wrap the following code with a using statement.
using (FtpClient conn = new FtpClient()) {
m_reset.Reset();
conn.Host = "localhost";
conn.Credentials = new NetworkCredential("ftptest", "ftptest");
conn.Connect();
if (item.Type == FtpFileSystemObjectType.Link && item.LinkTarget != null) {
conn.BeginDereferenceLink(item, new AsyncCallback(DereferenceLinkCallback), conn);
m_reset.WaitOne();
}
conn.Disconnect();
}
}
static void DereferenceLinkCallback(IAsyncResult ar) {
FtpClient conn = ar.AsyncState as FtpClient;
FtpListItem target;
try {
if (conn == null)
throw new InvalidOperationException("The FtpControlConnection object is null!");
target = conn.EndDereferenceLink(ar);
if (target != null) {
// success...
}
}
catch (Exception ex) {
Console.WriteLine(ex.ToString());
}
finally {
m_reset.Set();
}
}
}
}