asp.net FTP客户端设计与开发

4.1 软件总体分析与设计
根据需求分析,按照系统开发的基本观点对功能进行分解,从功能上可对模块作如下划分:
1.连接管理模块:主要完成主机与服务器之间的连接与关闭操作。
2.文件管理模块:主要完成文件的显示、新建文件、删除文件等。
3.文件传输模块:主要完成主机与服务器连接成功以后文件的上传与下载。
4.辅助功能模块:主要是保存一些登录信息和一些简单的配置信息。

4.3 模块的程序实现
4.3.1 连接管理的程序实现
在用户打开软件后进入主窗体(MFFTP.cs),在连接服务器输入框中输入IP地址、用户名及密码后。先发送IP地址和端口号到服务器,然后对其应答分析,如果应答码为220表示对新用户服务准备好,继续发送用户名返回应答码331表示用户名正确需要口令。最后发送密码直到返回应答码230表示用户登录成功。其主要代码程序如下:

		public void Connect()
		{
			socketControl = new Socket(AddressFamily.InterNetwork,SocketType.Stream,ProtocolType.Tcp);
			IPEndPoint ep = new IPEndPoint(IPAddress.Parse(RemoteHost), strRemotePort);
			// 链接
			try
			{
				socketControl.Connect(ep);
			}
			catch(Exception)
			{
				throw new IOException("Couldn't connect to remote server");
			}
			// 获取应答码
			ReadReply();
			if(iReplyCode != 220)
			{
				DisConnect();
				throw new IOException(strReply.Substring(4));
			}
			// 登录
			SendCommand("USER "+strRemoteUser);
			if( !(iReplyCode == 331 || iReplyCode == 230) )
			{
				CloseSocketConnect();//关闭连接
				throw new IOException(strReply.Substring(4));
			}
			if( iReplyCode != 230 )
			{
				SendCommand("PASS "+strRemotePass);
				if( !(iReplyCode == 230 || iReplyCode == 202) )
				{
					CloseSocketConnect();//关闭连接
					throw new IOException(strReply.Substring(4));
				}
			}
			bConnected = true;
			// 切换到目录
			ChDir(strRemotePath);
		}

4.3.2 文件管理的程序实现
对文件的管理有新建、删除、及属性设置。这里只对文件夹的删除操作做介绍,先实例化DirectoryInfo类并传入参数path,然后判断是文件还是文件夹用Delete()方法来删除文件。其主要代码程序如下:
private void MyDeleteFile(string path)
{//删除本地文件
try
{
if(path==null)
return;
DirectoryInfo MyDir=new DirectoryInfo(path);
if(MessageBox.Show(“是否删除文件夹:”+path+“及其所有内容?”,“提示信息”,MessageBoxButtons.YesNo,MessageBoxIcon.Question)==DialogResult.Yes)
{
MyDir.Delete(true);

			}
			else if(MessageBox.Show("是否删除文件:"+path+"及其所有内容?","提示信息",MessageBoxButtons.YesNo,MessageBoxIcon.Question)==DialogResult.Yes)
			{
				FileInfo fi=new FileInfo(path);
				fi.Delete();
			}
		}
		catch
		{
			MessageBox.Show("该文件不存在","信息提示",MessageBoxButtons.OK,MessageBoxIcon.Information);
		}
	}

4.3.3 文件传输的程序实现
先进行判断是否与主机连接成功,获取要下载的文件名、保存到本机的路径、保存到本机时的文件名。在进行设置传输模式:二进制Binary传输或ACSII传输,在创建数据连接发送PASV被动模式进行传输然后对应答命令进行判断。最后进行数据传输以流方式传输。其主要代码程序如下:

		public void Get(string strRemoteFileName,string strFolder,string strLocalFileName)
		{
			if(!bConnected)
			{
				Connect();
			}
			SetTransferType(TransferType.Binary);
			if (strLocalFileName.Equals(""))
			{
				strLocalFileName = strRemoteFileName;
			}
			if(!File.Exists(strLocalFileName))
			{
				Stream st = File.Create(strLocalFileName);
				st.Close();
			}
			FileStream output = new 
				FileStream(strFolder + "\\" + strLocalFileName,FileMode.Create);
			Socket socketData = CreateDataSocket();
			SendCommand("RETR " + strRemoteFileName);
			if(!(iReplyCode == 150 || iReplyCode == 125
				|| iReplyCode == 226 || iReplyCode == 250))
			{
				throw new IOException(strReply.Substring(4));
			}
			while(true)
			{
				int iBytes = socketData.Receive(buffer, buffer.Length, 0);
				output.Write(buffer,0,iBytes);
				if(iBytes <= 0)
				{
					break;
				}
			}
			output.Close();
			if (socketData.Connected)
			{
				socketData.Close();
			}
			if(!(iReplyCode == 226 || iReplyCode == 250))
			{
				ReadReply();
				if(!(iReplyCode == 226 || iReplyCode == 250))
				{
					throw new IOException(strReply.Substring(4));
				}
			}
		}

4.3.4 辅助功能的程序实现
当需要返回上级目录时,先检查当前目录字符串是否小于3,如果小于了3则表示已经是跟目录了,不能在返回上级目录了。其他情况直接用Substring来去掉最后一个目录。具体办法是每次取字符串从0到最后一个“\”。然后把该字符串赋值给ComboBox。程序代码如下:其主要代码程序如下:

		private void but_Fa_Click(object sender, System.EventArgs e)
		{//返回上级目录
			string path=this.comboBox1.Text;
			string newpath;
			if(path.EndsWith("\\"))
			{
				if(path.Length<=3)
				{
					MessageBox.Show("根目录了!","系统提示",MessageBoxButtons.OK,MessageBoxIcon.Information);
					return;
				}
				newpath=path.Substring(0,path.LastIndexOf("\\"));
				this.comboBox1.Text=newpath.Substring(0,newpath.LastIndexOf("\\"));
			}
			else
			{
				if(path.LastIndexOf("\\")!=2)
				{
					newpath=path.Substring(0,path.LastIndexOf("\\"));
					this.comboBox1.Text=newpath;
				}
				else
				{
					newpath=path.Substring(0,path.LastIndexOf("\\")+1);
					this.comboBox1.Text=newpath;
					return;
				}
			}
		}

