如题!C#判断远程目录能否存在,例如判断ftp://10.64.66.223:904//Pattern/p.4这里的P.4文件夹能否存在,求高手指点,感谢!
解决方案
10
有密码有端口,用原生FTP类就可以
http://www.cnblogs.com/goody9807/archive/2010/01/29/1659205.html
http://www.cnblogs.com/goody9807/archive/2010/01/29/1659205.html
5
var request = (FtpWebRequest)WebRequest.Create ”ftp://10.64.66.223:904//Pattern/p.4"); request.Credentials = new NetworkCredential("user", "pass"); request.Method = WebRequestMethods.Ftp.GetFileSize; try { FtpWebResponse response = (FtpWebResponse)request.GetResponse(); } catch (WebException ex) { FtpWebResponse response = (FtpWebResponse)ex.Response; if (response.StatusCode == FtpStatusCode.ActionNotTakenFileUnavailable) { 不存在 } }
5
直接调用http://blog.csdn.net/chinacsharper/article/details/9501773这里面的DirectoryExist方法。
当然需要提供ftp地址、账号、密码等参数值。
当然需要提供ftp地址、账号、密码等参数值。
5
//引用一个其它人写的代码
//引用的名字空间
using System;
using System.Collections.Generic;
using System.Text;
using System.Net;
using System.Net.Sockets;
using System.IO;
/// <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;
}
}
//引用的名字空间
using System;
using System.Collections.Generic;
using System.Text;
using System.Net;
using System.Net.Sockets;
using System.IO;
/// <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;
}
}
5
FTP的用FTP相关类判断就行
假如是文件系统路径就用netshare
假如是文件系统路径就用netshare
5
FTP相关类
5
都可以实现!
5
这个可以。
5
ftp 存在对应的方法
5
FTP相关类
5