C# Keep track of the amount of written bytes
<p>I was looking into writing data and I used:</p>

<pre><code>System.IO.File.WriteAllBytes(path, GetBytes());
</code></pre>

<p>This worked for me but then I wanted to get the progress of the written bytes so I can have a progressbar added to it. I found out that my method can't connect to a progressbar so I decided to use this:</p>

<pre><code>using (BinaryWriter writer = new BinaryWriter(System.IO.File.Open(path, FileMode.Create))) {
            byte[] nextBytes = GetBytes(); // your logic to get what to write;
            writer.Write(nextBytes);
        }
</code></pre>

<p>This writes my file succesfully but after a lot of searching (maybe not enough) I can't seem to find out how to keep track of the amount of bytes being written until it's complete. Maybe I'm using a wrong method, if anyone could help me out, would be awesome! (I use a WPF)</p>

<p>Thanks in advance,</p>

<p><strong>Update:</strong></p>

<p>On top of the project I delcare BackgroundWorker()</p>

<pre><code>    BackgroundWorker bw;
</code></pre>

<p>Then when I start the download I haev this:</p>

<pre><code>bw = new BackgroundWorker();
            bw.WorkerReportsProgress = true;
            bw.WorkerSupportsCancellation = true;
            bw.DoWork += new DoWorkEventHandler(bw_DoWork);
            bw.ProgressChanged += new ProgressChangedEventHandler(bw_ProgressChanged);

            bw.RunWorkerAsync(data);
</code></pre>

<p>My doWork method:</p>

<pre><code>        private void bw_DoWork (object sender, DoWorkEventArgs e) {
        downloadData data = (downloadData) e.Argument;
        _source = data.DownloadLocation + @"\";

        sem.WaitOne();

        //enters
        updateStatusBar("- Downloading -", data, 0);
        Video vid = youtube.GetVideo(data.Url);
        byte[] bytes = vid.GetBytes();

        using (var writer = new BinaryWriter(System.IO.File.Open(_source + vid.FullName, FileMode.Create))) {

            // do your read logic here and read into bytes

            var bytesLeft = bytes.Length;
            var bytesWritten = 0;
            while (bytesLeft &gt; 0) {
                int chunk = Math.Min(64, bytesLeft);

                writer.Write(bytes, bytesWritten, chunk);
                bytesWritten += chunk;
                bytesLeft -= chunk;

                // calculate progress bar status
                bw.ReportProgress(bytesWritten * 100 / bytes.Length, data);
            }
        }
        // Leaves
        updateStatusBar("- Done -", data, 100);

        sem.Release();
    }
</code></pre>

<p>My Progresschanged method:</p>

<pre><code>    private void bw_ProgressChanged (object sender, ProgressChangedEventArgs e) {
        YoutubeData data = (YoutubeData) e.UserState;
        updateStatusBar("Downloading: " + e.ProgressPercentage, data, e.ProgressPercentage);
    }
</code></pre>

<p>Method for updating status bar:</p>

<pre><code>        private void updateStatusBar(string txt, downloadData data, int progress) {
        _downloadData.Remove(data);
        data.ProgressText = txt;
        data.Status = progress;
        _downloadData.Add(data);

        Dispatcher.Invoke(new Action(() =&gt; {
            lv_DownloadData.ItemsSource = null;
            lv_DownloadData.ItemsSource = _youtubeData;
        }));
    }
</code></pre>

<p>But now it freezes my WPF.</p>