關(guān)于C#連接FTP時(shí)路徑問題的解決方法
前言
本文主要給大家介紹了關(guān)于C#連接FTP時(shí)路徑問題的相關(guān)內(nèi)容,分享出來供大家參考學(xué)習(xí),話不多說,來一起看看詳細(xì)的介紹:
今天在開發(fā)項(xiàng)目時(shí),需要連接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">返回錯(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 = "下載的文件正在使用,請稍后再試";
break;
default:
errorMsg = "發(fā)生未知錯(cuò)誤";
break;
}
flag = false;
}
catch
{
errorMsg = "網(wǎng)絡(luò)連接發(fā)生錯(cuò)誤,請稍后再試";
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,如下圖
解決方法
這是因?yàn)?code>FtpWebRequest.Create 連接時(shí)不能識別'\' 這樣的文件路徑標(biāo)識符,才會(huì)拋出上面的異常,因此傳入的參數(shù)應(yīng)該為”127.0.0.1/1.doc”?;蛘咴诜椒ɡ锩孢M(jìn)行替換。代碼如下所示:
ftpRequest = (FtpWebRequest)FtpWebRequest.Create(new Uri("ftp://" + ftpServerFilePath.Replace("\\","/")));
這樣就不會(huì)跑異常,至于能否連接或者文件是否存在,請自行查看連接
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">返回錯(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 = "下載的文件正在使用,請稍后再試";
break;
default:
errorMsg = "發(fā)生未知錯(cuò)誤";
break;
}
flag = false;
}
catch
{
errorMsg = "網(wǎng)絡(luò)連接發(fā)生錯(cuò)誤,請稍后再試";
flag = true;
}
finally
{
if (ftpResponse != null)
{
ftpResponse.Close();
}
}
return flag;
}
總結(jié)
以上就是這篇文章的全部內(nèi)容了,希望本文的內(nèi)容對大家的學(xué)習(xí)或者工作能帶來一定的幫助,如果有疑問大家可以留言交流,謝謝大家對我們的支持
上一篇:使用C#創(chuàng)建Windows服務(wù)的實(shí)例代碼
欄 目:C#教程
本文標(biāo)題:關(guān)于C#連接FTP時(shí)路徑問題的解決方法
本文地址:http://www.jygsgssxh.com/a1/C_jiaocheng/5554.html
您可能感興趣的文章
- 01-10關(guān)于ASP網(wǎng)頁無法打開的解決方案
- 01-10C#操作ftp類完整實(shí)例
- 01-10關(guān)于nancy中的身份驗(yàn)證
- 01-10C#連接數(shù)據(jù)庫的方法
- 01-10輕松學(xué)習(xí)C#的運(yùn)算符
- 01-10C#編程實(shí)現(xiàn)連接ACCESS數(shù)據(jù)庫實(shí)例詳解
- 01-10C#編程實(shí)現(xiàn)連接SQL SERVER數(shù)據(jù)庫實(shí)例詳解
- 01-10C#中ZipHelper 壓縮和解壓幫助類
- 01-10C#程序連接數(shù)據(jù)庫及讀取數(shù)據(jù)庫中字段的簡單方法總結(jié)
- 01-10關(guān)于C#中排序函數(shù)的總結(jié)


閱讀排行
本欄相關(guān)
- 01-10C#通過反射獲取當(dāng)前工程中所有窗體并
- 01-10關(guān)于ASP網(wǎng)頁無法打開的解決方案
- 01-10WinForm限制窗體不能移到屏幕外的方法
- 01-10WinForm繪制圓角的方法
- 01-10C#實(shí)現(xiàn)txt定位指定行完整實(shí)例
- 01-10WinForm實(shí)現(xiàn)仿視頻播放器左下角滾動(dòng)新
- 01-10C#停止線程的方法
- 01-10C#實(shí)現(xiàn)清空回收站的方法
- 01-10C#通過重寫Panel改變邊框顏色與寬度的
- 01-10C#實(shí)現(xiàn)讀取注冊表監(jiān)控當(dāng)前操作系統(tǒng)已
隨機(jī)閱讀
- 01-11ajax實(shí)現(xiàn)頁面的局部加載
- 01-10SublimeText編譯C開發(fā)環(huán)境設(shè)置
- 01-10C#中split用法實(shí)例總結(jié)
- 08-05DEDE織夢data目錄下的sessions文件夾有什
- 01-10delphi制作wav文件的方法
- 01-11Mac OSX 打開原生自帶讀寫NTFS功能(圖文
- 01-10使用C語言求解撲克牌的順子及n個(gè)骰子
- 04-02jquery與jsp,用jquery
- 08-05dedecms(織夢)副欄目數(shù)量限制代碼修改
- 08-05織夢dedecms什么時(shí)候用欄目交叉功能?


