Ftp.MakeDirectory嵌套结构 [英] Ftp.MakeDirectory nested structure
问题描述
如果你缺少在ftp上创建子目录的权限,你们会怎么做?
显然你不能在一个命令中创建一个目录
所以这个:
pre > path =ftp://someftp.com/files/newFolder/innerFolder;
var request =(FtpWebRequest)WebRequest.Create(path);
request.Method = WebRequestMethods.Ftp.MakeDirectory;
var response =(FtpWebResponse)request.GetResponse());如果'files'或'newFolder'已经存在,
会抛出一个错误代码为550的异常。 p>
-
如何检查我是否有权创建子目录?
-
如果我没有这些权利,我该怎么办?
好吧:因为 FtpWebRequest
是无状态的 - 无法在FTP上更改当前目录。幸运的是,我们可以使用其中一个开源FTP库。这里有一个AlexPilotti的FTPS库的例子,它可以通过NuGet使用(var client = new FTPSClient())$ b $来获得NuGet
b {
var address = Regex.Match(path,@^(ftp://)?(\ w * |。?)* /).Value.Replace(ftp://, ).Replace(/,);
var dirs = Regex.Split(path.Replace(address,).Replace(ftp://,),/\").Where(x => x.Length> 0);
client.Connect(address,credential,ESSLSupportMode.ClearText);
foreach(var dir in dirs)
{
try
{
client.MakeDir(dir);
}
catch(FTPException)
{
}
client.SetCurrentDirectory(dir);
}
}
}
What do you guys do if you lack of permissions to create subdirs on a ftp? Apparently you can't create a directory in one command
So this:
path = "ftp://someftp.com/files/newFolder/innerFolder";
var request = (FtpWebRequest)WebRequest.Create(path);
request.Method = WebRequestMethods.Ftp.MakeDirectory;
var response = (FtpWebResponse)request.GetResponse());
will throw an exception with error code 550 if 'files' or 'newFolder' is already exists
How can I check if I have the rights to create subdirs?
What can I do if I don't have those rights? Show me please code that lets you create a folder structure in more than one command.
Alright: because FtpWebRequest
is stateless - there is no way to change current directory on FTP. Fortunately we can use one of the open-source FTP libraries. Here's an example with AlexPilotti's FTPS library, which is availible through NuGet
using (var client = new FTPSClient())
{
var address = Regex.Match(path, @"^(ftp://)?(\w*|.?)*/").Value.Replace("ftp://", "").Replace("/", "");
var dirs = Regex.Split(path.Replace(address, "").Replace("ftp://", ""), "/").Where(x => x.Length > 0);
client.Connect(address, credential, ESSLSupportMode.ClearText);
foreach (var dir in dirs)
{
try
{
client.MakeDir(dir);
}
catch (FTPException)
{
}
client.SetCurrentDirectory(dir);
}
}
}
这篇关于Ftp.MakeDirectory嵌套结构的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!