C#如何判断FTP服务器上的文件夹是否存在 使用 reqFTP.Method = WebRequestMethods.Ftp.MakeDirectory; 如果存在相同文件夹会报错是吗?怎样判断是否存在同名文件夹呢? 解决方案 » 免费领取超大流量手机卡,每月29元包185G流量+100分钟通话, 中国电信官方发货 还有 如果是 /a/b/c 这样的路径,会报错,怎么解决呢? C#操作FTP的相关代码,搜一下,不是一大堆吗?找不到相应的解决办法? 先判断文件夹是否存在Directory.Exists("///") 不知道楼主是要实现什么功能 如果最终目的是为了创建目录的话 (我的需求)我的做法是,不管目录文件夹是否存在 ,循环创建, 如果存在就会出异常,捕获,但不抛, 不存在的话就会创建.看看,有更好的方法发给我看看代码: ...... { ...... string serverPathURL1 = "ftp://" + fileStoreServerIP + ":" + fileStoreServerPort + "/" + this.UpSectionCB.Text; string serverPathURL2 = serverPathURL1 + "/" + this.UpFileGroupTB.Text.Trim(); string serverPathURL3 = serverPathURL2 + "/" + this.UpFileTypeCB.Text; // 此例子中 this.UpSectionCB.Text, this.UpFileGroupTB.Text.Trim() , // this.UpFileTypeCB.Text 都表示 文件夹 名称 // fileStoreServerIP ,fileStoreServerPort 表示ftp服务器IP , 端口 UpMakeDirectory(serverPathURL1); UpMakeDirectory(serverPathURL2); UpMakeDirectory(serverPathURL3); ...... } /// <summary> /// 创建文件路径 /// </summary> /// <param name="uploadUrl">创建的路径</param> public void UpMakeDirectory(string uploadUrl) { FtpWebResponse uploadResponse = null; try { FtpWebRequest uploadRequest = (FtpWebRequest)WebRequest.Create(new Uri(uploadUrl)); uploadRequest.Method = WebRequestMethods.Ftp.MakeDirectory; uploadRequest.Proxy = null; uploadResponse = (FtpWebResponse)uploadRequest.GetResponse(); } catch { if (uploadResponse != null) { uploadResponse.Close(); } } if (uploadResponse != null) { uploadResponse.Close(); } } ftp好像没有现成的判断文件夹是否存在的命令我是用获取文件列表,然后遍历是否相同来实现的 哎,我用的try catch捕捉错误,出错了继续循环创建,就是感觉有点小慢,没办法了,笨方法,呵呵。 [高分提问] C# 写的应用程序 如何和 C# 写的asp.net 网站通信? 注册windows事件不响应问题 【求指点】VS2008 制作新版本安装包 问题 怎么用正则表达式提取字符串中的汉字? 多线程的时候CPU吃紧 怎样用sql 语句对xml进行操作 怎么读取USB口数据 遍历文件 ,并在treeview中显示 请教大家一个关于打印的问题 (求助)在C#中,使用OracleCliet执行存储过程的代码到底应该怎么写? 验证控件不能验证 当csdn排名升至2万却遇见难题:datalist指定行的超级链接
找不到相应的解决办法?
Directory.Exists("///")
如果最终目的是为了创建目录的话 (我的需求)我的做法是,不管目录文件夹是否存在 ,循环创建,
如果存在就会出异常,捕获,但不抛, 不存在的话就会创建.看看,有更好的方法发给我看看代码: ......
{
...... string serverPathURL1 = "ftp://" + fileStoreServerIP + ":" + fileStoreServerPort + "/" + this.UpSectionCB.Text;
string serverPathURL2 = serverPathURL1 + "/" + this.UpFileGroupTB.Text.Trim();
string serverPathURL3 = serverPathURL2 + "/" + this.UpFileTypeCB.Text;
// 此例子中 this.UpSectionCB.Text, this.UpFileGroupTB.Text.Trim() ,
// this.UpFileTypeCB.Text 都表示 文件夹 名称
// fileStoreServerIP ,fileStoreServerPort 表示ftp服务器IP , 端口 UpMakeDirectory(serverPathURL1);
UpMakeDirectory(serverPathURL2);
UpMakeDirectory(serverPathURL3); ......
} /// <summary>
/// 创建文件路径
/// </summary>
/// <param name="uploadUrl">创建的路径</param>
public void UpMakeDirectory(string uploadUrl)
{
FtpWebResponse uploadResponse = null;
try
{
FtpWebRequest uploadRequest =
(FtpWebRequest)WebRequest.Create(new Uri(uploadUrl));
uploadRequest.Method = WebRequestMethods.Ftp.MakeDirectory;
uploadRequest.Proxy = null;
uploadResponse =
(FtpWebResponse)uploadRequest.GetResponse();
}
catch
{
if (uploadResponse != null)
{
uploadResponse.Close();
}
}
if (uploadResponse != null)
{
uploadResponse.Close();
}
}
我是用获取文件列表,然后遍历是否相同来实现的