c#判断ftp文件夹是否存在,不存在则创建文件夹

private void createdir()
        {
            string uri = "ftp://127.0.0.1/test/;

            if (!Directory.Exists(uri))
            {
                FtpWebRequest frequest = (FtpWebRequest)FtpWebRequest.Create(new Uri(uri));
                frequest.Credentials = new NetworkCredential(user, pwd);
                frequest.Method = WebRequestMethods.Ftp.MakeDirectory;
                try
                {
                    FtpWebResponse response = frequest.GetResponse() as FtpWebResponse;
                }
                catch { }
            }

        }

以下是使用C#创建FTP文件夹的示例代码: ```csharp using System; using System.Net; class Program { static void Main(string[] args) { string ftpFolderName = "newFolder"; // 要创建文件夹名称 string ftpServerIP = "ftp://ftp.example.com"; // FTP服务器地址 string ftpUserID = "username"; // FTP登录用户名 string ftpPassword = "password"; // FTP登录密码 try { // 创建FTP请求对象 FtpWebRequest request = (FtpWebRequest)WebRequest.Create(ftpServerIP + "/" + ftpFolderName); request.Method = WebRequestMethods.Ftp.MakeDirectory; request.Credentials = new NetworkCredential(ftpUserID, ftpPassword); // 发送FTP请求 FtpWebResponse response = (FtpWebResponse)request.GetResponse(); Console.WriteLine("FTP文件夹创建成功!"); response.Close(); } catch (WebException ex) { FtpWebResponse response = (FtpWebResponse)ex.Response; if (response.StatusCode == FtpStatusCode.ActionNotTakenFileUnavailable) { Console.WriteLine("FTP文件夹已存在!"); } else { Console.WriteLine("FTP文件夹创建失败:" + ex.Message); } } } } ``` 该示例代码使用`FtpWebRequest`类创建FTP请求对象,并设置请求方法为`WebRequestMethods.Ftp.MakeDirectory`,表示创建文件。然后设置FTP登录用户名和密码,并发送FTP请求。如果文件夹已存在,则会捕获`WebException`异常,并判断异常状态码是否为`FtpStatusCode.ActionNotTakenFileUnavailable`,如果是,则表示文件夹已存在,否则表示创建文件失败。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值