Form1.cs
上传用户:yiyuerguo
上传日期:2014-09-27
资源大小:3781k
文件大小:9k
源码类别:

C#编程

开发平台:

Others

  1. using System;
  2. using System.Drawing;
  3. using System.Collections;
  4. using System.ComponentModel;
  5. using System.Windows.Forms;
  6. using System.Data;
  7. using System.Net;
  8. using System.Net.Sockets;
  9. using System.IO;
  10. using System.Threading;
  11. using System.Text.RegularExpressions;
  12. using System.Text;
  13. namespace FtpClient
  14. {
  15. /// <summary>
  16. /// Form1 的摘要说明。
  17. /// </summary>
  18. public class Form1 : System.Windows.Forms.Form
  19. {
  20. private TcpClient client;
  21. private int i;
  22. private NetworkStream netStream;
  23. private FileStream filestream=null;
  24. private System.Windows.Forms.ComboBox comboBox1;
  25. private System.Windows.Forms.Button button1;
  26. private System.Windows.Forms.Button button2;
  27. private System.Windows.Forms.Button button3;
  28. private System.Windows.Forms.SaveFileDialog saveFileDialog1;
  29. private System.Windows.Forms.TextBox textBox1;
  30. private System.Windows.Forms.Label label1;
  31. private System.Windows.Forms.Label label2;
  32. private System.Windows.Forms.Label label3;
  33. private System.Windows.Forms.Label label4;
  34. private System.Windows.Forms.TextBox textBox2;
  35. private System.Windows.Forms.TextBox textBox3;
  36. //private string s = null;
  37. private string inputString = null;
  38. /// <summary>
  39. /// 必需的设计器变量。
  40. /// </summary>
  41. private System.ComponentModel.Container components = null;
  42. public Form1()
  43. {
  44. //
  45. // Windows 窗体设计器支持所必需的
  46. //
  47. InitializeComponent();
  48. //
  49. // TODO: 在 InitializeComponent 调用后添加任何构造函数代码
  50. //
  51. }
  52. /// <summary>
  53. /// 清理所有正在使用的资源。
  54. /// </summary>
  55. protected override void Dispose( bool disposing )
  56. {
  57. if( disposing )
  58. {
  59. if (components != null) 
  60. {
  61. components.Dispose();
  62. }
  63. }
  64. base.Dispose( disposing );
  65. }
  66. #region Windows Form Designer generated code
  67. /// <summary>
  68. /// 设计器支持所需的方法 - 不要使用代码编辑器修改
  69. /// 此方法的内容。
  70. /// </summary>
  71. private void InitializeComponent()
  72. {
  73. this.label1 = new System.Windows.Forms.Label();
  74. this.label2 = new System.Windows.Forms.Label();
  75. this.label3 = new System.Windows.Forms.Label();
  76. this.label4 = new System.Windows.Forms.Label();
  77. this.comboBox1 = new System.Windows.Forms.ComboBox();
  78. this.button1 = new System.Windows.Forms.Button();
  79. this.button2 = new System.Windows.Forms.Button();
  80. this.button3 = new System.Windows.Forms.Button();
  81. this.textBox1 = new System.Windows.Forms.TextBox();
  82. this.textBox2 = new System.Windows.Forms.TextBox();
  83. this.textBox3 = new System.Windows.Forms.TextBox();
  84. this.saveFileDialog1 = new System.Windows.Forms.SaveFileDialog();
  85. this.SuspendLayout();
  86. // 
  87. // label1
  88. // 
  89. this.label1.Location = new System.Drawing.Point(16, 24);
  90. this.label1.Name = "label1";
  91. this.label1.Size = new System.Drawing.Size(104, 24);
  92. this.label1.TabIndex = 1;
  93. this.label1.Text = "服务器 IP:";
  94. // 
  95. // label2
  96. // 
  97. this.label2.Location = new System.Drawing.Point(256, 24);
  98. this.label2.Name = "label2";
  99. this.label2.Size = new System.Drawing.Size(80, 16);
  100. this.label2.TabIndex = 2;
  101. this.label2.Text = "端口:";
  102. // 
  103. // label3
  104. // 
  105. this.label3.Location = new System.Drawing.Point(16, 48);
  106. this.label3.Name = "label3";
  107. this.label3.Size = new System.Drawing.Size(152, 24);
  108. this.label3.TabIndex = 3;
  109. this.label3.Text = "服务器文件列表:";
  110. // 
  111. // label4
  112. // 
  113. this.label4.Location = new System.Drawing.Point(16, 168);
  114. this.label4.Name = "label4";
  115. this.label4.Size = new System.Drawing.Size(112, 16);
  116. this.label4.TabIndex = 5;
  117. this.label4.Text = "选择文件:";
  118. // 
  119. // comboBox1
  120. // 
  121. this.comboBox1.Location = new System.Drawing.Point(16, 192);
  122. this.comboBox1.Name = "comboBox1";
  123. this.comboBox1.Size = new System.Drawing.Size(416, 20);
  124. this.comboBox1.TabIndex = 6;
  125. this.comboBox1.SelectedIndexChanged += new System.EventHandler(this.comboBox1_SelectedIndexChanged);
  126. // 
  127. // button1
  128. // 
  129. this.button1.Location = new System.Drawing.Point(16, 288);
  130. this.button1.Name = "button1";
  131. this.button1.Size = new System.Drawing.Size(64, 24);
  132. this.button1.TabIndex = 7;
  133. this.button1.Text = "连接";
  134. this.button1.Click += new System.EventHandler(this.ConnectBtn_click);
  135. // 
  136. // button2
  137. // 
  138. this.button2.Location = new System.Drawing.Point(104, 288);
  139. this.button2.Name = "button2";
  140. this.button2.Size = new System.Drawing.Size(64, 24);
  141. this.button2.TabIndex = 8;
  142. this.button2.Text = "下载";
  143. this.button2.Click += new System.EventHandler(this.DownloadBtn_click);
  144. // 
  145. // button3
  146. // 
  147. this.button3.Location = new System.Drawing.Point(200, 288);
  148. this.button3.Name = "button3";
  149. this.button3.Size = new System.Drawing.Size(72, 24);
  150. this.button3.TabIndex = 9;
  151. this.button3.Text = "关闭";
  152. this.button3.Click += new System.EventHandler(this.CloseBtn_click);
  153. // 
  154. // textBox1
  155. // 
  156. this.textBox1.Location = new System.Drawing.Point(88, 16);
  157. this.textBox1.Name = "textBox1";
  158. this.textBox1.Size = new System.Drawing.Size(144, 21);
  159. this.textBox1.TabIndex = 11;
  160. this.textBox1.Text = "";
  161. // 
  162. // textBox2
  163. // 
  164. this.textBox2.Location = new System.Drawing.Point(304, 16);
  165. this.textBox2.Name = "textBox2";
  166. this.textBox2.Size = new System.Drawing.Size(120, 21);
  167. this.textBox2.TabIndex = 12;
  168. this.textBox2.Text = "";
  169. // 
  170. // textBox3
  171. // 
  172. this.textBox3.Location = new System.Drawing.Point(16, 72);
  173. this.textBox3.Multiline = true;
  174. this.textBox3.Name = "textBox3";
  175. this.textBox3.Size = new System.Drawing.Size(408, 88);
  176. this.textBox3.TabIndex = 13;
  177. this.textBox3.Text = "";
  178. // 
  179. // Form1
  180. // 
  181. this.AutoScaleBaseSize = new System.Drawing.Size(6, 14);
  182. this.ClientSize = new System.Drawing.Size(440, 317);
  183. this.Controls.Add(this.textBox3);
  184. this.Controls.Add(this.textBox2);
  185. this.Controls.Add(this.textBox1);
  186. this.Controls.Add(this.button3);
  187. this.Controls.Add(this.button2);
  188. this.Controls.Add(this.button1);
  189. this.Controls.Add(this.comboBox1);
  190. this.Controls.Add(this.label4);
  191. this.Controls.Add(this.label3);
  192. this.Controls.Add(this.label2);
  193. this.Controls.Add(this.label1);
  194. this.Name = "Form1";
  195. this.Text = "Form1";
  196. this.ResumeLayout(false);
  197. }
  198. #endregion
  199. /// <summary>
  200. /// 应用程序的主入口点。
  201. /// </summary>
  202. [STAThread]
  203. static void Main() 
  204. {
  205. Application.Run(new Form1());
  206. }
  207. private void ConnectBtn_click(object sender, System.EventArgs e)
  208. {
  209. int port=0;
  210. IPAddress serverIP=IPAddress.Parse("127.0.0.1");
  211. try
  212. {
  213. serverIP=IPAddress.Parse(textBox1.Text);
  214. }
  215. catch{MessageBox.Show("The IP is not correct!");}
  216. client=new TcpClient();
  217. try
  218. {
  219. port=Int32.Parse(textBox2.Text);
  220. }
  221. catch{MessageBox.Show("Please enter a number!");}
  222. try
  223. {
  224. //连接服务器端IP地址和端口
  225. client.Connect(serverIP,port);
  226. //读取流
  227. netStream = client.GetStream();
  228. byte[] bb = new byte[6400];
  229. i = netStream.Read(bb,0,6400);
  230. string s = System.Text.Encoding.BigEndianUnicode.GetString(bb);
  231. textBox3.AppendText(s);
  232. int j = textBox3.Lines.Length;
  233. //将流中的内容按条加入comboBox1中
  234. for(int k=0;k<j-1;k++)
  235. {
  236. comboBox1.Items.Add(textBox3.Lines[k]);
  237. }
  238. comboBox1.Text = comboBox1.Items[0].ToString();
  239. }
  240. catch(Exception ee){MessageBox.Show(ee.Message);}
  241. }
  242. private void DownloadBtn_click(object sender, System.EventArgs e)
  243. {
  244. inputString = comboBox1.Text;
  245. string[] splitResults;
  246. string s = null;
  247. //以.作为分隔符 找到最后一个.后面的数据 则为文件名的后坠
  248. //作为saveFileDialog1的Filter
  249. splitResults = Regex.Split(inputString,"\.");
  250. StringBuilder resultsString = new StringBuilder(32);
  251. foreach(string stringElement in splitResults)
  252. {
  253. resultsString.Append(stringElement + "n");
  254. s =stringElement;
  255. }
  256. saveFileDialog1.FileName=comboBox1.Text;
  257. saveFileDialog1.Filter = "(*."+s+")|*."+s;
  258. if(saveFileDialog1.ShowDialog()==DialogResult.OK)
  259. {
  260. filestream = new FileStream(saveFileDialog1.FileName,FileMode.OpenOrCreate,FileAccess.Write);
  261. netStream = client.GetStream();
  262. string down = comboBox1.Text+"rn";
  263. byte[] by = System.Text.Encoding.BigEndianUnicode.GetBytes(down.ToCharArray());
  264. netStream.Write(by,0,by.Length);
  265. netStream.Flush();
  266. Thread thread = new Thread(new ThreadStart(download));
  267. thread.Start();
  268. }
  269. }
  270. private void download()
  271. {
  272. //建立一个流
  273. Stream stream=null;
  274. //从服务器端读取信息
  275. stream=client.GetStream();
  276. int length=1024;
  277. byte[] bye=new byte[1024];
  278. int size = stream.Read(bye,0,length);
  279. //把流的信息写入新文件中
  280. while(size > 0)
  281. {
  282. filestream.Write(bye,0,size);
  283. filestream.Flush();
  284. if (size<1024)
  285. break;
  286. size = stream.Read(bye,0,length);
  287. filestream.Close();
  288. MessageBox.Show("文件成功下载!");
  289. }
  290. private void CloseBtn_click(object sender, System.EventArgs e)
  291. {
  292. try
  293. {
  294. netStream=client.GetStream();
  295. string clo="***The client has been closed!***";
  296. byte[] by=System.Text.Encoding.BigEndianUnicode.GetBytes(clo.ToCharArray());
  297. netStream.Write(by,0,by.Length);
  298. netStream.Flush();
  299. client.Close();
  300. }
  301. catch{return;}
  302. }
  303. private void comboBox1_SelectedIndexChanged(object sender, System.EventArgs e)
  304. {
  305. }
  306. }
  307. }