⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 ftpclient.cs

📁 ftp浏览器
💻 CS
📖 第 1 页 / 共 4 页
字号:
				}
			}
			else if ( l_strCommand.Equals("MODE") ) {
				/*	200	(success) */
				/*	500,501,504,530 (syntax error) */
				/*	421	(service not available) */
				switch( l_iResponseCode ) {
					case 200 :
						l_iRetval = 1;
						break;
					case 500 : 
						m_strError = "<MODE> command. Syntax Error Command Unrecognized";
						l_iRetval = 0;
						break;
					case 501 :
						m_strError = "<MODE> command. Syntax error in parameters or arguments";
						l_iRetval = 0;
						break;
					case 504 :
						m_strError = "<MODE> command. Command not implemented";
						l_iRetval = 0;
						break;
					case 530 :
						m_strError = "<MODE> command. User not logged on";
						l_iRetval = 0;
						break;
					default:
						m_strError = "Unknown error in MODE command. Response code :" +  l_iResponseCode;
						l_iRetval = 0;
						break;
				}
			}
			else if ( l_strCommand.Equals("TYPE") ) {
				/*	200	(success) */
				/*	500,501,504,530 (syntax error) */
				/*	421	(service not available) */
				switch( l_iResponseCode ) {
					case 200 :
						l_iRetval = 1;
						break;
					case 500 : 
						m_strError = "<TYPE> command. Syntax Error Command Unrecognized";
						l_iRetval = 0;
						break;
					case 501 :
						m_strError = "<TYPE> command. Syntax error in parameters or arguments";
						l_iRetval = 0;
						break;
					case 504 :
						m_strError = "<TYPE> command. Command not implemented";
						l_iRetval = 0;
						break;
					case 530 :
						m_strError = "<TYPE> command. User not logged on";
						l_iRetval = 0;
						break;
					default:
						m_strError = "Unknown error in TYPE command. Response code :" +  l_iResponseCode;
						l_iRetval = 0;
						break;
				}
			}
			else if ( l_strCommand.Equals("PORT") ) {
				/*	200,226,150	(success) */
				/*	500,501,530 (syntax error) */
				/*	421	(service not available) */
				switch( l_iResponseCode ) {
					case 200 : case 226 : case 150 : case 250 :
						l_iRetval = 1;
						break;
					case 500 : 
						m_strError = "<PORT> command. Syntax Error Command Unrecognized";
						l_iRetval = 0;
						break;
					case 501 :
						m_strError = "<PORT> command. Syntax error in parameters or arguments";
						l_iRetval = 0;
						break;
					case 530 :
						m_strError = "<PORT> command. User not logged on";
						l_iRetval = 0;
						break;
					case 426 :
						m_strError = "<PORT> command. Connection closed transfer aborted";
						l_iRetval = 0;
						break;
					default:
						m_strError = "Unknown error in PORT command. Response code :" +  l_iResponseCode;
						l_iRetval = 0;
						break;
				}
			}
			else if ( l_strCommand.Equals("LIST") ) {
				/*	125,150,250,226 (success) */
				/*	425,426,451,450,500,501,502,530 (syntax error) */
				/*	421	(service not available) */
				switch( l_iResponseCode ) {
					case 125 : case 150 : case 250 : case 226 :
						l_iRetval = 1;
						break;
					case 425 :
						m_strError = "<LIST> command. Can't open data connection";
						l_iRetval = 0;
						break;
					case 426 :
						m_strError = "<LIST> command. Connection closed; transfer aborted";
						l_iRetval = 0;
						break;
					case 451 :
						m_strError = "<LIST> command. Requested action aborted: Local error in processing";
						l_iRetval = 0;
						break;
					case 450 :
						m_strError = "<LIST> command. Requested file action not taken.File unavailable (e.g., file busy)";
						l_iRetval = 0;
						break;
					case 500 : 
						m_strError = "<LIST> command. Syntax Error Command Unrecognized";
						l_iRetval = 0;
						break;
					case 501 :
						m_strError = "<LIST> command. Syntax error in parameters or arguments";
						l_iRetval = 0;
						break;
					case 502 :
						m_strError = "<LIST> command. Command not implemented";
						l_iRetval = 0;
						break;
					case 530 :
						m_strError = "<LIST> command. User not logged on";
						l_iRetval = 0;
						break;
					default:
						m_strError = "Unknown error in LIST command. Response code :" +  l_iResponseCode;
						l_iRetval = 0;
						break;
				}
			}
			else if ( l_strCommand.StartsWith("CWD") ) {
				/*	250,150 (success) */
				/*	500,501,502,530,550 (syntax error) */
				/*	421	(service not available) */
				switch( l_iResponseCode ) {
					case 250 : case 150 : case 226: case 200 : case 257 :
						l_iRetval = 1;
						break;
					case 500 : 
						m_strError = "<CWD> command. Syntax Error Command Unrecognized";
						l_iRetval = 0;
						break;
					case 501 :
						m_strError = "<CWD> command. Syntax error in parameters or arguments";
						l_iRetval = 0;
						break;
					case 502 :
						m_strError = "<CWD> command. Command not implemented";
						l_iRetval = 0;
						break;
					case 530 :
						m_strError = "<CWD> command. User not logged on";
						l_iRetval = 0;
						break;
					case 550 :
						m_strError = "<CWD> command. Requested action not taken.File unavailable (e.g., file not found, no access).";
						l_iRetval = 0;
						break;
					default:
						m_strError = "Unknown error in CWD command. Response code :" +  l_iResponseCode;
						l_iRetval = 0;
						break;
				}
			}
			else if ( l_strCommand.Equals("RETR") ) {
				/*	125,150,110,250,226 (success) */
				/*	226,425,426,451,450,500,501,502,530 (syntax error) */
				/*	421	(service not available) */
				switch( l_iResponseCode ) {
					case 125 : case 150 : case 110 : case 250 : case 226 : case 200 :
						l_iRetval = 1;
						break;
					case 425 :
						m_strError = "<RETR> command. Can't open data connection";
						l_iRetval = 0;
						break;
					case 426 :
						m_strError = "<RETR> command. Connection closed; transfer aborted";
						l_iRetval = 0;
						break;
					case 451 :
						m_strError = "<RETR> command. Requested action aborted: Local error in processing";
						l_iRetval = 0;
						break;
					case 450 :
						m_strError = "<RETR> command. Requested file action not taken.File unavailable (e.g., file busy)";
						l_iRetval = 0;
						break;
					case 500 : 
						m_strError = "<RETR> command. Syntax Error Command Unrecognized";
						l_iRetval = 0;
						break;
					case 501 :
						m_strError = "<RETR> command. Syntax error in parameters or arguments";
						l_iRetval = 0;
						break;
					case 502 :
						m_strError = "<RETR> command. Command not implemented";
						l_iRetval = 0;
						break;
					case 530 :
						m_strError = "<RETR> command. User not logged on";
						l_iRetval = 0;
						break;
					default:
						m_strError = "Unknown error in RETR command. Response code :" +  l_iResponseCode;
						l_iRetval = 0;
						break;
				}
			}
			else if ( l_strCommand.Equals("STOR") ) {
				/*	125,150,110,250,226 (success) */
				/*	425,426,451,551,552,532,450,452,533,500,501,530 (syntax error) */
				/*	550 (Access denied) */
				/*	421	(service not available) */
				switch( l_iResponseCode ) {
					case 125 : case 150 : case 110 : case 250 : case 226 :
						l_iRetval = 1;
						break;
					case 425 :
						m_strError = "<STOR> command. Can't open data connection";
						l_iRetval = 0;
						break;
					case 426 :
						m_strError = "<STOR> command. Connection closed; transfer aborted";
						l_iRetval = 0;
						break;
					case 451 :
						m_strError = "<STOR> command. Requested action aborted: Local error in processing";
						l_iRetval = 0;
						break;
					case 550 :
						m_strError = "<STOR> command. Requested action not taken. File unavailable (e.g., file not found, no access)";
						l_iRetval = 0;
						break;
					case 551 :
						m_strError = "<STOR> command. Requested action aborted: Page type unknown";
						l_iRetval = 0;
						break;
					case 552 :
						m_strError = "<STOR> command. Requested action aborted: Exceeded storage allocation (for current directory or dataset)";
						l_iRetval = 0;
						break;
					case 532 :
						m_strError = "<STOR> command. Need account for storing files";
						l_iRetval = 0;
						break;
					case 450 :
						m_strError = "<STOR> command. Requested file action not taken. File unavailable (e.g., file busy)";
						l_iRetval = 0;
						break;
					case 452 :
						m_strError = "<STOR> command. Requested file action not taken. Insufficient storage space in system";
						l_iRetval = 0;
						break;
					case 553 :
						m_strError = "<STOR> command. Requested action not taken. Filename not allowed";
						l_iRetval = 0;
						break;
					case 500 : 
						m_strError = "<STOR> command. Syntax Error Command Unrecognized";
						l_iRetval = 0;
						break;
					case 501 :
						m_strError = "<STOR> command. Syntax error in parameters or arguments";
						l_iRetval = 0;
						break;
					case 530 :
						m_strError = "<STOR> command. User not logged on";
						l_iRetval = 0;
						break;
					default:
						m_strError = "Unknown error in STOR command. Response code :" +  l_iResponseCode;
						l_iRetval = 0;
						break;
				}
			}
			else if ( l_strCommand.StartsWith("MKD") ) {
				/*	257,150 (success) */
				/*	500,501,502,530,550 (syntax error) */
				/*	421	(service not available) */
				switch ( l_iResponseCode ) {
					case 257 : case 150 :
						l_iRetval = 1;
						break;
					case 500 : 
						m_strError = "<MKD> command. Syntax Error Command Unrecognized";
						l_iRetval = 0;
						break;
					case 501 :
						m_strError = "<MKD> command. Syntax error in parameters or arguments";
						l_iRetval = 0;
						break;
					case 502 :
						m_strError = "<MKD> command. Command not implemented";
						l_iRetval = 0;
						break;
					case 530 :
						m_strError = "<MKD> command. User not logged on";
						l_iRetval = 0;
						break;
					case 550 :
						m_strError = "<MKD> command. Requested action not taken. File unavailable (e.g., file not found, no access)";
						l_iRetval = 0;
						break;
					default:
						m_strError = "Unknown error in MKD command. Response code :" +  l_iResponseCode;
						l_iRetval = 0;
						break;
				}
			}
			else if ( l_strCommand.StartsWith("RMD") ) {
				/*	250 ,150 (success) */
				/*	500,501,502,530,550 (syntax error) */
				/*	421	(service not available) */
				switch ( l_iResponseCode ) {
					case 250 : case 150 :
						l_iRetval = 1;
						break;
					case 500 : 
						m_strError = "<RMD> command. Syntax Error Command Unrecognized";
						l_iRetval = 0;
						break;
					case 501 :
						m_strError = "<RMD> command. Syntax error in parameters or arguments";
						l_iRetval = 0;
						break;
					case 502 :
						m_strError = "<RMD> command. Command not implemented";
						l_iRetval = 0;
						break;
					case 530 :
						m_strError = "<RMD> command. User not logged on";
						l_iRetval = 0;
						break;
					case 550 :
						m_strError = "<RMD> command. Requested action not taken. File unavailable (e.g., file not found, no access)";
						l_iRetval = 0;
						break;
					default:
						m_strError = "Unknown error in RMD command. Response code :" +  l_iResponseCode;
						l_iRetval = 0;
						break;
				}
			}
			else if ( l_strCommand.Equals("DELE") ) {
				/*	250 (success) */
				/*	450,550,500,501,502,530 (syntax error) */
				/*	421	(service not available) */
				switch ( l_iResponseCode ) {
					case 250 : case 226 :
						l_iRetval = 1;
						break;
					case 450 :
						m_strError = "<DELE> command. Requested action not taken. File unavailable (e.g., file not found, no access)";
						l_iRetval = 0;
						break;
					case 550 :
						m_strError = "<DELE> command. Requested action not taken. File unavailable (e.g., file not found, no access)";
						l_iRetval = 0;
						break;
					case 500 : 
						m_strError = "<DELE> command. Syntax Error Command Unrecognized";
						l_iRetval = 0;
						break;
					case 501 :
						m_strError = "<DELE> command. Syntax error in parameters or arguments";
						l_iRetval = 0;
						break;
					case 502 :
						m_strError = "<DELE> command. Command not implemented";
						l_iRetval = 0;
						break;
					case 530 :
						m_strError = "<DELE> command. User not logged on";
						l_iRetval = 0;
						break;
					default:
						m_strError = "Unknown error in DELE command. Response code :" +  l_iResponseCode;
						l_iRetval = 0;
						break;
				}
			}
			else if ( l_strCommand.Equals("QUIT") ) {
				/*	221	(success) */
				/*	500 */
				switch( l_iResponseCode ) {
					case 221 :
						l_iRetval = 1;
						break;
					case 500 :
						m_strError = "Syntax error, command unrecognized";
						l_iRetval = 1;
						break;
					default :
						m_strError = "Unknown error in QUIT command. Response code :" +  l_iResponseCode;
						l_iRetval = 1;
						break;
				}
			}
			return l_iRetval;
		}

		/// <summary>
		/// Executes commmand on a Ftp server 
		/// </summary>
		/// <param name="l_strCommand">Command string</param>
		/// <param name="l_refstrOutput">Output string from Ftp server </param>
		/// <returns>1 if success, else 0</returns>
		private int ExecuteCommand(string l_strCommand,ref string l_refstrOutput){
			int l_iRetval = 0;
			/*	Check for Status of FTP client socket here */
			if ( m_ClientSocket == null ) {
				m_strError = "Sockets not Initialized";
				return l_iRetval;
			}

			/*	Proceed with issuing command */
			Byte[] l_bSendData = new Byte[512];
			Byte[] l_bRecvData = new Byte[1024];
			string l_strOutput = "",l_strTemp = "";
			
			l_bSendData = Encoding.ASCII.GetBytes(l_strCommand);

			/* Send the command */
			l_iRetval = m_ClientSocket.Send(l_bSendData);
			if ( l_iRetval != l_bSendData.Length ) {
				m_strError = "Error in sending data";
				return 0;
			}

			l_bRecvData.Initialize();
			l_strTemp = "";
			l_strOutput = "";

			/* Receive data in a loop until the FTP server sends it */
			for ( ; ( l_iRetval = m_ClientSocket.Receive(l_bRecvData)) > 0 ;  ) {
				l_strTemp = Encoding.ASCII.GetString(l_bRecvData,0,l_iRetval);
				l_strOutput += l_strTemp;
				if ( m_ClientSocket.Available == 0 ){
					break;
				}
			}
			l_refstrOutput = l_strOutput;

			/*	Call ParseResponseCode() here */
			if ( l_strOutput.Length > 3 ) {
				l_iRetval = ParseResponseCode(l_strCommand.Substring(0,4),l_strOutput.Substring(0,3));
			}
			return l_iRetval;
		}

		/// <summary>
		/// LIST command implementation
		/// </summary>
		/// <returns></returns>
		public DataTable GetList(string l_strCurrDir){
			string l_strCommand = "";
			int l_iRetval = 0;

			if ( l_strCurrDir == null  ){
				/* if null is passed, populate root folder */
				l_strCurrDir = m_strRootDir;
			}
			l_strCurrDir.Trim();

			string l_strFileList = "";
			l_strCommand = "LIST " + l_strCurrDir + "\r\n";
			l_iRetval = ExecuteDataCommand(l_strCommand,out l_strFileList); 
			if ( l_iRetval != 1 ) {
				return null;
			}

			string[] l_strarrFiles = l_strFileList.Split("\n".ToCharArray());
			DataTable l_dtFileList = null;
			switch ( m_iServerOS) {
				case 1 :	/*	Unix	*/
					l_dtFileList = ParseStringArrayForUnix(l_strarrFiles);
					m_strError = "";
					break;
				case 2 :	/*	Windows	*/
					l_dtFileList = ParseStringArrayForWindows(l_strarrFiles);

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -