如何修复UriFormatException消息i';当我试图把一个文本文件上传到我的ftp时

本文关键字:一个 文本 文件 ftp 我的 消息 UriFormatException 何修复 | 更新日期: 2023-09-27 18:29:42

这是我的代码,我正试图将文件上传到我的ftp:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.IO;
using System.Net;
namespace mws
{
    class FtpFileUpload
    {
        static string ftpurl = "ftp.newsxpressmedia.com";
        static string filename = @"c:'temp'FtpTestFile.txt";
        static string ftpusername = "myusername";
        static string ftppassword = "mypassword";
        static string value;
        public static void test()
        {
            try
            {
                FtpWebRequest ftpClient = (FtpWebRequest)FtpWebRequest.Create(new Uri(ftpurl +
                                            ftpusername + "_" + filename));
                ftpClient.Credentials = new System.Net.NetworkCredential(ftpusername, ftppassword);
                ftpClient.Method = System.Net.WebRequestMethods.Ftp.UploadFile;
                ftpClient.UseBinary = true;
                ftpClient.KeepAlive = true;
                System.IO.FileInfo fi = new System.IO.FileInfo(filename);
                ftpClient.ContentLength = fi.Length;
                byte[] buffer = new byte[4097];
                int bytes = 0;
                int total_bytes = (int)fi.Length;
                System.IO.FileStream fs = fi.OpenRead();
                System.IO.Stream rs = ftpClient.GetRequestStream();
                while (total_bytes > 0)
                {
                    bytes = fs.Read(buffer, 0, buffer.Length);
                    rs.Write(buffer, 0, bytes);
                    total_bytes = total_bytes - bytes;
                }
                fs.Close();
                rs.Close();
                FtpWebResponse uploadResponse = (FtpWebResponse)ftpClient.GetResponse();
                value = uploadResponse.StatusDescription;
                uploadResponse.Close();
            }
            catch (Exception err)
            {
                string t = err.ToString();
            }
        }
    }
}

异常出现在线路上:

FtpWebRequest ftpClient = (FtpWebRequest)FtpWebRequest.Create(new Uri(ftpurl +
                                        ftpusername + "_" + filename));

这是异常消息:

System.UriFormatException was caught
  HResult=-2146233033
  Message=Invalid URI: The URI scheme is not valid.
  Source=System
  StackTrace:
       at System.Uri.CreateThis(String uri, Boolean dontEscape, UriKind uriKind)
       at System.Uri..ctor(String uriString)
       at mws.FtpFileUpload.test() in d:'C-Sharp'Download File'Downloading-File-Project-Version-012'Downloading File'FtpFileUpload.cs:line 22
  InnerException: 

如何修复UriFormatException消息i';当我试图把一个文本文件上传到我的ftp时

您的ftp URL应该是这样的:ftp://ftp.newsxpressmedia.com