C#往ftp服务器上传文件夹以及子文件夹
staticstringdirToCreate="ftp://abc.com";staticvoidRecureDirectory(DirectoryInfodirect...
static string dirToCreate = "ftp://abc.com" ;
static void RecureDirectory(DirectoryInfo directory)
{
//CreateFolder(directory);
dirToCreate += @"/" + directory;
FtpWebRequest request = (FtpWebRequest)FtpWebRequest.Create(dirToCreate);
request.Credentials = new NetworkCredential("xxx", "yyyy");
request.UsePassive = true;
request.UseBinary = true;
request.KeepAlive = false;
request.Method = WebRequestMethods.Ftp.MakeDirectory;
using (var resp = (FtpWebResponse)request.GetResponse())
{
Console.WriteLine(resp.StatusCode);
}
FileInfo[] files = null;
DirectoryInfo[] subDirect = null;
// process all the files directly under this folder
files = directory.GetFiles("*.*");
if (files != null)
{
foreach (FileInfo file in files)
{
UploadFile();
}
//find all the subdirectories under this directory
}
subDirect = directory.GetDirectories();
if (subDirect.Length != 0)
{
foreach (DirectoryInfo dirInfo in subDirect)
{
//resurvise call for each subdirectory
RecureDirectory(dirInfo);
}
}
}
我的code创建路径的时候显然有问题。所有的子文件夹都加在一个路径上了。 但是不知道怎么递归的时候怎么解决路径的问题。。。求指教。。。
可能没表达清楚。 在ftp上递归创建文件夹以及子文件夹夹。路径总是不对。路径应该存在哪里才能正确创建所有的目录? 展开
static void RecureDirectory(DirectoryInfo directory)
{
//CreateFolder(directory);
dirToCreate += @"/" + directory;
FtpWebRequest request = (FtpWebRequest)FtpWebRequest.Create(dirToCreate);
request.Credentials = new NetworkCredential("xxx", "yyyy");
request.UsePassive = true;
request.UseBinary = true;
request.KeepAlive = false;
request.Method = WebRequestMethods.Ftp.MakeDirectory;
using (var resp = (FtpWebResponse)request.GetResponse())
{
Console.WriteLine(resp.StatusCode);
}
FileInfo[] files = null;
DirectoryInfo[] subDirect = null;
// process all the files directly under this folder
files = directory.GetFiles("*.*");
if (files != null)
{
foreach (FileInfo file in files)
{
UploadFile();
}
//find all the subdirectories under this directory
}
subDirect = directory.GetDirectories();
if (subDirect.Length != 0)
{
foreach (DirectoryInfo dirInfo in subDirect)
{
//resurvise call for each subdirectory
RecureDirectory(dirInfo);
}
}
}
我的code创建路径的时候显然有问题。所有的子文件夹都加在一个路径上了。 但是不知道怎么递归的时候怎么解决路径的问题。。。求指教。。。
可能没表达清楚。 在ftp上递归创建文件夹以及子文件夹夹。路径总是不对。路径应该存在哪里才能正确创建所有的目录? 展开
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询