C# 检测ftp服务器是否存在目录,不存在就创建

.Net技术 码拜 6年前 (2015-12-02) 3195次浏览 0个评论

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

using System.Net;
using System.Net.Sockets;

/// <summary>
/// 检测目录是否存在
/// </summary>
/// <param name=”ServerIP”></param>
/// <param name=”pFtpUserID”></param>
/// <param name=”pFtpPW”></param>
/// <param name=”FileSource”></param>
/// <param name=”FileCategory”></param>
/// <returns></returns>
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;
}
}
}
/// <summary>
/// ftp创建目录(创建文件夹)
/// </summary>
/// <param name=”IP”>IP服务地址</param>
/// <param name=”UserName”>登陆账号</param>
/// <param name=”UserPass”>密码</param>
/// <param name=”FileSource”></param>
/// <param name=”FileCategory”></param>
/// <returns></returns>
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;
}
}
/// <summary>
/// 检测目录是否存在
/// </summary>
/// <param name=”pFtpServerIP”></param>
/// <param name=”pFtpUserID”></param>
/// <param name=”pFtpPW”></param>
/// <returns>false不存在,true存在</returns>
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;
}
}

检测并创建目录,可以避免上传时错误“远程服务器返回错误: (553) 不允许此文件名


CodeBye 版权所有丨如未注明 , 均为原创丨本网站采用BY-NC-SA协议进行授权 , 转载请注明C# 检测ftp服务器是否存在目录,不存在就创建
喜欢 (0)
[1034331897@qq.com]
分享 (0)

文章评论已关闭!