upload file to server using C#

The following code sample shows uploading of text file "test.txt" in the app's directory to "ftp://kyrathasoft.com/testbed/test.txt" using login info bigdaddy@kyrathasoft.com and password "shitstorm":

namespace UploadExample {

class Program {

    static void Main(string[] args) {

        // Get the object used to communicate with the server.
        FtpWebRequest request = (FtpWebRequest)WebRequest.Create("ftp://kyrathasoft.com/testbed/test.txt");
        request.Method = WebRequestMethods.Ftp.UploadFile;

        request.Credentials = new NetworkCredential("bigdaddy@kyrathasoft.com", "shitstorm");

        // Copy the contents of the file to the request stream.
        StreamReader sourceStream = new StreamReader("test.txt");
        byte[] fileContents = Encoding.UTF8.GetBytes(sourceStream.ReadToEnd());
        request.ContentLength = fileContents.Length;

        Stream requestStream = request.GetRequestStream();
        requestStream.Write(fileContents, 0, fileContents.Length);

        FtpWebResponse response = (FtpWebResponse)request.GetResponse();

        Console.WriteLine("Upload File Complete, status {0}", response.StatusDescription);


        Console.WriteLine("Press a key to end program...");



Author: kyrathasoft

The only programmer in Glens Fork, Ky.