C# 是否在FTP中上载到文件夹?

C# 是否在FTP中上载到文件夹?,c#,ftp,C#,Ftp,我正在使用以下代码学习如何使用FTP加载文件。如何设置文件将上载到的路径或文件夹 using System; using System.IO; using System.Net; using System.Text; namespace Examples.System.Net { public class WebRequestGetExample { public static void Main () { // Get

我正在使用以下代码学习如何使用FTP加载文件。如何设置文件将上载到的路径或文件夹

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();
            }
        }
    }
}

该文件夹是创建
请求时设置的URL的一部分:
“ftp://www.contoso.com/test.htm“
。如果您使用
“ftp://www.contoso.com/wibble/test.htm“
然后文件将上载到名为
wibble
的文件夹中


您可能需要首先将请求与
Method=WebRequestMethods.Ftp.MakeDirectory一起使用,以创建
wibble
文件夹(如果它不存在)。

我使用的是Ftp.myserver.com/myfolder。我得到了错误-System.UriFormatException:无效的URI:无法确定URI的格式。如何修复此问题?是否在前面包含了
ftp://
?我将其设置为ftp://myserver。com/myfolder并收到错误-System.Net.WebException:无法连接到远程服务器我故意将所有这些空格放在ftp地址中,以便so不会将其转换为链接。
ftp
字符串有两个用途,因此可能需要两次:首先作为开头的“协议说明符”
“ftp://”
其次,它可能是远程服务器主机名的一部分。您以前的示例将其作为主机名的一部分,但您最近的示例没有。您是否应该使用
ftp://ftp.myserver.com/myfolder