将文本文件上传到5个ftp服务器

基兰

新成员
已加入
2014年7月24日
留言内容
1
编程经验
Beginner
我是C#的新手,正在研究此代码以将单个文本文件(csv文件)上传到5个(摄像机)ftp位置。我正在这段代码上工作,它在一个站点上很好用,但如何在其他4个(摄像机)ftp位置上做到这一点。如何在数组中传递多点ftp地址

C#:
[COLOR=#00008B]private[/COLOR] [COLOR=#00008B]void[/COLOR] button1_Click([COLOR=#00008B]object[/COLOR] sender, [COLOR=#2B91AF]EventArgs[/COLOR] e)
        {
           [COLOR=#2B91AF]UploadBlacklistFile[/COLOR]([COLOR=#00008B]string[/COLOR] ftpServerUrl,[COLOR=#00008B]string[/COLOR] filename);

        }
             [COLOR=#00008B]void[/COLOR] [COLOR=#2B91AF]UploadBlacklistFile[/COLOR]([COLOR=#00008B]string[/COLOR] ftpServerUrl,[COLOR=#00008B]string[/COLOR] filename)
            {
            [COLOR=#2B91AF]Boolean[/COLOR] ftpMode = [COLOR=#00008B]true[/COLOR];

            [COLOR=#808080]//// Get the object used to communicate with the server.[/COLOR]
            [COLOR=#808080]//FtpWebRequest request = (FtpWebRequest)WebRequest.Create("ftp://158.125.161.83");[/COLOR]
            [COLOR=#808080]//request.Method = WebRequestMethods.Ftp.UploadFileWithUniqueName;[/COLOR]

            [COLOR=#808080]//// This example assumes the FTP site uses anonymous logon.[/COLOR]
            [COLOR=#808080]//request.Credentials = new NetworkCredential("superuser", "superuser");[/COLOR]

            [COLOR=#00008B]if[/COLOR] (ftpMode == [COLOR=#00008B]true[/COLOR])
            {
                request.[COLOR=#2B91AF]KeepAlive[/COLOR] = [COLOR=#00008B]false[/COLOR];
            }
            [COLOR=#808080]// 复制 the contents of the file to the request stream.[/COLOR]
            [COLOR=#2B91AF]StreamReader[/COLOR] sourceStream = [COLOR=#00008B]new[/COLOR] [COLOR=#2B91AF]StreamReader[/COLOR]([COLOR=#800000]"C:/blacklist/ticketsblacklist.csv"[/COLOR]);
            [COLOR=#00008B]byte[/COLOR][] fileContents = [COLOR=#2B91AF]Encoding[/COLOR].UTF8.[COLOR=#2B91AF]GetBytes[/COLOR](sourceStream.[COLOR=#2B91AF]ReadToEnd[/COLOR]());
            sourceStream.[COLOR=#2B91AF]Close[/COLOR]();
            request.[COLOR=#2B91AF]ContentLength[/COLOR] = fileContents.[COLOR=#2B91AF]Length[/COLOR];

            [COLOR=#2B91AF]Stream[/COLOR] requestStream = request.[COLOR=#2B91AF]GetRequestStream[/COLOR]();
            requestStream.[COLOR=#2B91AF]Write[/COLOR](fileContents, [COLOR=#800000]0[/COLOR], fileContents.[COLOR=#2B91AF]Length[/COLOR]);
            requestStream.[COLOR=#2B91AF]Close[/COLOR]();

            [COLOR=#2B91AF]FtpWebResponse[/COLOR] response = ([COLOR=#2B91AF]FtpWebResponse[/COLOR])request.[COLOR=#2B91AF]GetResponse[/COLOR]();

            [COLOR=#2B91AF]Console[/COLOR].[COLOR=#2B91AF]WriteLine[/COLOR]([COLOR=#800000]"Upload File Complete, status {0}"[/COLOR], response.[COLOR=#2B91AF]StatusDescription[/COLOR]);

            response.[COLOR=#2B91AF]Close[/COLOR]();

    [COLOR=#00008B]string[/COLOR] [] ftpaddress = ({[COLOR=#800000]"100.100.100.85"[/COLOR],[COLOR=#800000]"parm"[/COLOR],[COLOR=#800000]"parm"[/COLOR]};{[COLOR=#800000]"101.101.101.85"[/COLOR],[COLOR=#800000]"parm1"[/COLOR],[COLOR=#800000]"parm1"[/COLOR]})

    [COLOR=#00008B]foreach[/COLOR]([COLOR=#00008B]string[/COLOR] addr [COLOR=#00008B]in[/COLOR] ftpaddress)
{
    uploadFile(addr, [COLOR=#800000]@"C:/blacklist/ticketsblacklist.csv"[/COLOR]);
        }
    }
}
 

金西尼

C#论坛主持人
工作人员
已加入
2011年4月23日
留言内容
3,491
地点
悉尼,澳大利亚
编程经验
10+
您将编写一个将位置作为参数的方法,然后将文件上传到该位置。然后,您将调用该方法五次,每次都传递一个不同的位置。这些位置可以存储在一个数组中,您可以使用“ foreach”循环访问每个数组并将其传递给上述方法。
 
最佳 底部