c#跟踪写入的字节数

本文关键字:字节数 跟踪 | 更新日期: 2023-09-27 18:12:51

我正在寻找写入数据,我使用:

System.IO.File.WriteAllBytes(path, GetBytes());

这对我有用,但后来我想得到写入字节的进度,所以我可以有一个进度条添加到它。我发现我的方法不能连接到进度条,所以我决定使用这个:

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);
        }

这写我的文件成功,但经过大量的搜索(也许不够),我似乎不能找出如何保持跟踪字节数写入,直到它完成。也许我用错了方法,如果有人能帮我,那就太棒了!(我使用WPF)

提前感谢,

更新:

在项目的顶部,我delcare BackgroundWorker()

    BackgroundWorker bw;

当我开始下载时,我看到了这个:

bw = new BackgroundWorker();
            bw.WorkerReportsProgress = true;
            bw.WorkerSupportsCancellation = true;
            bw.DoWork += new DoWorkEventHandler(bw_DoWork);
            bw.ProgressChanged += new ProgressChangedEventHandler(bw_ProgressChanged);
            bw.RunWorkerAsync(data);

My doWork方法:

        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 > 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();
    }

My Progresschanged method:

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

状态栏更新方法:

        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(() => {
            lv_DownloadData.ItemsSource = null;
            lv_DownloadData.ItemsSource = _youtubeData;
        }));
    }

但是现在它冻结了我的WPF

c#跟踪写入的字节数

将字节写入块中,并使用BackgroundWorker

    using(var writer = new BinaryWriter(System.IO.File.Open(path, FileMode.Create)))) {
       // do your read logic here and read into bytes
        var bytesLeft = bytes.Length;
        var bytesWritten = 0;
        while(bytesLeft > 0) {
            int chunk = Math.Min(64, bytesLeft);
            writer.WriteBytes(array, bytesWritten, chunk);
            bytesWritten += chunk;
            bytesLeft -= chunk;
            // calculate progress bar status
            backgroundWorker.ReportProgress(bytesWritten * 100 / array.Length);
        }
    }

OK,这是完全未经测试的…但请留在我身边…每个BinaryWriter将在BackgroundWorker上做它的工作

设置后台worker

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

然后你需要在某处使用bw.RunWorkerAsync()启动BackgroundWorker

void bw_DoWork(object sender, DoWorkEventArgs e)
{
    // Do your task here
     // report its progress
    bw.ReportProgress(..); 
}
void bw_ProgressChanged(object sender, ProgressChangedEventArgs e)
{
    // update your progress bar here
}