So I am making a windows explorer like program and I want to implement file copying with streams ... so here i what I end up with:

try
        {
            using (Stream input = new FileStream(@"F:\dmd-sepdawn.avi", FileMode.Open))
            using (Stream output = new FileStream(@"c:\test\dmd-sepdawn.avi", FileMode.OpenOrCreate))
            {
                int bufLength = 1024 * 1024;
                byte[] buffer = new byte[bufLength];
                int readCount = input.Read(buffer, 0, buffer.Length);

                while (readCount > 0)
                {
                    output.Write(buffer, 0, readCount);
                    readCount = input.Read(buffer, 0, 1024);
                }
            }
        }
        catch (Exception ex)
        {
            Console.WriteLine(ex.ToString());
        }

Everything is working fine though but then I came across this article
http://msdn.microsoft.com/en-us/magazine/cc337900.aspx ... it got my full attention because it's done in asynchronous way so I decided to implement this one into my code but damn ... for some purpose it does not work ???

Does someone possibly know where it the catch?

Here is the full code of it's procedure:

using System;
using System.Collections.Generic;
using System.IO;
using System.Text;
using System.Threading;
using System.ComponentModel;

public class MainClass
{
    public static void Main()
    {
        Stream input = new FileStream(@"F:\Filmi\September.Dawn.DVDRip.XviD-DiAMOND\dmd-sepdawn.avi", FileMode.Open);
        Stream output = new FileStream(@"c:\test\dmd-sepdawn.avi", FileMode.OpenOrCreate);
        
        CopyStreamToStream(input, output, (src, dst, exc) =>
        {
            src.Close();
            dst.Close();
        });
        
    }

    public static void CopyStreamToStream(Stream source, Stream destination, Action<Stream, Stream, Exception> completed)
    {
        byte[] buffer = new byte[0x1000];
        AsyncOperation asyncOp = AsyncOperationManager.CreateOperation(null);

        Action<Exception> done = e =>
        {
            if (completed != null) asyncOp.Post(delegate
                {
                    completed(source, destination, e);
                }, null);
        };

        AsyncCallback rc = null;
        rc = readResult =>
        {
            try
            {
                int read = source.EndRead(readResult);
                if (read > 0)
                {
                    destination.BeginWrite(buffer, 0, read, writeResult =>
                    {
                        try
                        {
                            destination.EndWrite(writeResult);
                            source.BeginRead(
                                buffer, 0, buffer.Length, rc, null);
                        }
                        catch (Exception exc) { done(exc); }
                    }, null);
                }
                else done(null);
            }
            catch (Exception exc) { done(exc); }
        };

        source.BeginRead(buffer, 0, buffer.Length, rc, null);
    }

Check the "Output" window of your IDE to see if an unhandled exception is being thrown. That method doesn't report progress so perhaps the file is still copying? It worked fine for me.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.