当在第一次登录主机时,为方便以后在次登录该主机则需要保存其登录信息。先把所有的输入框中的值赋给有代表意思的字符串。如果主机别名为空则主机别名与主机地址相同。
然后通过IniWriteValue方法来写入mfftp.ini文件中,其主要代码程序如下:

private void but_Ok_Click(object sender, System.EventArgs e)
		{//添加登录主机信息
			string hostname=this.text_Name.Text.Trim();
			string hostip=this.text_SerIp.Text.Trim();
			string loginname=this.loginName.Text.Trim();
			string loginpwd=this.login_Pwd.Text.Trim();
			string mydir=this.text_add.Text.Trim();
			if(hostname=="")
			{
				hostname=hostip;
			}
			IniFile inf=new IniFile();
			inf.path=".\\mfftp.ini";	
			int i=Convert.ToInt32(inf.IniReadValue("MFFTP_Options","HostNum"));
			//string serAdd=inf.IniReadValue("MFFTP_OptionsHost0","HostIp");
			string ServerName= "MFFTP_OptionsHost"+i;
			inf.IniWriteValue(ServerName,"HostName",hostname);
			inf.IniWriteValue(ServerName,"HostIp",hostip);
			inf.IniWriteValue(ServerName,"LoginName",loginname);
			inf.IniWriteValue(ServerName,"LoginPwd",loginpwd);
			inf.IniWriteValue(ServerName,"MyDir",mydir);
			i=i+1;
			inf.IniWriteValue("MFFTP_Options","HostNum",i.ToString());
			this.standm.sername(hostname);
			this.Close();
		}	

using System;
using System.Drawing;
using System.Collections;
using System.ComponentModel;
using System.Windows.Forms;

namespace MFFTP
{
	/// <summary>
	/// CreatNewName 的摘要说明。
	/// </summary>
	public class CreatNewName : System.Windows.Forms.Form
	{
		private System.Windows.Forms.TextBox text_newName;
		private System.Windows.Forms.Button but_Ok;
		private System.Windows.Forms.Button but_Cancel;
		/// <summary>
		/// 必需的设计器变量。
		/// </summary>
		private System.ComponentModel.Container components = null;
        private MFFTP mfftp;
		public CreatNewName(MFFTP FTP)
		{
			//
			// Windows 窗体设计器支持所必需的
			//
			InitializeComponent();
			this.mfftp=FTP;

			//
			// TODO: 在 InitializeComponent 调用后添加任何构造函数代码
			//
		}

		/// <summary>
		/// 清理所有正在使用的资源。
		/// </summary>
		protected override void Dispose( bool disposing )
		{
			if( disposing )
			{
				if(components != null)
				{
					components.Dispose();
				}
			}
			base.Dispose( disposing );
		}

		#region Windows 窗体设计器生成的代码
		/// <summary>
		/// 设计器支持所需的方法 - 不要使用代码编辑器修改
		/// 此方法的内容。
		/// </summary>
		private void InitializeComponent()
		{
			this.text_newName = new System.Windows.Forms.TextBox();
			this.but_Ok = new System.Windows.Forms.Button();
			this.but_Cancel = new System.Windows.Forms.Button();
			this.SuspendLayout();
			// 
			// text_newName
			// 
			this.text_newName.Location = new System.Drawing.Point(48, 40);
			this.text_newName.Name = "text_newName";
			this.text_newName.Size = new System.Drawing.Size(144, 21);
			this.text_newName.TabIndex = 0;
			this.text_newName.Text = "";
			// 
			// but_Ok
			// 
			this.but_Ok.Location = new System.Drawing.Point(56, 112);
			this.but_Ok.Name = "but_Ok";
			this.but_Ok.TabIndex = 1;
			this.but_Ok.Text = "修 改";
			this.but_Ok.Click += new System.EventHandler(this.but_Ok_Click);
			// 
			// but_Cancel
			// 
			this.but_Cancel.Location = new System.Drawing.Point(160, 112);
			this.but_Cancel.Name = "but_Cancel";
			this.but_Cancel.TabIndex = 2;
			this.but_Cancel.Text = "取 消";
			this.but_Cancel.Click += new System.EventHandler(this.but_Cancel_Click);
			// 
			// CreatNewName
			// 
			this.AutoScaleBaseSize = new System.Drawing.Size(6, 14);
			this.ClientSize = new System.Drawing.Size(352, 174);
			this.ControlBox = false;
			this.Controls.Add(this.but_Cancel);
			this.Controls.Add(this.but_Ok);
			this.Controls.Add(this.text_newName);
			this.Name = "CreatNewName";
			this.Text = "重命名";
			this.ResumeLayout(false);

		}
		#endregion

		private void but_Cancel_Click(object sender, System.EventArgs e)
		{
			this.Close();
		}

		private void but_Ok_Click(object sender, System.EventArgs e)
		{
			string newFileName=this.text_newName.Text.Trim();
			this.mfftp.newFileName(newFileName);
			this.Close();
		}
	}
}

链接:https://pan.baidu.com/s/1Xwd0U_KktL0hRFPaggIOGQ?pwd=6688
提取码:6688

  • 10
    点赞
  • 9
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值