最新国产好看的视频,伊人天堂AV在线,国产Aaaaaa视频,蜜臀视频在线观看一区,人妻av色图,密臀久久久精品影片,青青视频免费观看毛片,久草在线观看视,国产三级精品色情在线

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

 更新時間:2017年08月01日 12:03:10   作者:菜鳥葫蘆娃  
最近在工作中遇到一個需求,需要利用C#連接FTP,在連接過程中遇到一個問題,所以下面這篇文章主要給大家介紹了關(guān)于C#連接FTP時路徑問題的解決方法,需要的朋友可以參考借鑒,下面來一起看看吧。

前言

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

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

判斷的代碼如下:

/// <summary>
  /// 測試是否可以成功連接FTP和判斷文件是否存在
  /// </summary>
  /// <param name="ftpServerFilePath">FTP上文件地址</param>
  /// <param name="ftpUserId">FTP登陸用戶名</param>
  /// <param name="ftpPwd">FTP登陸密碼</param>
  /// <param name="errorMsg">返回錯誤消息</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è)置為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 = "下載的文件正在使用,請稍后再試";
      break;
     default:
      errorMsg = "發(fā)生未知錯誤";
      break;
    }
    flag = false;
   }
   catch
   {
    errorMsg = "網(wǎng)絡(luò)連接發(fā)生錯誤,請稍后再試";
    flag = true;
   }
   finally
   {
    if (ftpResponse != null)
    {
     ftpResponse.Close();
    }
   }
   return flag;
  }

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

解決方法

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

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

這樣就不會跑異常,至于能否連接或者文件是否存在,請自行查看連接

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>
  /// 測試是否可以成功連接FTP和判斷文件是否存在
  /// </summary>
  /// <param name="ftpServerFilePath">FTP上文件地址</param>
  /// <param name="ftpUserId">FTP登陸用戶名</param>
  /// <param name="ftpPwd">FTP登陸密碼</param>
  /// <param name="errorMsg">返回錯誤消息</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è)置為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 = "下載的文件正在使用,請稍后再試";
      break;
     default:
      errorMsg = "發(fā)生未知錯誤";
      break;
    }
    flag = false;
   }
   catch
   {
    errorMsg = "網(wǎng)絡(luò)連接發(fā)生錯誤,請稍后再試";
    flag = true;
   }
   finally
   {
    if (ftpResponse != null)
    {
     ftpResponse.Close();
    }
   }
   return flag;
  }

總結(jié)

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

相關(guān)文章

最新評論

秦安县| 宝丰县| 陇西县| 金川县| 响水县| 麟游县| 航空| 兴化市| 安福县| 荣成市| 禹城市| 德化县| 红安县| 郧西县| 金寨县| 巴林左旗| 进贤县| 登封市| 海安县| 探索| 乌兰县| 锦屏县| 龙南县| 柯坪县| 天全县| 天台县| 通海县| 泰兴市| 南川市| 庆元县| 铜川市| 大冶市| 昭苏县| 防城港市| 吴江市| 越西县| 明星| 淄博市| 湄潭县| 平罗县| 左权县|