c# - WebClient 下载文件到 0KB?

标签 c# webclient

我需要覆盖的文件位于我的本地计算机上。我正在检索的文件来 self 的 FTP 服务器。这些文件的名称都相同,但字节不同,例如,它们已更新。

我使用本地计算机上的文件作为目标文件 - 这意味着我使用它们的名称可以轻松地在 FTP 服务器上找到它们。

这是我编写的代码:

private void getFiles () {

    string startupPath = Application.StartupPath;
    /*
     * This finds the files within the users installation folder
     */
    string[] files = Directory.GetFiles(startupPath + "\\App_Data", "*.*",
    SearchOption.AllDirectories);

    foreach (string s in files)
    {
        /*
         * This gets the file name
         */
        string fileName = Path.GetFileName(s);
        /*
         * This gets the folder and subfolders after the main directory
         */
        string filePath = s.Substring(s.IndexOf("App_Data"));
        downloadFile("user:<a href="https://stackoverflow.com/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="b1c1d0c2c2f1dcc8c2d8c5d49fc5da" rel="noreferrer noopener nofollow">[email protected]</a>/updates/App_Data/" + fileName,
        startupPath + "\\" + filePath);
    }
}

private void downloadFile (string urlAddress, string location)
{
    using (WebClient webClient = new WebClient())
    {
        System.Uri URL = new System.Uri("ftp://" + urlAddress);
        webClient.DownloadFileAsync(URL, location);
    }
}

代码完成后,由于某种原因,子文件夹中的文件显示为0KB。这很奇怪,因为我知道我的 FTP 服务器上的每个文件都大于 0KB。

我的问题是:为什么子文件夹中的文件显示为 0KB?

如果这篇文章有任何不清楚的地方,请告诉我,我会尽力澄清。

最佳答案

在回答评论中的问题时,以下是一种可能的方法,但是尚不清楚 getFiles 是否应该是阻塞方法。在我的示例中,我假设是这样(在所有下载完成之前该方法不会退出)。我不确定其功能,因为这是我凭空写下的,但它是一个总体想法。

private void getFiles () {

    string startupPath = Application.StartupPath;
    /*
     * This finds the files within the users installation folder
     */
    string[] files = Directory.GetFiles(startupPath + "\\App_Data", "*.*",
        SearchOption.AllDirectories);
    using (WebClient client = new WebClient())
    {
        int downloadCount = 0;
        client.DownloadDataCompleted += 
            new DownloadDataCompletedEventHandler((o, e) => 
            {
                    downloadCount--;
            });
        foreach (string s in files)
        {
            /*
             * This gets the file name
             */
            string fileName = Path.GetFileName(s);
            /*
             * This gets the folder and subfolders after the main directory
             */
            string filePath = s.Substring(s.IndexOf("App_Data"));
            downloadFile(client, "user:<a href="https://stackoverflow.com/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="7101100202311c08021805145f051a" rel="noreferrer noopener nofollow">[email protected]</a>/updates/App_Data/" + fileName,
            startupPath + "\\" + filePath);
            downloadCount++;
        }
        while (downloadCount > 0) { }
    }
}

private void downloadFile (WebClient client, string urlAddress, string location)
{
    System.Uri URL = new System.Uri("ftp://" + urlAddress);
    client.DownloadFileAsync(URL, location);
}

关于c# - WebClient 下载文件到 0KB?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16201318/

相关文章:

c# - 如何计算立方体中的点是否在WPF 3D中可见

c# - 如何在单独的线程中正确关闭异步套接字监听器?

C# WebClient - 下载文件后 LOH 大幅增加

c# - 从 app.config 文件的 web.config 部分获取值?

c# - 在浏览器关闭中杀死 session 值

powershell - 有没有办法在powershell中使用WebClient对象来监控下载进度?

java - 基于网络的点对点客户端使用什么技术

c# - 如何在 WebClient.DownloadStringAsync URL 中使用字符串

c# - 通过 C# 登录网站

c# - 为什么 DNX 运行代码两次