問題描述
我要做的是在 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 上傳整個目錄的文章就介紹到這了,希望我們推薦的答案對大家有所幫助,也希望大家多多支持html5模板網!