Form1.cs
Upload User: chinafred
Upload Date: 2007-08-14
Package Size: 10127k
Code Size: 4k
Category:

ADO-ODBC

Development Platform:

C#

  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. namespace ch7_1
  10. {
  11. /// <summary>
  12. /// Form1 的摘要说明。
  13. /// </summary>
  14. public class Form1 : System.Windows.Forms.Form
  15. {
  16. /// <summary>
  17. /// 必需的设计器变量。
  18. /// </summary>
  19. private System.ComponentModel.Container components = null;
  20. private System.Windows.Forms.Label label1;
  21. private System.Windows.Forms.Label label2;
  22. private System.Windows.Forms.TextBox textBox2;
  23. private System.Windows.Forms.Button button1;
  24. private System.Windows.Forms.TextBox textBox3;
  25. private System.Windows.Forms.TextBox textBox1;
  26. public Form1()
  27. {
  28. //
  29. // Windows 窗体设计器支持所必需的
  30. //
  31. InitializeComponent();
  32. //
  33. // TODO: 在 InitializeComponent 调用后添加任何构造函数代码
  34. //
  35. }
  36. /// <summary>
  37. /// 清理所有正在使用的资源。
  38. /// </summary>
  39. protected override void Dispose( bool disposing )
  40. {
  41. if( disposing )
  42. {
  43. if (components != null) 
  44. {
  45. components.Dispose();
  46. }
  47. }
  48. base.Dispose( disposing );
  49. }
  50. #region Windows Form Designer generated code
  51. /// <summary>
  52. /// 设计器支持所需的方法 - 不要使用代码编辑器修改
  53. /// 此方法的内容。
  54. /// </summary>
  55. private void InitializeComponent()
  56. {
  57. this.label1 = new System.Windows.Forms.Label();
  58. this.textBox1 = new System.Windows.Forms.TextBox();
  59. this.label2 = new System.Windows.Forms.Label();
  60. this.textBox2 = new System.Windows.Forms.TextBox();
  61. this.button1 = new System.Windows.Forms.Button();
  62. this.textBox3 = new System.Windows.Forms.TextBox();
  63. this.SuspendLayout();
  64. // 
  65. // label1
  66. // 
  67. this.label1.Location = new System.Drawing.Point(24, 32);
  68. this.label1.Name = "label1";
  69. this.label1.Size = new System.Drawing.Size(56, 16);
  70. this.label1.TabIndex = 0;
  71. this.label1.Text = "主机名称";
  72. // 
  73. // textBox1
  74. // 
  75. this.textBox1.Location = new System.Drawing.Point(24, 64);
  76. this.textBox1.Name = "textBox1";
  77. this.textBox1.Size = new System.Drawing.Size(144, 21);
  78. this.textBox1.TabIndex = 1;
  79. this.textBox1.Text = "";
  80. // 
  81. // label2
  82. // 
  83. this.label2.Location = new System.Drawing.Point(24, 104);
  84. this.label2.Name = "label2";
  85. this.label2.Size = new System.Drawing.Size(64, 16);
  86. this.label2.TabIndex = 2;
  87. this.label2.Text = "IP地址";
  88. // 
  89. // textBox2
  90. // 
  91. this.textBox2.Location = new System.Drawing.Point(24, 136);
  92. this.textBox2.Name = "textBox2";
  93. this.textBox2.Size = new System.Drawing.Size(144, 21);
  94. this.textBox2.TabIndex = 3;
  95. this.textBox2.Text = "";
  96. // 
  97. // button1
  98. // 
  99. this.button1.Location = new System.Drawing.Point(24, 192);
  100. this.button1.Name = "button1";
  101. this.button1.TabIndex = 4;
  102. this.button1.Text = "获取";
  103. this.button1.Click += new System.EventHandler(this.button1_Click);
  104. // 
  105. // textBox3
  106. // 
  107. this.textBox3.Location = new System.Drawing.Point(120, 192);
  108. this.textBox3.Name = "textBox3";
  109. this.textBox3.Size = new System.Drawing.Size(120, 21);
  110. this.textBox3.TabIndex = 5;
  111. this.textBox3.Text = "textBox3";
  112. // 
  113. // Form1
  114. // 
  115. this.AutoScaleBaseSize = new System.Drawing.Size(6, 14);
  116. this.ClientSize = new System.Drawing.Size(264, 261);
  117. this.Controls.AddRange(new System.Windows.Forms.Control[] {
  118.   this.textBox3,
  119.   this.button1,
  120.   this.textBox2,
  121.   this.label2,
  122.   this.textBox1,
  123.   this.label1});
  124. this.Name = "Form1";
  125. this.Text = "获取IP地址";
  126. this.ResumeLayout(false);
  127. }
  128. #endregion
  129. /// <summary>
  130. /// 应用程序的主入口点。
  131. /// </summary>
  132. [STAThread]
  133. static void Main() 
  134. {
  135. Application.Run(new Form1());
  136. }
  137. private void button1_Click(object sender, System.EventArgs e)
  138. {
  139. //获取本地主机名称
  140. string host=Dns.GetHostName();
  141. this.textBox1.Text=host;
  142.            
  143. IPHostEntry ipHostEntry=Dns.GetHostByName(Dns.GetHostName());
  144. //获得本机IP地址
  145. IPAddress addr = new IPAddress ( ipHostEntry.AddressList [0].Address ) ;
  146. this.textBox2.Text=addr.ToString();
  147.           
  148. }
  149. }
  150. }