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

📄 ftpclient.cs

📁 用C#写的FTP客户端
💻 CS
📖 第 1 页 / 共 4 页
字号:
					break;
				default:
					l_iRetVal = 2;
					break;
			}
			return l_iRetVal;
		}

		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_strErrorMessage = "Error : Sockets not initialised";
				return l_iRetval;
			}

			/*	Proceed with issuing command */
			Byte[] l_bSendData = new Byte[512];
			Byte[] l_bRecvData = new Byte[512];
			string l_strOutput = "",l_strTemp = "";
			
			l_bSendData = Encoding.ASCII.GetBytes(l_strCommand);
			l_iRetval = m_ClientSocket.Send(l_bSendData,l_bSendData.Length,0);
			if ( l_iRetval != l_bSendData.Length ) {
				m_strErrorMessage = "Error : Error in sending data";
				return 0;
			}

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

			for ( ; ( l_iRetval = m_ClientSocket.Receive(l_bRecvData,511,0)) > 0 ;  ) {
				l_strTemp = Encoding.ASCII.GetString(l_bRecvData,0,l_iRetval);
				l_strOutput += l_strTemp;
			}

			l_refstrOutput = l_strOutput;
			m_strErrorMessage = "";

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

		public string GetLastError(){
			return m_strErrorMessage;
		}

		/*	LIST command implementation */
		public int GetList(){
			//FTPDataConn conn = new FTPDataConn();
			string l_strCommand = "";
			int l_iRetval = 0;

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

			/*	Set Message in the parent window */
			m_ParentWindow.SetStausMessage("Listing Folders and Files");

/*			{
				*	Write to a file here *
				File f = new File(@"C:\Temp\b.txt");
				Stream s = f.OpenWrite();
				Byte[] l_bData = new Byte[l_strFileList.Length + 1];
				l_bData.Initialize();
				l_bData = Encoding.ASCII.GetBytes(l_strFileList);
				s.Write(l_bData,0,l_bData.Length);
				s.Close();
				f.Refresh();
				*	end of file handling *

			}
*/
			string[] l_strarrFiles = l_strFileList.Split("\n".ToCharArray());
			int l_iCount = 0;
			switch (m_iServerOS) {
				case 1 :	/*	Unix	*/
					l_iCount = ParseStringArrayForUnix(l_strarrFiles);
					break;
				case 2 :	/*	Windows	*/
					l_iCount = ParseStringArrayForWind(l_strarrFiles);
					break;
				default:
					break;
			}

			/*	Set Message in the parent window */
			string l_strText = l_iCount + " objects found";
			m_ParentWindow.SetStausMessage(l_strText);

			m_strErrorMessage = "";
			return 1;
		}

		/*	Called for Windows Servers, Parses the array and fills
		 *	the m_FileList object with list of files in each node
		 * */
		private int ParseStringArrayForWind(string[] l_arrFiles){
			m_FileList.RemoveAll();

			string l_strcurFile = "";
			int l_iFileCount = 0;
			char[] l_arrchSep = new char[2];
			l_arrchSep[0] = ' ';
			l_arrchSep[1] = '\0';
			string[] l_strarrFields = new string[5];

			foreach(object obj in l_arrFiles) {
				l_strcurFile = (string) obj;
				l_strcurFile.Trim();
				
				if (l_strcurFile == null || l_strcurFile.Length == 0 ) {
					break;
				}

				l_strarrFields = SplitStringForWindows(l_strcurFile);
				if ( l_strarrFields.Length == 1 || l_strarrFields.Length <= 0 ) {
					/*	If the element is empty, do not attempt to parse it */
					continue;
				}

				/*	Each field is now in l_strarrFields */
				FileInfo l_FileInfo = GetFileInfoForWindows(l_strarrFields);
				if ( l_FileInfo != null ) {
					/*	Add it to list Object */
					m_FileList.Add(l_FileInfo);
				}
				l_iFileCount++;
			}
			return l_iFileCount;
		}

		private string[] SplitStringForWindows(string l_strInString){
			string[] l_strarrOut = new string[5];
			string l_strTemp = "";
			char[] l_arrchSep = new char[2];
			l_arrchSep[0] = ' ';
			l_arrchSep[1] = '\0';
			int l_iFieldCount = 0;

			/*	Squeeze more spaces into single space character	*/
			int l_iCount = 0,l_iLen = l_strInString.Length;
			for ( ; l_iCount < l_iLen ; l_iCount++ ) {
				if ( l_strInString[l_iCount] == ' ' ) {
					if ( l_iFieldCount <= 1 ) {
						l_strarrOut[l_iFieldCount] = l_strTemp;
						l_iFieldCount++;
						l_strTemp = "";
						if ( l_strInString[l_iCount + 1] == ' ' ) {
							for ( l_iCount++ ;  l_strInString[l_iCount] == ' '; l_iCount++ ) ;
							/*	Postion the counter back to previous character	*/
							l_iCount--;
						}
					}
					else if ( l_iFieldCount == 2 ) {
						/*	Check for first digit here 
						 * if '<' then it is a directory
						 * else it is a file
						 * */
						if ( l_strTemp[0] == '<' ) {
							/*	Directory Flag	*/
							l_strarrOut[l_iFieldCount] = "D";
							l_iFieldCount++;

							/*	size of directory is not known	*/
							l_strarrOut[l_iFieldCount] = "0";
							l_iFieldCount++;
							/*	Skip the rest of characters */
							for ( l_iCount++ ;  l_strInString[l_iCount] != ' '; l_iCount++ ) ;
						}
						else {
							/*	Normal File	*/
							l_strarrOut[l_iFieldCount] = "N";
							l_iFieldCount++;
							l_strarrOut[l_iFieldCount] = l_strTemp;
							l_iFieldCount++;
						}
					}
				}
				else {
					if ( l_iFieldCount == 4 ) {
						/*	File name */
						l_strTemp = "";
						for ( ; l_iCount < l_strInString.Length ; l_iCount++ ){
							l_strTemp += l_strInString[l_iCount];
						}
						/*	remove \r\n character */
						l_strTemp = l_strTemp.Substring(0,l_strTemp.Length - 1);
						l_strarrOut[l_iFieldCount] = l_strTemp;
					}
					else {
						l_strTemp += l_strInString[l_iCount];
					}
				}
			}

			//l_strarrOut = l_strTemp.Split(l_arrchSep);
			return l_strarrOut;
		}

		private FileInfo GetFileInfoForWindows(string[] l_strarrFields){
			FileInfo l_FileInfo = new FileInfo();
			string l_strField = "";
			string l_strFileName = "" ,l_strFilePath = "" ,l_strDateCreated = "";
			string l_strTimeCreated = "";
			int l_iFileType = 0,l_iFileSize = 0;
			int l_iCount = 0;

			foreach(object obj in l_strarrFields) {
				l_strField = (string) obj;
				if (l_strField == null ) {
					break;
				}
				switch( l_iCount) {
					case 0:		/*	Date */
						l_strDateCreated = l_strField;
						break;
					case 1 :	/*	Time */
						l_strTimeCreated = l_strField;
						break;
					case 2 :	/*	File Type  N - normal file, D - directory	*/
						if ( l_strField.Equals("N") ) {
							l_iFileType = 1;
						}
						else if ( l_strField.Equals("D") ) {
							l_iFileType = 2;
						}
						break;
					case 3 :	/*	File size in bytes */
						try {
							l_iFileSize = int.Parse(l_strField);
						}
						catch ( FormatException e ) {
							l_iFileSize = 0;
						}
						break;
					case 4 :	/*	Name of the file */
						l_strFileName = l_strField;
						break;
				}	/*	End of switch statement */
				l_iCount++;
			}	/*	End of for */

			/*	If error occured while parsing return null and ignore this element */
			/*if (l_iErrFlag == 1 ) {
				return null;
			}
			*/
			l_strFilePath = m_strCurrentWorkingDir + "/" + l_strFileName;
			l_FileInfo.SetFileInfo(l_iFileType,l_strFileName,l_strFilePath,l_strDateCreated,l_iFileSize,"","",l_strTimeCreated);
			return l_FileInfo;
		}

		/*	Called for Unix Servers,Parses the array and fills the  
		 *	m_FileList object with list of files in each node
		 * */
		private int ParseStringArrayForUnix(string[] l_arrFiles){
			m_FileList.RemoveAll();

			string l_strcurFile = "";
			int l_iFileCount = l_arrFiles.Length;
			int l_iLineCount = 0;
			char[] l_arrchSep = new char[2];
			l_arrchSep[0] = ' ';
			l_arrchSep[1] = '\0';

			foreach(object obj in l_arrFiles) {
				l_strcurFile = (string) obj;

				/*	If first line then parse for length */
				if ( l_iLineCount == 0 ) {
					string[] l_strTemp = l_strcurFile.Split(l_arrchSep);
					try {
						string l_strLen = l_strTemp[1];
						l_strLen.Trim();
						m_FileList.TotalLength = l_strLen.ToInt32();
					}
					catch ( FormatException e ) {
						m_FileList.TotalLength = 0;
					}
					catch(Exception e) {
						m_FileList.TotalLength = 0;
					}
				}
				else {
					/*	Parse for file info */
					string[] l_strarrFields = SplitStringForUnix(l_strcurFile);
					if (l_strarrFields.Length == 1 || l_strarrFields[0].Length <= 0 ) {
						/*	If the element is empty, do not attempt to parse it */
						l_iLineCount++;
						continue;
					}
					/*	Each field is now in l_strarrFields */
					FileInfo l_FileInfo = GetFileInfoForUnix(l_strarrFields);
					if ( l_FileInfo != null ) {
						/*	Add it to list Object */
						m_FileList.Add(l_FileInfo);
					}
				}
				l_iLineCount++;
			}
			return l_iLineCount;
		}

		private FileInfo GetFileInfoForUnix(string[] l_strarrFields){
			FileInfo l_FileInfo = new FileInfo();
			string l_strField = "";
			string l_strFileName = "" ,l_strFilePath = "" ,l_strDateCreated = "";
			string l_strFileOwner = "",l_strFileGroup = "";
			int l_iFileType = 0,l_iFileSize = 0;
			int l_iCount = 0,l_iErrFlag = 0;
			char l_chTemp = '\0';

			foreach(object obj in l_strarrFields) {
				l_strField = (string) obj;
				if (l_strField.Length <= 0 ) {
					l_iErrFlag = 1;
					break;
				}
				switch( l_iCount) {
					case 0:		/*	File Type */
						l_chTemp = l_strField[0];
						if ( l_chTemp == '-' ) {
							/*	Normal File */
							l_iFileType = 1;
						}
						else if ( l_chTemp == 'd') {
							/*	Directory */
							l_iFileType = 2;
						}
						else if ( l_chTemp == 'c' ) {
							/*	System file */
							l_iFileType = 3;
						}
						else {
							/*	Unknown file type */
							l_iFileType = 0;
						}
						break;
					case 1 :	/*	File Links */
						break;
					case 2 :	/*	File Owner */
						l_strFileOwner = l_strField;
						break;
					case 3 :	/*	File group */
						l_strFileGroup = l_strField;
						break;
					case 4 :	/*	File size in bytes */
						l_iFileSize = int.Parse(l_strField);
						break;
					case 5 :	/*	Date created ( contains month in mmm format ) */
						l_strDateCreated = l_strField;
						break;
					case 6 :	/*	Date created ( contains day in dd format ) */
						l_strDateCreated += " " + l_strField;
						break;
					case 7 :	/*	Date created ( contains time in HH:SS or year in YYYY format ) */
						l_strDateCreated += " " + l_strField;
						break;
					case 8 :	/*	File name */
						l_strFileName = l_strField;
						l_strFileName = l_strFileName.Trim();
						if (l_strFileName.Length == 0 ) {
							l_strFileName = "<unknown>";
						}
						break;
					case 9 :
						break;
				}	/*	End of switch statement */
				l_iCount++;
			}	/*	End of for */

			/*	If error occured while parsing return null and ignore this element */
			if (l_iErrFlag == 1 ) {
				return null;
			}

			l_strFilePath = m_strCurrentWorkingDir + "/" + l_strFileName;
			l_FileInfo.SetFileInfo(l_iFileType,l_strFileName,l_strFilePath,l_strDateCreated,l_iFileSize,l_strFileOwner,l_strFileGroup,"");
			return l_FileInfo;
		}


		/*	This function takes a string and squeezes more than
		 *	one space characters into one character and 
		 *	then splits into array and returns back to the caller

⌨️ 快捷键说明

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