upload text file to 5 ftp servers

kiran

New member
Joined
Jul 24, 2014
Messages
1
Programming Experience
Beginner
I am newbie to c# and working on this code to upload single text file (csv file) to 5 (cameras) ftp locations. I am working on this code it works well for one site but how can i do that for 4 other (cameras) ftp locations.how to pass mutliple ftp adddress in an array

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]// Copy 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]);
        }
    }
}
 

jmcilhinney

C# Forum Moderator
Staff member
Joined
Apr 23, 2011
Messages
3,134
Location
Sydney, Australia
Programming Experience
10+
You would write a method that took the location as an argument and then uploaded the file to that location. You would then call that method five times, passing a different location each time. The locations could be stored in an array and you could use a `foreach` loop to visit each one and pass it to the aforementioned method.
 
Top Bottom