C#使用FTP上传整个目录 [英] C# Upload whole directory using FTP

查看:128
本文介绍了C#使用FTP上传整个目录的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想要做的是在C#(C Sharp)中使用FTP上传网站。所以我需要上传文件夹中的所有文件和文件夹,保持其结构。我正在使用这个FTP类: http://www.codeproject.com / tips / 443588 / Simple-Csharp-FTP-class 进行实际上传。

What I'm trying to do is to upload a website using FTP in C# (C Sharp). So I need to upload all files and folders within a folder, keeping its structure. I'm using this FTP class: http://www.codeproject.com/Tips/443588/Simple-Csharp-FTP-Class for the actual uploading.

我得出结论,我需要写一个递归方法通过主目录的每个子目录,并上传其中的所有文件和文件夹。这应该使我的文件夹的精确副本到FTP。问题是...我不知道如何编写一个这样的方法。我以前写过递归方法,但我是FTP部分的新手。

I have come to the conclusion that I need to write a recursive method that goes through every sub-directory of the main directory and upload all files and folders in it. This should make an exact copy of my folder to the FTP. Problem is... I have no clue how to write a method like that. I have written recursive methods before but I'm new to the FTP part.

这是我到目前为止:

private void recursiveDirectory(string directoryPath)
    {
        string[] filePaths = null;
        string[] subDirectories = null;

        filePaths = Directory.GetFiles(directoryPath, "*.*");
        subDirectories = Directory.GetDirectories(directoryPath);

        if (filePaths != null && subDirectories != null)
        {
            foreach (string directory in subDirectories)
            {
                ftpClient.createDirectory(directory);
            }
            foreach (string file in filePaths)
            {
                ftpClient.upload(Path.GetDirectoryName(directoryPath), file);
            }
        }
    }

但是远远没有完成我不知道如何继续。我确信比我更需要知道这一点!感谢提前:)

But its far from done and I don't know how to continue. I'm sure more than me needs to know this! Thanks in advance :)

哦和...如果它报告进度,这将是很好:)(我正在使用进度条)

Ohh and... It would be nice if it reported its progress too :) (I'm using a progress bar)

编辑:
可能已经不清楚...如何使用FTP上传包含所有子目录和文件的目录?

It might have been unclear... How do I upload a directory including all sub-directories and files with FTP?

推荐答案

解决问题! :)



好的,所以我设法写方法myslef。如果有人需要,可以自由复制:

Problem Solved! :)

Alright so I managed to write the method myslef. If anyone need it feel free to copy:

private void recursiveDirectory(string dirPath, string uploadPath)
    {
        string[] files = Directory.GetFiles(dirPath, "*.*");
        string[] subDirs = Directory.GetDirectories(dirPath);

        foreach (string file in files)
        {
            ftpClient.upload(uploadPath + "/" + Path.GetFileName(file), file);
        }

        foreach (string subDir in subDirs)
        {
            ftpClient.createDirectory(uploadPath + "/" + Path.GetFileName(subDir));
            recursiveDirectory(subDir, uploadPath + "/" + Path.GetFileName(subDir));
        }
    }

它工作得很好:)

这篇关于C#使用FTP上传整个目录的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