File Stream Copy

0 votes
asked Aug 16, 2012 by Bharad (320 points)
edited Aug 17, 2012

Is it possible to package using filestream and place the packaged file into a remote server by "copying". or should i place the packaged file by "FTPing" ?

Applies to: File Transfer Pack
commented Aug 16, 2012 by Jan Sotola (17,670 points)
edited Aug 16, 2012

Could you be, please, more specific? What do you meen by "FTPing" in contrast to "copying"?

commented Aug 17, 2012 by Bharad (320 points)
edited Aug 17, 2012

If I try to place the file using normal "File Copy" option, I call it copying, whereas if I try to place it on a remote FTP location and if I use FTP methodologies to place it, I call it as "FTPing"

1 Answer

0 votes
answered Aug 17, 2012 by Jan Sotola (17,670 points)
edited Aug 17, 2012

I'm not sure what do you exactly mean with "packaging a file". But once you have a filestream, you can:

  • either copy the stream to a file on local disc or on network folder using the system CopyTo method,
  • or copy the stream to an FTP server using the Rebex FTP/SSL component.

Here is an example for both ways:

using System;
using System.IO;
using Rebex.Net;

namespace Rebex.Examples
    class Program
        static void Main(string[] args)
            bool copyUsingFtp = true;

            // create a source stream 
            // (we obtain it simply by opening a file, it woud be more complicated in your application)
            using (FileStream src = File.OpenRead(@"c:\temp\file1.txt"))
                // copy the stream content to the destination...
                if (copyUsingFtp)
                    // ... etiher using FTP ...
                    FtpFile(src, "/temp/copied_file.txt");
                    // ... or using system file copy
                    CopyFile(src, @"c:\temp\copied_file.txt");

        private static void CopyFile(FileStream src, string dstFilename)
            // create stream for destination file (in "overwrite" mode)
            using (FileStream dst = new FileStream(dstFilename, FileMode.Create))
                // copy the stream using system CopyTo method (new in .NET 4.0)

        private static void FtpFile(FileStream src, string dstFilename)
            // create a Rebex FTP/SSL client object
            using (Ftp client = new Ftp())
                // connect and login to the remote FTP server
                client.Login("username", "password");

                // upload the source stream to the FTP server
                client.PutFile(src, dstFilename);

                // disconnect from the FTP server

Note: The CopyTo method is new in .NET 4.0. For older versions of .NET framework you can copy streams manually e.g according the following article.