欧美一区二区三区老妇人-欧美做爰猛烈大尺度电-99久久夜色精品国产亚洲a-亚洲福利视频一区二区

C#連接FTP時(shí)路徑出現(xiàn)問題怎么辦-創(chuàng)新互聯(lián)

小編給大家分享一下C#連接FTP時(shí)路徑出現(xiàn)問題怎么辦,希望大家閱讀完這篇文章后大所收獲,下面讓我們一起去探討吧!

創(chuàng)新互聯(lián)是一家以網(wǎng)絡(luò)技術(shù)公司,為中小企業(yè)提供網(wǎng)站維護(hù)、成都網(wǎng)站建設(shè)、網(wǎng)站建設(shè)、網(wǎng)站備案、服務(wù)器租用、域名與空間、軟件開發(fā)、重慶小程序開發(fā)公司等企業(yè)互聯(lián)網(wǎng)相關(guān)業(yè)務(wù),是一家有著豐富的互聯(lián)網(wǎng)運(yùn)營(yíng)推廣經(jīng)驗(yàn)的科技公司,有著多年的網(wǎng)站建站經(jīng)驗(yàn),致力于幫助中小企業(yè)在互聯(lián)網(wǎng)讓打出自已的品牌和口碑,讓企業(yè)在互聯(lián)網(wǎng)上打開一個(gè)面向全國(guó)乃至全球的業(yè)務(wù)窗口:建站電話聯(lián)系:18980820575

最近在工作中遇到一個(gè)需求,需要利用C#連接FTP,在連接過程中遇到一個(gè)問題,所以下面這篇文章主要給大家介紹了關(guān)于C#連接FTP時(shí)路徑問題的解決方法,需要的朋友可以參考借鑒,下面來一起看看吧。

今天在開發(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)容為無效的URi,如下圖

C#連接FTP時(shí)路徑出現(xiàn)問題怎么辦

解決方法

這是因?yàn)?code>FtpWebRequest.Create 連接時(shí)不能識(shí)別'\' 這樣的文件路徑標(biāo)識(shí)符,才會(huì)拋出上面的異常,因此傳入的參數(shù)應(yīng)該為”127.0.0.1/1.doc”。或者在方法里面進(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;
  }

看完了這篇文章,相信你對(duì)C#連接FTP時(shí)路徑出現(xiàn)問題怎么辦有了一定的了解,想了解更多相關(guān)知識(shí),歡迎關(guān)注創(chuàng)新互聯(lián)行業(yè)資訊頻道,感謝各位的閱讀!

名稱欄目:C#連接FTP時(shí)路徑出現(xiàn)問題怎么辦-創(chuàng)新互聯(lián)
本文鏈接:http://chinadenli.net/article20/dghjco.html

成都網(wǎng)站建設(shè)公司_創(chuàng)新互聯(lián),為您提供品牌網(wǎng)站建設(shè)外貿(mào)建站ChatGPTApp開發(fā)標(biāo)簽優(yōu)化動(dòng)態(tài)網(wǎng)站

廣告

聲明:本網(wǎng)站發(fā)布的內(nèi)容(圖片、視頻和文字)以用戶投稿、用戶轉(zhuǎn)載內(nèi)容為主,如果涉及侵權(quán)請(qǐng)盡快告知,我們將會(huì)在第一時(shí)間刪除。文章觀點(diǎn)不代表本網(wǎng)站立場(chǎng),如需處理請(qǐng)聯(lián)系客服。電話:028-86922220;郵箱:631063699@qq.com。內(nèi)容未經(jīng)允許不得轉(zhuǎn)載,或轉(zhuǎn)載時(shí)需注明來源: 創(chuàng)新互聯(lián)

小程序開發(fā)