php ftp文件夹不存在时自动创建,C# 检测ftp服务器是否存在目录,不存在就创建...

C# 检测ftp服务器是否存在目录文件夹,不存在时就创建一个,代码如下:

using System.Net;

using System.Net.Sockets;

///

/// 检测目录是否存在

///

///

///

///

///

///

///

public static bool checkDirectory(string ServerIP, string pFtpUserID, string pFtpPW, string FileSource, string FileCategory)

{

//检测目录是否存在

Uri uri = new Uri(“ftp://” + ServerIP + “/” + FileSource + “/”);

if (!DirectoryIsExist(uri, pFtpUserID, pFtpPW))

{

//创建目录

uri = new Uri(“ftp://” + ServerIP + “/” + FileSource);

if (CreateDirectory(uri, pFtpUserID, pFtpPW))

{

//检测下一级目录是否存在

uri = new Uri(“ftp://” + ServerIP + “/” + FileSource + “/” + FileCategory + “/”);

if (!DirectoryIsExist(uri, pFtpUserID, pFtpPW))

{

//创建目录

uri = new Uri(“ftp://” + ServerIP + “/” + FileSource + “/” + FileCategory);

if (CreateDirectory(uri, pFtpUserID, pFtpPW))

{

return true;

}

else { XtraMessageBox.Show(“创建目录:{0}失败”, FileCategory); return false; }

}

else

{

return true;

}

}

else { XtraMessageBox.Show(“创建目录:{0}失败”, FileSource); return false; }

}

else

{

//检测下一级目录是否存在

uri = new Uri(“ftp://” + ServerIP + “/” + FileSource + “/” + FileCategory + “/”);

if (!DirectoryIsExist(uri, pFtpUserID, pFtpPW))

{

//创建目录

uri = new Uri(“ftp://” + ServerIP + “/” + FileSource + “/” + FileCategory);

if (CreateDirectory(uri, pFtpUserID, pFtpPW))

{

return true;

}

else { XtraMessageBox.Show(“创建目录:{0}失败”, FileCategory); return false; }

}

else

{

return true;

}

}

}

///

/// ftp创建目录(创建文件夹)

///

/// IP服务地址

/// 登陆账号

/// 密码

///

///

///

public static bool CreateDirectory(Uri IP, string UserName, string UserPass)

{

try

{

FtpWebRequest FTP = (FtpWebRequest)FtpWebRequest.Create(IP);

FTP.Credentials = new NetworkCredential(UserName, UserPass);

FTP.Proxy = null;

FTP.KeepAlive = false;

FTP.Method = WebRequestMethods.Ftp.MakeDirectory;

FTP.UseBinary = true;

FtpWebResponse response = FTP.GetResponse() as FtpWebResponse;

response.Close();

return true;

}

catch

{

return false;

}

}

///

/// 检测目录是否存在

///

///

///

///

/// false不存在,true存在

public static bool DirectoryIsExist(Uri pFtpServerIP, string pFtpUserID, string pFtpPW)

{

string[] value = GetFileList(pFtpServerIP, pFtpUserID, pFtpPW);

if (value == null)

{

return false;

}

else

{

return true;

}

}

public static string[] GetFileList(Uri pFtpServerIP, string pFtpUserID, string pFtpPW)

{

StringBuilder result = new StringBuilder();

try

{

FtpWebRequest reqFTP = (FtpWebRequest)FtpWebRequest.Create(pFtpServerIP);

reqFTP.UseBinary = true;

reqFTP.Credentials = new NetworkCredential(pFtpUserID, pFtpPW);

reqFTP.Method = WebRequestMethods.Ftp.ListDirectoryDetails;

WebResponse response = reqFTP.GetResponse();

StreamReader reader = new StreamReader(response.GetResponseStream());

string line = reader.ReadLine();

while (line != null)

{

result.Append(line);

result.Append(“\n”);

line = reader.ReadLine();

}

reader.Close();

response.Close();

return result.ToString().Split(‘\n’);

}

catch

{

return null;

}

}

CodeBye 版权所有丨如未注明 , 均为原创丨本网站采用BY-NC-SA协议进行授权 , 转载请注明C# 检测ftp服务器是否存在目录,不存在就创建!

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值