第七色在线视频,2021少妇久久久久久久久久,亚洲欧洲精品成人久久av18,亚洲国产精品特色大片观看完整版,孙宇晨将参加特朗普的晚宴

為了賬號(hào)安全,請(qǐng)及時(shí)綁定郵箱和手機(jī)立即綁定
已解決430363個(gè)問(wèn)題,去搜搜看,總會(huì)有你想問(wèn)的

如何從 FileStream 報(bào)告進(jìn)度

如何從 FileStream 報(bào)告進(jìn)度

C#
四季花海 2023-08-20 14:36:46
我想報(bào)告加密文件的進(jìn)度,這是我的代碼,我該怎么做?using (FileStream destination = new FileStream(destinationFilename, FileMode.CreateNew, FileAccess.Write, FileShare.None))using (CryptoStream cryptoStream = new CryptoStream(destination, transform, CryptoStreamMode.Write))using (FileStream source = new FileStream(sourceFilename, FileMode.Open, FileAccess.Read, FileShare.Read)){    await source.CopyToAsync(cryptoStream);}
查看完整描述

1 回答

?
互換的青春

TA貢獻(xiàn)1797條經(jīng)驗(yàn) 獲得超6個(gè)贊

要正確執(zhí)行此操作,您需要插入另一個(gè)流來(lái)報(bào)告進(jìn)度。所以像這樣的事情...


        using (FileStream destination = new FileStream(destinationFilename, FileMode.CreateNew, FileAccess.Write, FileShare.None))

        using (CryptoStream cryptoStream = new CryptoStream(destination, transform, CryptoStreamMode.Write))

        using (FileStream source = new FileStream(sourceFilename, FileMode.Open, FileAccess.Read, FileShare.Read))

        {

            using (ProgressStream progressStream = new ProgressStream(source))

            {

                progressStream.UpdateProgress += ProgressStream_UpdateProgress;

                progressStream.CopyTo(cryptoStream);

            }

        }

ProgressStream 在哪里...


public class ProgressStream : Stream

{

    private Stream m_input = null;

    private long m_length = 0L;

    private long m_position = 0L;

    public event EventHandler<ProgressEventArgs> UpdateProgress;


    public ProgressStream(Stream input)

    {

        m_input = input;

        m_length = input.Length;

    }

    public override void Flush()

    {

        throw new System.NotImplementedException();

    }


    public override long Seek(long offset, SeekOrigin origin)

    {

        throw new System.NotImplementedException();

    }


    public override void SetLength(long value)

    {

        throw new System.NotImplementedException();

    }


    public override int Read(byte[] buffer, int offset, int count)

    {

        int n = m_input.Read(buffer, offset, count);

        m_position += n;

        UpdateProgress?.Invoke(this, new ProgressEventArgs((1.0f * m_position)/m_length));

        return n;

    }


    public override void Write(byte[] buffer, int offset, int count)

    {

        throw new System.NotImplementedException();

    }


    public override bool CanRead => true;

    public override bool CanSeek => false;

    public override bool CanWrite => false;

    public override long Length => m_length;

    public override long Position

    {

        get {  return m_position; }

        set {  throw new System.NotImplementedException();}

    }

}

ProgressEventArgs 是


public class ProgressEventArgs : EventArgs

{

    private float m_progress;


    public ProgressEventArgs(float progress)

    {

        m_progress = progress;

    }


    public float Progress => m_progress;


}

事件處理程序可能是這樣的......


    private void ProgressStream_UpdateProgress(object sender, ProgressEventArgs e)

    {

        Console.WriteLine($"Progress is {e.Progress * 100.0f}%");

    }

當(dāng)針對(duì)示例文件運(yùn)行時(shí)會(huì)產(chǎn)生......


Progress is 5.272501%

Progress is 10.545%

Progress is 15.8175%

Progress is 21.09%

Progress is 26.3625%

Progress is 31.635%

Progress is 36.9075%

Progress is 42.18%

Progress is 47.4525%

Progress is 52.72501%

Progress is 57.99751%

Progress is 63.27001%

Progress is 68.5425%

Progress is 73.815%

Progress is 79.08751%

Progress is 84.36001%

Progress is 89.63251%

Progress is 94.90501%

Progress is 100%

Progress is 100%

有很大的增強(qiáng)和優(yōu)化空間,但這是做你想做的事情的唯一有效方法。


查看完整回答
反對(duì) 回復(fù) 2023-08-20
  • 1 回答
  • 0 關(guān)注
  • 123 瀏覽

添加回答

舉報(bào)

0/150
提交
取消
微信客服

購(gòu)課補(bǔ)貼
聯(lián)系客服咨詢優(yōu)惠詳情

幫助反饋 APP下載

慕課網(wǎng)APP
您的移動(dòng)學(xué)習(xí)伙伴

公眾號(hào)

掃描二維碼
關(guān)注慕課網(wǎng)微信公眾號(hào)