0

I coded a file sharing app using FileStream.CopyTo copying files for me.To add Progress bar facility in it,I found a dozen of links googling and saying to use CopyFileEx for Progress bar using CallBack method .After a detailed reading of MSDN docs.I found another option WebClient for the same purpose and confused me rather more.I then skimmed through here where the author doesn't talk about Stream.CopyTo but advocating CopyFileEx too.

I am already done with coding using CopyTo method.For progress bar will it provide me facility and how?

PS. I searched a lot but none of link showed me how to implement it using CopyTo.

Here's my code For Client-side:

         NetworkStream netstream = clientSocket.GetStream();


        using (FileStream fs = new FileStream(path, FileMode.Open, FileAccess.Read))
        {
            try
            {
                TransmitFileName(netstream, Path.GetFileName(path));

              //  MessageBox.Show("Starting Data Transfer");

                int data_len = (int)fs.Length;
                byte[] buffer = new byte[bufferSize];
                int totalbytes = 0;
                while (totalbytes < data_len)
                {

                    var bytesread = fs.Read(buffer, 0, buffer.Length);
                    if (totalbytes == data_len) { break; }
                    try
                    {
                        netstream.Write(buffer, 0, bytesread);
                        totalbytes += bytesread;
                    }
                    catch (Exception ex)
                    {
                        MessageBox.Show(ex.Message);

                    }
                 }

               string suff_tb= SizeSuffix(totalbytes);

                MessageBox.Show("Total Bytes Receieved" + suff_tb);

            }
            catch (Exception ex)
            {

                MessageBox.Show(ex.ToString());

            }
            finally
            {
                MessageBox.Show("Data transfer completed");
                fs.Close();
                netstream.Close();

            }

        }

Server-side:

        Directory.CreateDirectory(Path.GetDirectoryName(fileloc));
            //copy data from client machine to local drive

            try
            {
                using (FileStream fs = new FileStream(fileloc, FileMode.OpenOrCreate, FileAccess.Write))
                {
                    netStream.CopyTo(fs);

                }
            }
            catch (Exception exx)
            {
                MessageBox.Show(exx.ToString());

            }
            finally
            {
                netStream.Close();
                FileInfo fi = new FileInfo(fileloc);
                long siz = fi.Length;
                MessageBox.Show("File Size is" + SizeSuffix(siz));


            }
  • The answer is No. It can be Yes if you have to copy multiple files (then you have total number of files and copying them in cycle gives you progress). Problem is what `CopyTo` doesn't gives you any way to determine progress. It is started and when its finished - file is 100% copied. You can use *progressless indicator* (animated gif of something rotating, jumping, whatever). Or you have to use other ways. WinAPI `CopyFileEx` has `CopyProgressRoutine` callback, in which you **can** estimate progress of currently copied single file. – Sinatr Jan 12 '15 at 11:09
  • Progressless indicator..........will u elaborate? – yeung Leone Jan 12 '15 at 11:52
  • Have u a working example of `CopyFileEx` with Progress bar...i need it @Sinatr – yeung Leone Jan 12 '15 at 11:55
  • Couldn't i achieve something(other than CopyFileEx)like suggested here?-->http://stackoverflow.com/questions/10354610/copyfileex-the-parameter-is-invalid-error – yeung Leone Jan 12 '15 at 12:08
  • [Progressless](http://i.stack.imgur.com/r6tWv.gif) indicator. [CopyFileEx example](http://www.codeproject.com/Articles/36647/How-to-copy-files-in-C-with-a-customizable-progres). – Sinatr Jan 12 '15 at 12:10

0 Answers0