用ftpsupport建立所有ftp服务器上不存在的目录
最近项目中用ftpsupport.dll进行ftp上传,当用户上传文件时如果目录不存在则建立该目录,但是
现在的问题是用户传来的目录有几层都是不存在的,这样就需要循环建立所有不存在的目录,
比如需要上传文件到目录/vagerent/temp/a1/a2,但是/temp/a1/a2三个目录都没有建立。
方法如下:
/// <summary>
/// 检测ftp上是否有该目录,如果没有则建立
/// </summary>
/// <param name="ip"></param>
/// <param name="name"></param>
/// <param name="psw"></param>
/// <param name="dr">形如/ftproot/dir2/dir2_2/dirlast</param>
/// <returns></returns>
private void FtpMakeAllDir(string ip,string name,string psw,string dr)
{
FtpConnection ftp = new FtpConnection();
ftp.Connect(ip,name,psw);
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
string [] dir = dr.Split('/');
string curDir = "/";
for (int i = 0;i<dir.Length;i++)
{
curDir += "/"+dir[i];
if (!ftp.DirectoryExist(curDir))
{
try
{
ftp.CreateDirectory(curDir);
}
catch{}
}
}
ftp.Close();
}
关于ftpsupport.dll下载和使用的问题请看另一篇blog:
用ftpsupport进行ftp上传
现在的问题是用户传来的目录有几层都是不存在的,这样就需要循环建立所有不存在的目录,
比如需要上传文件到目录/vagerent/temp/a1/a2,但是/temp/a1/a2三个目录都没有建立。
方法如下:
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedBlockStart.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedBlockEnd.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedBlockStart.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockEnd.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockEnd.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockEnd.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedBlockEnd.gif)
关于ftpsupport.dll下载和使用的问题请看另一篇blog:
用ftpsupport进行ftp上传