gpt4 book ai didi

c# - 复制大文件时更新进度条

转载 作者:行者123 更新时间:2023-12-02 05:44:07 26 4
gpt4 key购买 nike

我正在寻找一种在将文件从一个位置复制到另一个位置时更新进度条的方法。

我正在 BackgroundWorker 中进行复制,并且还在后台更新进度条。我试过使用 file.length 来获取文件大小并使用它来计算百分比并以这种方式更新栏,但没有任何乐趣。

我附上了代码,非常感谢任何帮助,谢谢。

namespace Copier

{ 公共(public)部分类 Form1:表格 { 公共(public) Form1() { 初始化组件();

    // Declare for use in all methods
public string copyFrom;
public string copyTo;

private void btnCopyFrom_Click(object sender, EventArgs e)
{
// uses a openFileDialog, openFD, to chose the file to copy
copyFrom = "";

openFD.InitialDirectory = @"C:\Documents and Settings\user\My Documents";
openFD.FileName = "";

//openFD.ShowDialog();

if (openFD.ShowDialog() == DialogResult.Cancel)
{
MessageBox.Show("cancel button clicked");
}

else
{
// sets copyFrom = to the file chosen from the openFD
copyFrom = openFD.FileName;
// shows it in a textbox
txtCopyFrom.Text = copyFrom;
}
}

private void btnCopyTo_Click(object sender, EventArgs e)
{
//uses folderBrowserDialog, folderBD, to chose the folder to copy to
copyTo = "";

this.folderBD.RootFolder = System.Environment.SpecialFolder.MyComputer;
this.folderBD.ShowNewFolderButton = false;
//folderBD.ShowDialog();
//DialogResult result = this.folderBD.ShowDialog();

if (folderBD.ShowDialog() == DialogResult.Cancel)
{
MessageBox.Show("cancel button clicked");
}
else
{
// sets copyTo = to the folder chosen from folderBD
copyTo = this.folderBD.SelectedPath;
//shows it in a textbox.
txtCopyTo.Text = copyTo;
}
}

private void btnCopy_Click(object sender, EventArgs e)
{
copyBGW.RunWorkerAsync();
}

private void btnCancel_Click(object sender, EventArgs e)
{
Application.Exit();
}

//=================================================================
// BackGroundWorkers
//=================================================================

private void copyBGW_DoWork(object sender, DoWorkEventArgs e)
{
try
{
// copies file
string destinatationPath = Path.Combine(copyTo, Path.GetFileName(copyFrom));
File.Copy(copyFrom, destinatationPath);
MessageBox.Show("File Copied");
}
catch (Exception ex)
{
MessageBox.Show(ex.Message);
}
}
}

或者有人可以告诉我一种让进度条自行移动的方法,以表明表单没有卡住吗?

清理了代码

感谢到目前为止的输入

最佳答案

我认为最简单的方法是调用 CopyFileEx,它允许您指定进度处理程序,以便在复制文件时从操作系统获取更新。这是从 CopyFileEx 的 pinvoke.net 页面复制的示例代码:

[DllImport("kernel32.dll", SetLastError = true, CharSet = CharSet.Auto)]
[return: MarshalAs(UnmanagedType.Bool)]
static extern bool CopyFileEx(string lpExistingFileName, string lpNewFileName,
CopyProgressRoutine lpProgressRoutine, IntPtr lpData, ref Int32 pbCancel,
CopyFileFlags dwCopyFlags);

delegate CopyProgressResult CopyProgressRoutine(
long TotalFileSize,
long TotalBytesTransferred,
long StreamSize,
long StreamBytesTransferred,
uint dwStreamNumber,
CopyProgressCallbackReason dwCallbackReason,
IntPtr hSourceFile,
IntPtr hDestinationFile,
IntPtr lpData);

int pbCancel;

enum CopyProgressResult : uint
{
PROGRESS_CONTINUE = 0,
PROGRESS_CANCEL = 1,
PROGRESS_STOP = 2,
PROGRESS_QUIET = 3
}

enum CopyProgressCallbackReason : uint
{
CALLBACK_CHUNK_FINISHED = 0x00000000,
CALLBACK_STREAM_SWITCH = 0x00000001
}

[Flags]
enum CopyFileFlags : uint
{
COPY_FILE_FAIL_IF_EXISTS = 0x00000001,
COPY_FILE_RESTARTABLE = 0x00000002,
COPY_FILE_OPEN_SOURCE_FOR_WRITE = 0x00000004,
COPY_FILE_ALLOW_DECRYPTED_DESTINATION = 0x00000008
}

private void XCopy(string oldFile, string newFile)
{
CopyFileEx(oldFile, newFile, new CopyProgressRoutine(this.CopyProgressHandler), IntPtr.Zero, ref pbCancel, CopyFileFlags.COPY_FILE_RESTARTABLE);
}

private CopyProgressResult CopyProgressHandler(long total, long transferred, long streamSize, long StreamByteTrans, uint dwStreamNumber,CopyProgressCallbackReason reason, IntPtr hSourceFile, IntPtr hDestinationFile, IntPtr lpData)
{
return CopyProgressResult.PROGRESS_CONTINUE;
}

关于c# - 复制大文件时更新进度条,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10517869/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com