【问题标题】:How to use this Ftp Code [closed]如何使用此 Ftp 代码 [关闭]
【发布时间】:2011-06-14 03:19:40
【问题描述】:

我是新的 C#。我想知道我应该在此代码中更改什么以连接到我的 ftp 服务器。任何人都可以帮助我吗?我无法理解string fileNameUri serverUrilong offset,所以请帮助我。

ftphost address= localhost
username = test
password = test
filename = test.zip

   public static bool RestartDownloadFromServer(string fileName, Uri serverUri, long offset)
    {
        // The serverUri parameter should use the ftp:// scheme.
        // It identifies the server file that is to be downloaded
        // Example: ftp://contoso.com/someFile.txt.

        // The fileName parameter identifies the local file.
        //The serverUri parameter identifies the remote file.
        // The offset parameter specifies where in the server file to start reading data.

        if (serverUri.Scheme != Uri.UriSchemeFtp)
        {
            return false;
        }
        // Get the object used to communicate with the server.
        FtpWebRequest request = (FtpWebRequest)WebRequest.Create(serverUri);
        request.Method = WebRequestMethods.Ftp.DownloadFile;
        request.ContentOffset = offset;
        FtpWebResponse response = null;
        try
        {
            response = (FtpWebResponse)request.GetResponse();
        }
        catch (WebException e)
        {
            Console.WriteLine(e.Status);
            Console.WriteLine(e.Message);
            return false;
        }
        // Get the data stream from the response.
        Stream newFile = response.GetResponseStream();
        // Use a StreamReader to simplify reading the response data.
        StreamReader reader = new StreamReader(newFile);
        string newFileData = reader.ReadToEnd();
        // Append the response data to the local file
        // using a StreamWriter.
        StreamWriter writer = File.AppendText(fileName);
        writer.Write(newFileData);
        // Display the status description.

        // Cleanup.
        writer.Close();
        reader.Close();
        response.Close();
        Console.WriteLine("Download restart - status: {0}", response.StatusDescription);
        return true;
    }

感谢您的加入。

【问题讨论】:

  • 你需要改变它吗?如果是,为什么?
  • 如果你想学习 c# - 从阅读书籍和回顾 简单 示例开始。如果您需要完成工作,请雇用一些人。
  • 如果您不快速详细说明,您的问题将被关闭为“不是一个真正的问题”。请快速解释一下你的意思:你在什么意义上看不懂string fileName和其他参数?
  • @Lasse V. Karlsen:至少需要正确处理流和读取器/写入器。我知道你知道,但那是可以改变的 ;-)
  • 是否要下载新文件? fileName 是本地文件名,serverURIScheme 是 ftp 服务器名称和文件路径的组合,类似于 URL(你可以在 IE 中实际测试它,然后在 C# 中使用它),offset 是之前读取的字节数ftp 事件。

标签: c# ftp


【解决方案1】:

方法开头的 cmets 绰绰有余。

调用方法开始:

 RestartDownloadFromServer("ftp://localhost/test.zip", "c:\test.zip", 0);

您的示例无法处理用户名/密码。为此,您需要创建一个 NetworkCredential 并将其添加到 WebRequest。

【讨论】:

  • 谢谢先生,你能告诉我如何添加用户名和密码吗?因为我需要安全来下载文件。
【解决方案2】:

在您的代码示例中这些词是您还需要的。

// serverUri 参数应使用 ftp:// 方案。 // 它标识要下载的服务器文件 // 示例:ftp://contoso.com/someFile.txt。 // fileName 参数标识本地文件。 //serverUri参数标识远程文件。 // offset 参数指定从服务器文件中的哪个位置开始读取数据。

【讨论】:

  • 6,666 用upvote 改变如此高的声誉价值真是太可悲了;-)
  • 是的 :) Awww 什么过山车
【解决方案3】:

听起来你才刚刚开始 C#

查看以下链接。你需要先学会走路才能跑!

http://msdn.microsoft.com/en-us/beginner/bb308730.aspx

http://www.csharp-station.com/Tutorial.aspx

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-01-25
    • 2013-04-06
    • 2014-03-21
    • 1970-01-01
    相关资源
    最近更新 更多