欧美大屁股bbbbxxxx,狼人大香伊蕉国产www亚洲,男ji大巴进入女人的视频小说,男人把ji大巴放进女人免费视频,免费情侣作爱视频

歡迎來(lái)到入門教程網(wǎng)!

C#教程

當(dāng)前位置:主頁(yè) > 軟件編程 > C#教程 >

關(guān)于C#連接FTP時(shí)路徑問題的解決方法

來(lái)源:本站原創(chuàng)|時(shí)間:2020-01-10|欄目:C#教程|點(diǎn)擊: 次

前言

本文主要給大家介紹了關(guān)于C#連接FTP時(shí)路徑問題的相關(guān)內(nèi)容,分享出來(lái)供大家參考學(xué)習(xí),話不多說(shuō),來(lái)一起看看詳細(xì)的介紹:

今天在開發(fā)項(xiàng)目時(shí),需要連接FTP獲取文件,其中關(guān)鍵的一步就是判斷能否連接FTP以及FTP上的文件是否存在

判斷的代碼如下:

/// <summary>
  /// 測(cè)試是否可以成功連接FTP和判斷文件是否存在
  /// </summary>
  /// <param name="ftpServerFilePath">FTP上文件地址</param>
  /// <param name="ftpUserId">FTP登陸用戶名</param>
  /// <param name="ftpPwd">FTP登陸密碼</param>
  /// <param name="errorMsg">返回錯(cuò)誤消息</param>
  /// <returns></returns>
  private bool IsCanConnectFtp(string ftpServerFilePath, string ftpUserId, string ftpPwd, out string errorMsg)
  {
   bool flag = true;
   FtpWebResponse ftpResponse = null;
   FtpWebRequest ftpRequest = null;
   errorMsg = string.Empty;
   try
   {
    ftpRequest = (FtpWebRequest)FtpWebRequest.Create(new Uri("ftp://" + ftpServerFilePath));
    ftpRequest.Method = WebRequestMethods.Ftp.ListDirectory;
    ftpRequest.Timeout = 2 * 1000;//超時(shí)時(shí)間設(shè)置為2秒。
    ftpRequest.Credentials = new NetworkCredential(ftpUserId, ftpPwd);
    ftpResponse = (FtpWebResponse)ftpRequest.GetResponse();
   }
   catch (WebException exception)
   {
    ftpResponse = (FtpWebResponse)exception.Response;
    switch (ftpResponse.StatusCode)
    {
     case FtpStatusCode.ActionNotTakenFileUnavailable:
      errorMsg = "下載的文件不存在";
      break;
     case FtpStatusCode.ActionNotTakenFileUnavailableOrBusy:
      errorMsg = "下載的文件正在使用,請(qǐng)稍后再試";
      break;
     default:
      errorMsg = "發(fā)生未知錯(cuò)誤";
      break;
    }
    flag = false;
   }
   catch
   {
    errorMsg = "網(wǎng)絡(luò)連接發(fā)生錯(cuò)誤,請(qǐng)稍后再試";
    flag = true;
   }
   finally
   {
    if (ftpResponse != null)
    {
     ftpResponse.Close();
    }
   }
   return flag;
  }

當(dāng) ftpServerFilePath 的路徑為 “127.0.0.1\1.doc”, 這樣進(jìn)行傳參時(shí),就會(huì)拋異常,異常內(nèi)容為無(wú)效的URi,如下圖

解決方法

這是因?yàn)?code>FtpWebRequest.Create 連接時(shí)不能識(shí)別'\' 這樣的文件路徑標(biāo)識(shí)符,才會(huì)拋出上面的異常,因此傳入的參數(shù)應(yīng)該為”127.0.0.1/1.doc”?;蛘咴诜椒ɡ锩孢M(jìn)行替換。代碼如下所示:

 ftpRequest = (FtpWebRequest)FtpWebRequest.Create(new Uri("ftp://" + ftpServerFilePath.Replace("\\","/")));

