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

C#如何解決連接FTP時的路徑問題-創(chuàng)新互聯(lián)

這篇文章主要為大家展示了“C#如何解決連接FTP時的路徑問題”,內容簡而易懂,條理清晰,希望能夠幫助大家解決疑惑,下面讓小編帶領大家一起研究并學習一下“C#如何解決連接FTP時的路徑問題”這篇文章吧。

10年積累的成都網(wǎng)站設計、成都做網(wǎng)站經(jīng)驗,可以快速應對客戶對網(wǎng)站的新想法和需求。提供各種問題對應的解決方案。讓選擇我們的客戶得到更好、更有力的網(wǎng)絡服務。我雖然不認識你,你也不認識我。但先網(wǎng)站制作后付款的網(wǎng)站建設流程,更有三門免費網(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));
    ftpRequest.Method = WebRequestMethods.Ftp.ListDirectory;
    ftpRequest.Timeout = 2 * 1000;//超時時間設置為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)絡連接發(fā)生錯誤,請稍后再試";
    flag = true;
   }
   finally
   {
    if (ftpResponse != null)
    {
     ftpResponse.Close();
    }
   }
   return flag;
  }

當 ftpServerFilePath 的路徑為 “127.0.0.1\1.doc”, 這樣進行傳參時,就會拋異常,異常內容為無效的URi,如下圖

C#如何解決連接FTP時的路徑問題

解決方法

這是因為FtpWebRequest.Create 連接時不能識別'\' 這樣的文件路徑標識符,才會拋出上面的異常,因此傳入的參數(shù)應該為”127.0.0.1/1.doc”。或者在方法里面進行替換。代碼如下所示:

 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 即可。

那么修改后的代碼為:(關于C# 連接完整的FTP 可以仔細 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;//超時時間設置為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)絡連接發(fā)生錯誤,請稍后再試";
    flag = true;
   }
   finally
   {
    if (ftpResponse != null)
    {
     ftpResponse.Close();
    }
   }
   return flag;
  }

以上是“C#如何解決連接FTP時的路徑問題”這篇文章的所有內容,感謝各位的閱讀!相信大家都有了一定的了解,希望分享的內容對大家有所幫助,如果還想學習更多知識,歡迎關注創(chuàng)新互聯(lián)成都網(wǎng)站建設公司行業(yè)資訊頻道!

另外有需要云服務器可以了解下創(chuàng)新互聯(lián)scvps.cn,海內外云服務器15元起步,三天無理由+7*72小時售后在線,公司持有idc許可證,提供“云服務器、裸金屬服務器、高防服務器、香港服務器、美國服務器、虛擬主機、免備案服務器”等云主機租用服務以及企業(yè)上云的綜合解決方案,具有“安全穩(wěn)定、簡單易用、服務可用性高、性價比高”等特點與優(yōu)勢,專為企業(yè)上云打造定制,能夠滿足用戶豐富、多元化的應用場景需求。

文章標題:C#如何解決連接FTP時的路徑問題-創(chuàng)新互聯(lián)
網(wǎng)頁網(wǎng)址:http://chinadenli.net/article20/eccjo.html

成都網(wǎng)站建設公司_創(chuàng)新互聯(lián),為您提供手機網(wǎng)站建設網(wǎng)站制作網(wǎng)站策劃微信小程序網(wǎng)站排名網(wǎng)站維護

廣告

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

小程序開發(fā)