如何提供文件操作进度对话框(C# 编程指南)

如果在 Microsoft.VisualBasic 命名空间中使用 CopyFile(String, String, UIOption) 方法,可以在 Windows 中提供显示文件操作进度的标准对话框。

注意

以下说明中的某些 Visual Studio 用户界面元素在计算机上出现的名称或位置可能会不同。 这些元素取决于你所使用的 Visual Studio 版本和你所使用的设置。 有关详细信息,请参阅个性化设置 IDE

在 Visual Studio 中添加引用

  1. 在菜单栏上,依次选择“项目”、“添加引用”。

    此时将显示“引用管理器”对话框。

  2. 在“程序集”区域,选择“Framework”(如果尚未选择它)。

  3. 在名称列表中,选择“Microsoft.VisualBasic”复选框,然后再选择“确定”按钮以关闭对话框。

示例

以下代码将 sourcePath 指定的目录复制到 destinationPath 指定的目录。 此代码还提供了标准的对话框,其中显示在操作完成前估计的剩余时间量。

// The following using directive requires a project reference to Microsoft.VisualBasic.
using Microsoft.VisualBasic.FileIO;

class FileProgress
{
    static void Main()
    {
        // Specify the path to a folder that you want to copy. If the folder is small,
        // you won't have time to see the progress dialog box.
        string sourcePath = @"C:\Windows\symbols\";
        // Choose a destination for the copied files.
        string destinationPath = @"C:\TestFolder";

        FileSystem.CopyDirectory(sourcePath, destinationPath,
            UIOption.AllDialogs);
    }
}

请参阅