File Copy with Progress Bar

后端 未结 6 702
有刺的猬
有刺的猬 2020-11-27 02:58

I used this code:

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Windows.Forms;
using System.IO;

namespace Windo         


        
6条回答
  •  陌清茗
    陌清茗 (楼主)
    2020-11-27 03:54

    I like this solution, because

    The copy engine is in the framework

    public delegate void IntDelegate(int Int);
    
    public static event IntDelegate FileCopyProgress;
    public static void CopyFileWithProgress(string source, string destination)
    {
        var webClient = new WebClient();
        webClient.DownloadProgressChanged += DownloadProgress;
        webClient.DownloadFileAsync(new Uri(source), destination);
    }
    
    private static void DownloadProgress(object sender, DownloadProgressChangedEventArgs e)
    {
        if(FileCopyProgress != null)
            FileCopyProgress(e.ProgressPercentage);
    }
    

    UNC Paths

    This should work with UNC paths as long as the permissions are set up. If not, you will get this error, in which case, I vote for the authenticated request user route.

    System.UnauthorizedAccessException: Access to the path '\testws01\c$\foo' is denied.

    ASP.NET is not authorized to access the requested resource. Consider granting access rights to the resource to the ASP.NET request identity. ASP.NET has a base process identity (typically {MACHINE}\ASPNET on IIS 5 or Network Service on IIS 6 and IIS 7, and the configured application pool identity on IIS 7.5) that is used if the application is not impersonating. If the application is impersonating via , the identity will be the anonymous user (typically IUSR_MACHINENAME) or the authenticated request user.

提交回复
热议问题