這樣就不會(huì)跑異常,至于能否連接或者文件是否存在,請(qǐng)自行查看連接

https://msdn.microsoft.com/zh-cn/library/system.net.ftpstatuscode(v=vs.110).aspx

或者自行 google FtpStatusCode 即可。

那么修改后的代碼為:(關(guān)于C# 連接完整的FTP 可以仔細(xì) google 查詢,網(wǎng)上多的是,這樣就不累述了)

 /// <summary>
  /// 測(cè)試是否可以成功連接FTP和判斷文件是否存在
  /// </summary>
  /// <param name="ftpServerFilePath">FTP上文件地址</param>
  /// <param name="ftpUserId">FTP登陸用戶名</param>
  /// <param name="ftpPwd">FTP登陸密碼</param>
  /// <param name="errorMsg">返回錯(cuò)誤消息</param>
  /// <returns></returns>
  private bool IsCanConnectFtp(string ftpServerFilePath, string ftpUserId, string ftpPwd, out string errorMsg)
  {
   bool flag = true;
   FtpWebResponse ftpResponse = null;
   FtpWebRequest ftpRequest = null;
   errorMsg = string.Empty;
   try
   {
    ftpRequest = (FtpWebRequest)FtpWebRequest.Create(new Uri("ftp://" + ftpServerFilePath.Replace("\\","/")));
    ftpRequest.Method = WebRequestMethods.Ftp.ListDirectory;
    ftpRequest.Timeout = 2 * 1000;//超時(shí)時(shí)間設(shè)置為2秒。
    ftpRequest.Credentials = new NetworkCredential(ftpUserId, ftpPwd);
    ftpResponse = (FtpWebResponse)ftpRequest.GetResponse();
   }
   catch (WebException exception)
   {
    ftpResponse = (FtpWebResponse)exception.Response;
    switch (ftpResponse.StatusCode)
    {
     case FtpStatusCode.ActionNotTakenFileUnavailable:
      errorMsg = "下載的文件不存在";
      break;
     case FtpStatusCode.ActionNotTakenFileUnavailableOrBusy:
      errorMsg = "下載的文件正在使用,請(qǐng)稍后再試";
      break;
     default:
      errorMsg = "發(fā)生未知錯(cuò)誤";
      break;
    }
    flag = false;
   }
   catch
   {
    errorMsg = "網(wǎng)絡(luò)連接發(fā)生錯(cuò)誤,請(qǐng)稍后再試";
    flag = true;
   }
   finally
   {
    if (ftpResponse != null)
    {
     ftpResponse.Close();
    }
   }
   return flag;
  }

總結(jié)

以上就是這篇文章的全部?jī)?nèi)容了,希望本文的內(nèi)容對(duì)大家的學(xué)習(xí)或者工作能帶來(lái)一定的幫助,如果有疑問大家可以留言交流,謝謝大家對(duì)我們的支持

上一篇:使用C#創(chuàng)建Windows服務(wù)的實(shí)例代碼

欄    目:C#教程

下一篇:C#設(shè)計(jì)模式之行為型模式詳解

本文標(biāo)題:關(guān)于C#連接FTP時(shí)路徑問題的解決方法

本文地址:http://mengdiqiu.com.cn/a1/C_jiaocheng/5554.html

網(wǎng)頁(yè)制作CMS教程網(wǎng)絡(luò)編程軟件編程腳本語(yǔ)言數(shù)據(jù)庫(kù)服務(wù)器

如果侵犯了您的權(quán)利,請(qǐng)與我們聯(lián)系,我們將在24小時(shí)內(nèi)進(jìn)行處理、任何非本站因素導(dǎo)致的法律后果,本站均不負(fù)任何責(zé)任。

聯(lián)系QQ:835971066 | 郵箱:835971066#qq.com(#換成@)

Copyright © 2002-2020 腳本教程網(wǎng) 版權(quán)所有