none
複製FTP伺服器上面的文件 RRS feed

  • 問題

  • 請問有什麼辦法可以複製FTP伺服器上面的文件??

    麻煩如果可以請附上FTPSERVER有設定密碼時如何加上帳號密碼的字串

    2007年10月14日 下午 04:47

解答

  • 這個是我之前寫FTP參考的資料,供參考http://msdn2.microsoft.com/zh-tw/library/ms233623.aspx

     

    而以下是之後寫出來的程式

    程式碼區塊

    internal bool Download(string downloadUrl,string TargetPath, string UserName, string Password)
    {
        Stream responseStream = null;
        FileStream fileStream = null;
        StreamReader reader = null;
        try
        {
            FtpWebRequest downloadRequest = (FtpWebRequest)WebRequest.Create(downloadUrl);
            if (UserName.Length > 0)
            {
                NetworkCredential nc = new NetworkCredential(UserName, Password);
                downloadRequest.Credentials = nc;
            }
            FtpWebResponse downloadResponse = (FtpWebResponse)downloadRequest.GetResponse();
            responseStream = downloadResponse.GetResponseStream();

            string fileName = Path.GetFileName(downloadRequest.RequestUri.AbsolutePath);

            if (fileName.Length == 0)
            {
                reader = new StreamReader(responseStream);
                throw new Exception(reader.ReadToEnd());
            }
            else
            {
                fileStream = File.Create(TargetPath+@"\"+fileName);
                byte[] buffer = new byte[1024];
                int bytesRead;
                while (true)
                {
                    bytesRead = responseStream.Read(buffer, 0, buffer.Length);
                    if (bytesRead == 0)
                    break;
                    fileStream.Write(buffer, 0, bytesRead);
                }
            }
            return true;
        }
        catch (UriFormatException ex)
        {
            throw new Exception(ex.Message);
        }
        catch (WebException ex)
        {
            throw new Exception(ex.Message);
        }
        catch (IOException ex)
        {
            throw new Exception(ex.Message);
        }
        finally
        {
            if (reader != null)
            reader.Close();
            else if (responseStream != null)
            responseStream.Close();
            if (fileStream != null)
            fileStream.Close();
        }
    }

     

     

    2007年10月15日 上午 03:19

所有回覆

  • Dear 小傅:

    你可以參考這個網站

    Code Project:C# FTP Client Library

    或是

    net 2.0(c#)下简单的FTP应用程序

     

    希望對你有所幫助

    Kevin, Tsui

     

     

    2007年10月14日 下午 06:47
  • 這個是我之前寫FTP參考的資料,供參考http://msdn2.microsoft.com/zh-tw/library/ms233623.aspx

     

    而以下是之後寫出來的程式

    程式碼區塊

    internal bool Download(string downloadUrl,string TargetPath, string UserName, string Password)
    {
        Stream responseStream = null;
        FileStream fileStream = null;
        StreamReader reader = null;
        try
        {
            FtpWebRequest downloadRequest = (FtpWebRequest)WebRequest.Create(downloadUrl);
            if (UserName.Length > 0)
            {
                NetworkCredential nc = new NetworkCredential(UserName, Password);
                downloadRequest.Credentials = nc;
            }
            FtpWebResponse downloadResponse = (FtpWebResponse)downloadRequest.GetResponse();
            responseStream = downloadResponse.GetResponseStream();

            string fileName = Path.GetFileName(downloadRequest.RequestUri.AbsolutePath);

            if (fileName.Length == 0)
            {
                reader = new StreamReader(responseStream);
                throw new Exception(reader.ReadToEnd());
            }
            else
            {
                fileStream = File.Create(TargetPath+@"\"+fileName);
                byte[] buffer = new byte[1024];
                int bytesRead;
                while (true)
                {
                    bytesRead = responseStream.Read(buffer, 0, buffer.Length);
                    if (bytesRead == 0)
                    break;
                    fileStream.Write(buffer, 0, bytesRead);
                }
            }
            return true;
        }
        catch (UriFormatException ex)
        {
            throw new Exception(ex.Message);
        }
        catch (WebException ex)
        {
            throw new Exception(ex.Message);
        }
        catch (IOException ex)
        {
            throw new Exception(ex.Message);
        }
        finally
        {
            if (reader != null)
            reader.Close();
            else if (responseStream != null)
            responseStream.Close();
            if (fileStream != null)
            fileStream.Close();
        }
    }

     

     

    2007年10月15日 上午 03:19