0

这是代码(出于显而易见的原因LicenseOwnerLicenseKey必须将其删除):

using System;
using System.Linq;
using EnterpriseDT.Net.Ftp;

namespace ftpClient
{
    internal class Program
    {
        static void Main()
        {
            var con = new SecureFTPConnection
            {
                Protocol = FileTransferProtocol.FTP,
                LicenseOwner = "***",
                LicenseKey = "***",
                ServerAddress = "ftp.swfwmd.state.fl.us", 
                ServerDirectory = "pub",
                UserName = "anonymous", 
                Password = "a@b.c"
            };

            var ftpTask = con.BeginConnect(null, null) as FTPTask;

            try
            {
                if (!ftpTask.AsyncWaitHandle.WaitOne(TimeSpan.FromMilliseconds(100), false))
                {
                    // The connection has timed out. Abort the task just created.
                    var isConnected = con.IsConnected;
                    var isCompleted = ftpTask.IsCompleted;
                    ftpTask.Cancel();
                    Console.WriteLine("Attempted to cancel: con.IsConnected = {0}, ftpTask.IsCompleted = {1}", isConnected, isCompleted);
                }
                else
                {
                    con.EndConnect(ftpTask);
                }
            }
            catch (Exception exc)
            {
                Console.WriteLine(exc);
                //ftpTask.AsyncWaitHandle.Close();
            }

            var files = con.GetFileInfos().Take(10);
            foreach (FTPFile file in files)
            {
                Console.Out.WriteLine(file.Name);
            }
            con.Close();
            Console.WriteLine("Press any key ...");
            Console.ReadKey();
        }
    }
}

运行它会输出以下内容:

Attempted to cancel: con.IsConnected = False, ftpTask.IsCompleted = False
CFCA
CFWI_PUB_COST_Docs
ECFT_Model_From_SFWMD
GWIS
Lake_Hancock_Field_Office_Project
PRINTSHOP
README.txt
RRWPI
RWSP
amr
Press any key ...

对于我的生活,我不明白为什么?我预计它会失败,因为我应该取消它,不是吗?

怎么了?

4

1 回答 1

0

连接不是常规任务,不能以这种方式取消。请改用 Close(true),因为这将强制关闭套接字。此外,您应该能够使用 Timeout 属性来实现相同的效果。这也将允许您调用同步方法 Connect,这将简化您的代码。

于 2014-07-24T01:07:09.377 回答