在Windows 10 Universal app中C#Ftp上传?

时间:2015-09-03 16:14:14

标签: c# ftp windows-10 win-universal-app

我想使用ftp从windows 10 app上传文件,我尝试了以下代码。

    using System;
    using System.IO;
    using System.Net;
    using System.Text;

    namespace Examples.System.Net
    {
        public class WebRequestGetExample
        {
            public static void Main ()
            {
        // Get the object used to communicate with the server.
                FtpWebRequest request = (FtpWebRequest)WebRequest.Create("ftp://www.contoso.com/test.htm");
        request.Method = WebRequestMethods.Ftp.UploadFile;

        // This example assumes the FTP site uses anonymous logon.
        request.Credentials = new NetworkCredential ("anonymous","janeDoe@contoso.com");

        // Copy the contents of the file to the request stream.
        StreamReader sourceStream = new StreamReader("testfile.txt");
        byte [] fileContents = Encoding.UTF8.GetBytes(sourceStream.ReadToEnd());
        sourceStream.Close();
        request.ContentLength = fileContents.Length;

        Stream requestStream = request.GetRequestStream();
        requestStream.Write(fileContents, 0, fileContents.Length);
        requestStream.Close();

        FtpWebResponse response = (FtpWebResponse)request.GetResponse();

        Console.WriteLine("Upload File Complete, status {0}", response.StatusDescription);

        response.Close();
        }
    }
        }
    }

但是visual studio找不到名称空间FtpWebRequestWebRequestMethods。我尝试使用link中的backgroundtransfer api的另一种方法,但它说WinRT information: 'uri': Uploading content is only supported for 'http' and 'https' schemes.不是ftp。

那么有什么解决方案可以显示进度条和速度吗?

2 个答案:

答案 0 :(得分:2)

FtpWebRequest类不是UWP应用程序的商店配置文件的一部分,并且通过BackgroundTransfer API不支持FTP上载(如您所确定的)。这使您可以通过套接字实现自己的FTP协议。有关说明和示例链接,请参阅http://is.gd/okj55K

答案 1 :(得分:1)

试试这个: https://github.com/kiewic/FtpClient

如果您无法打开项目(例如使用VS 2017),只需从中获取两个相关的类。