簡體   English   中英

如何每分鍾將文本文件上傳到ftp?

[英]How do i upload text file to my ftp every minute?

我有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.

嘗試這個:

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

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM