Workaround for path problems when connecting to FTP in C #

Source: Internet
Author: User
Tags ftp login
Recently encountered a demand in the work, need to use C # connection FTP, in the connection process encountered a problem, so the following article is mainly about the C # connection to the FTP path problem of the solution, the need for friends can reference, the following to see together.

Objective

This article mainly introduces to you about C # connection FTP path problem of related content, share out for everyone reference study, words not to say, come together to see the detailed introduction:

Today, when you are developing your project, you need to connect to FTP to get files, one of the key steps is to determine if you can connect to FTP and FTP files exist

The code to judge is as follows:


<summary>///test whether FTP can be successfully connected and determine if the file exists///</summary>//<param name= "Ftpserverfilepath" >ftp on the file address </param>//<param name= "Ftpuserid" >ftp login username </param>//<param name= "ftppwd" >ftp login password </ param>//<param name= "errormsg" > Return error message </param>//<returns></returns> private bool IsCan   Connectftp (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;//time-out is set to 2 seconds.    Ftprequest.credentials = new NetworkCredential (Ftpuserid, ftppwd);   Ftpresponse = (ftpwebresponse) ftprequest.getresponse (); } catch (WebException exception) {ftpresponse = (ftpwebresponse) exception. ResponsE      Switch (ftpresponse.statuscode) {Case FTPSTATUSCODE.ACTIONNOTTAKENFILEUNAVAILABLE:ERRORMSG = "The downloaded file does not exist";     Break      Case FTPSTATUSCODE.ACTIONNOTTAKENFILEUNAVAILABLEORBUSY:ERRORMSG = "The downloaded file is in use, please try again later";     Break      Default:errormsg = "An unknown error occurred";    Break   } flag = false;    The catch {errormsg = "network connection error occurred, please try again later";   Flag = true;    } finally {if (ftpresponse! = null) {ftpresponse.close ();  }} return flag; }

When the path of the Ftpserverfilepath is "127.0.0.1\1.doc", it throws an exception when the parameter is passed, and the exception is an invalid URI, as

Workaround

This is because a FtpWebRequest.Create file path identifier such as ' \ ' is not recognized when connecting, so the above exception is thrown, so the passed-in parameter should be "127.0.0.1/1.doc". or replace it in a method. The code looks like this:


Ftprequest = (ftpwebrequest) ftpwebrequest.create (New Uri ("ftp://" + ftpserverfilepath.replace ("\ \", "/")));

This will not run abnormally, as to whether the connection or the existence of the file, please check the connection

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

or Google Ftpstatuscode by yourself.

Then the modified code is: (about the C # connection complete FTP can be carefully Google query, many on the internet, so do not be tired of the statement)


 <summary>///test whether FTP can be successfully connected and determine if the file exists///</summary>//<param name= "Ftpserverfilepath" >ftp on the file address </param>//<param name= "Ftpuserid" >ftp login username </param>//<param name= "ftppwd" >ftp login password </ param>//<param name= "errormsg" > Return error message </param>//<returns></returns> private bool IsCan   Connectftp (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 (The New Uri ("ftp://" + ftpserverfilepath.replace ("\ \", "/")));    Ftprequest.method = WebRequestMethods.Ftp.ListDirectory;    Ftprequest.timeout = 2 * 1000;//time-out is set to 2 seconds.    Ftprequest.credentials = new NetworkCredential (Ftpuserid, ftppwd);   Ftpresponse = (ftpwebresponse) ftprequest.getresponse (); } catch (WebException exception) {Ftpresponse = (ftpwebresponse) exception.    Response;      Switch (ftpresponse.statuscode) {Case FTPSTATUSCODE.ACTIONNOTTAKENFILEUNAVAILABLE:ERRORMSG = "The downloaded file does not exist";     Break      Case FTPSTATUSCODE.ACTIONNOTTAKENFILEUNAVAILABLEORBUSY:ERRORMSG = "The downloaded file is in use, please try again later";     Break      Default:errormsg = "An unknown error occurred";    Break   } flag = false;    The catch {errormsg = "network connection error occurred, please try again later";   Flag = true;    } finally {if (ftpresponse! = null) {ftpresponse.close ();  }} return flag; }

summary

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.