【问题标题】:How do i upload text file to my ftp every minute?如何每分钟将文本文件上传到我的 ftp?
【发布时间】:2014-03-22 19:24:34
【问题描述】:

我有 ftp 服务器,我想每分钟使用一个计时器将相同的文本文件上传到 ftp。 对于使用此方法的测试我:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.IO;
using System.Net;

namespace ScrollLabelTest
{
    class FtpFileUploader
    {
        static string ftpurl = "ftp://ftp.test.com/";
        static string filename = @"c:\temp\test.txt";
        static string ftpusername = "name";
        static string ftppassword = "pass";
        static string value;

        public static void test()
        {
            try
            {
                FtpWebRequest ftpClient = (FtpWebRequest)FtpWebRequest.Create(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.Flush();
                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(ftpurl + "" + ftpusername + "_" + filename);

例外:

Invalid URI: Invalid port specified.

【问题讨论】:

    标签: c# ftp


    【解决方案1】:

    试试这个:

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

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2011-01-08
      • 1970-01-01
      • 1970-01-01
      • 2016-12-14
      • 2011-09-25
      • 2014-10-17
      • 1970-01-01
      相关资源
      最近更新 更多