Page 1 of 1

Quick look at StreamWriting and Uploading using FTP

#1 Bluezap  Icon User is offline

  • D.I.C Regular
  • member icon

Reputation: 6
  • View blog
  • Posts: 357
  • Joined: 19-January 12

Posted 28 December 2014 - 06:48 AM

This will be a very quick tutorial on -

  • Stream Writing
  • Uploading files with FTP



Stream Writing

By using the System.IO namespace here

class StreamWriting
{
    static void Main()
    {
        using (StreamWriter Writer = new StreamWriter("Example.txt"))
        {
            Writer.Write("Hello World")
        }
    }
}



Output text in "Example.txt" will be :-
Hello World

Uploading a file using FTP

Here we need to use System.IO and System.Net namespaces

public bool FtpTransfer(string fileName)
{
    string ftpAddress = "test.com";
    string username = "test";
    string password = "test";

    using (StreamReader stream = new StreamReader("C:\\" + fileName))
    {
        byte[] buffer = Encoding.Default.GetBytes(stream.ReadToEnd());

        WebRequest request = WebRequest.Create("ftp://" + ftpAddress + "/update/" + fileName);
        request.Method = WebRequestMethods.Ftp.UploadFile;
        request.Credentials = new NetworkCredential(username, password);
        Stream reqStream = request.GetRequestStream();
        reqStream.Write(buffer, 0, buffer.Length);
        reqStream.Close();
    }
}



We are using StreamReader here to get the contents of a file on a local director,then write those contents and uploading the entire stream into a server directory with the fileName

Now let's connect both of them and eliminate the need to have a local file

public bool FtpTransfer(string fileName)
{
    string ftpAddress = "test.com";
    string username = "test";
    string password = "test";

    WebRequest request = WebRequest.Create("ftp://" + ftpAddress + "/update/" + fileName);
    request.Method = WebRequestMethods.Ftp.UploadFile;
    request.Credentials = new NetworkCredential(username, password);
    Stream reqStream = request.GetRequestStream();

    using (StreamWriter writer = new StreamWriter(reqStream))
    {
        writer.WriteLine("Hello World");
    }
}



You can initiate it by using -

ftpTransfer("Example.txt");



Feel free to correct me if I'm wrong anywhere.
And hope this helps someone as I couldn't find a good resource on this matter :)/>

Also read -

How to: Write Text to a File
and
How to: Read Text from a File

This post has been edited by Skydiver: 29 December 2014 - 12:00 AM
Reason for edit:: Fixed inconsistent indentation; poor naming; and verbose code.


Is This A Good Question/Topic? 1
  • +

Replies To: Quick look at StreamWriting and Uploading using FTP

#2 farrell2k  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 874
  • View blog
  • Posts: 2,706
  • Joined: 29-July 11

Posted 29 December 2014 - 09:38 AM

Nice tutorial. Are we going to have to worry about a potential OutOfMemeoryException with ReadToEnd() for large files?
Was This Post Helpful? 0
  • +
  • -

#3 Skydiver  Icon User is online

  • Code herder
  • member icon

Reputation: 3868
  • View blog
  • Posts: 12,229
  • Joined: 05-May 12

Posted 29 December 2014 - 10:34 AM

Yes. That is why I was advising the author to look at using Stream.CopyTo().
Was This Post Helpful? 1
  • +
  • -

#4 Bluezap  Icon User is offline

  • D.I.C Regular
  • member icon

Reputation: 6
  • View blog
  • Posts: 357
  • Joined: 19-January 12

Posted 30 December 2014 - 03:42 AM

Yes, here is the revised version. Thank you for pointing that out. :bigsmile:

        {
            string ftpAddress = "";
            string username = "";
            string password = "";

            using (FileStream source = File.Open(@"C:\test.txt", FileMode.Open))
            {
                WebRequest request = WebRequest.Create("ftp://" + ftpAddress + "/" + "test.txt");
                request.Method = WebRequestMethods.Ftp.UploadFile;
                request.Credentials = new NetworkCredential(username, password);
                Stream reqStream = request.GetRequestStream();
                source.CopyTo(reqStream);
            }
        }


This post has been edited by Bluezap: 30 December 2014 - 03:43 AM

Was This Post Helpful? 0
  • +
  • -

Page 1 of 1