您的位置:首页 > 产品设计 > UI/UE

关于C#的FTPRequest客户类如何使用通配符去服务器端匹配文件

2010-01-14 21:42 549 查看
private static FtpWebRequest GetRequest( string URI, string username, string password )
		{
			FtpWebRequest result = ( FtpWebRequest )FtpWebRequest.Create( URI );
			result.Credentials = new System.Net.NetworkCredential( username, password );
			result.KeepAlive = false;
			return result;
		}

		/// <summary>
		/// 搜索远程文件
		/// </summary>
		/// <param name="targetDir"></param>
		/// <param name="hostname"></param>
		/// <param name="username"></param>
		/// <param name="password"></param>
		/// <param name="SearchPattern"></param>
		/// <returns></returns>
		public static List<string> ListDirectory( string targetDir, string hostname, string username, string password, string SearchPattern )
		{
			List<string> result = new List<string>();
			try
			{
				string URI = "FTP://" + hostname + "/" + targetDir + "/R_*Apply_*_*.xml";

				System.Net.FtpWebRequest ftp = GetRequest( URI, username, password );
				ftp.Method = System.Net.WebRequestMethods.Ftp.ListDirectory;
				ftp.UsePassive = true;
				ftp.UseBinary = true;

				string str = GetStringResponse( ftp );
				str = str.Replace( "/r/n", "/r" ).TrimEnd( '/r' );
				str = str.Replace( "/n", "/r" );
				if ( str != string.Empty )
					result.AddRange( str.Split( '/r' ) );
				//if (result.Count!=0)
				//CommonLogger.Info(OMSInterfaceType.FTP, string.Format("浏览远程目录找到文件{0}个", result.Count.ToString()));
				return result;
			}
			catch ( Exception ex )
			{
				Console.WriteLine( ex.Message );
				return null;
			}
		}

		private static string GetStringResponse( FtpWebRequest ftp )
		{
			//Get the result, streaming to a string
			string result = "";
			using ( FtpWebResponse response = ( FtpWebResponse )ftp.GetResponse() )
			{
				long size = response.ContentLength;
				using ( Stream datastream = response.GetResponseStream() )
				{
					using ( StreamReader sr = new StreamReader( datastream, System.Text.Encoding.Default ) )
					{
						result = sr.ReadToEnd();
						sr.Close();
					}

					datastream.Close();
				}

				response.Close();
			}

			return result;
		}

说明,其它就是就是在List目录的时候,后面直接带上匹配表达式即可以实现
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: 
相关文章推荐