Ejemplo n.º 1
0
        /// <summary>
        /// Connects to the FTP server
        /// </summary>
        public override void Connect()
        {
            // Init wininet and get the base handle
            m_hInternet = WinInet.InternetOpen(
                null,                           // The 'user-agent' making the request
                INTERNET_OPEN.TYPE_PRECONFIG,   // Use the default proxy settings
                null,                           // proxy name (no proxy)
                null,                           // proxy bypass (no bypass)
                0                               // Flags for the connection
                );

            // Validate the connection and throw an exception if necessary
            ValidateConnection(m_hInternet);

            // Get a FTP handle to use to put files
            m_hFtp = WinInet.InternetConnect(
                m_hInternet,                    // The internet connection to use
                m_ftpAddress,
                m_ftpPort,                      // The FTP port number
                m_userName,
                m_passWord,
                INTERNET_SERVICE.FTP,           // Open the connection for this type of service
                FtpConnectionFlags,             // FTP Flags
                0                               // Application Context
                );

            // Validate the handle and throw an exception if necessary
            ValidateConnection(m_hFtp);

            //save the home dir
            HomeDir = WinInet.GetFtpCurrentDirectory(m_hFtp);

            // Set the current directory to the path.  If the path is empty, don't set
            // the current directory as this is invalid.
            if (m_path != "")
            {
                if (!m_path.StartsWith("/", StringComparison.OrdinalIgnoreCase))
                {
                    string homeDir = HomeDir;
                    if (!homeDir.EndsWith("/", StringComparison.OrdinalIgnoreCase))
                    {
                        homeDir = homeDir + "/";
                    }
                    m_path = homeDir + m_path + "/";
                }

                if (!m_path.EndsWith("/", StringComparison.OrdinalIgnoreCase))
                {
                    //Mindspring's FTP behaves strangely if the current
                    //directory isn't set with a trailing "/"
                    m_path += "/";
                }

                if (!RawDirectoryExists(m_path))
                {
                    if (!(HomeDir != null && HomeDir.StartsWith(m_path, StringComparison.OrdinalIgnoreCase) &&
                          m_path != "" && m_path.EndsWith("/", StringComparison.OrdinalIgnoreCase)))
                    {
                        throw new NoSuchDirectoryException(m_path);
                    }
                }

                // Some FTP servers don't let you cwd to /, but we
                // still need to use it as our m_path.
                //
                // ValidateFTPCommand(pushDirectory(m_path));
            }
            else
            {
                //remember what the default working directory assigned by the server was.
                m_path = HomeDir;
            }
        }