将文件上传到 ftp 时,C# windows 应用程序不支持给定的路径格式

the given path formate is not supported in C# windows application while uploading files to ftp

我想将文件上传到 ftp,但出现错误 不支持给定路径的格式。 我在下面提到了我的代码,请帮助我。 给出错误的路径,我需要什么来保持路径?

以下是我的路径 this.path = @"ftp://ip address/Requests/";

   public bool UploadDocsToFTP(string SourceFilePath, string FileName)
    {

        string ServerUri = "";
        string FTPUserName = "";
        string FTPPassword = "";
        string ftpURI = "";

        try
        {
            try
            {
                ServerUri = ConfigurationManager.AppSettings["LinuxFileServerUri"];
                FTPUserName = ConfigurationManager.AppSettings["LinuxFtpUserName"];
                FTPPassword = ConfigurationManager.AppSettings["LinuxFtpPassword"];

                string[] splitfilename = SourceFilePath.Split('\');
                //String businesRegId = splitfilename[2];
                //String userRegId = splitfilename[3];
                //String folderType = splitfilename[3];

                //ftpURI = "ftp://" + ServerUri + "//" + businesRegId + "//" + userRegId;
                //if (!string.IsNullOrEmpty(folderType))
                //    ftpURI += "//" + folderType;

                //ServerUri = "ftp://" + ServerUri + "//" + businesRegId + "//" + userRegId + "//";
                //if (!string.IsNullOrEmpty(folderType))
                //    ServerUri += folderType + "//";
                // SetMethodRequiresCWD();

                FtpWebRequest request = (FtpWebRequest)WebRequest.Create("ftp://ip address/Requests" + FileName);
                request.Method = WebRequestMethods.Ftp.UploadFile;
                request.Credentials = new NetworkCredential(FTPUserName, FTPPassword);

                FileStream fs = File.OpenRead(SourceFilePath + FileName);
                byte[] buffer = new byte[fs.Length];
                fs.Read(buffer, 0, buffer.Length);
                fs.Close();

                Stream ftpstream = request.GetRequestStream();
                ftpstream.Write(buffer, 0, buffer.Length);
                ftpstream.Close();
            }
            catch (WebException e)
            {
                String status = ((FtpWebResponse)e.Response).StatusDescription;

                if (UploadDocsToFTP_DirNotExists(SourceFilePath, FileName))
                {
                    return true;
                }

                return false;
            }

        }
        catch (Exception ex)
        {
            ex.ToString();
            return false;
        }
        return true;
    }

A URL(这是你的路径)不能包含空格或其他特定字符,因此你必须对其进行编码。

您可以为此使用 System.Net.WebUtility

// instead of this: 
// FtpWebRequest request = (FtpWebRequest)WebRequest.Create("ftp://ip address/Requests" + FileName);

// do this: 
string path = System.Net.WebUtility.UrlEncode("ftp://ip address/Requests" + FileName);   
FtpWebRequest request = (FtpWebRequest)WebRequest.Create(path);