Form1.cs
上传用户:sflyled
上传日期:2013-01-18
资源大小:1760k
文件大小:9k
源码类别:

.net编程

开发平台:

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.IO;
  8. namespace htmlMaker
  9. {
  10. /// <summary>
  11. /// Summary description for Form1.
  12. /// </summary>
  13. public class Form1 : System.Windows.Forms.Form
  14. {
  15.         private System.Windows.Forms.Label label1;
  16.         private System.Windows.Forms.TextBox txbName;
  17.         private System.Windows.Forms.TextBox txbFilename;
  18.         private System.Windows.Forms.Label label2;
  19.         private System.Windows.Forms.Button btnGenerateCode;
  20.         private System.Windows.Forms.Button btnClearAll;
  21.         private System.Windows.Forms.Button btnExit;
  22.         private System.Windows.Forms.StatusBar stbMsg;
  23.         private string name = "";
  24.         private string filename = "";
  25.         private StreamWriter writer = null;
  26. /// <summary>
  27. /// Required designer variable.
  28. /// </summary>
  29. private System.ComponentModel.Container components = null;
  30. public Form1()
  31. {
  32. //
  33. // Required for Windows Form Designer support
  34. //
  35. InitializeComponent();
  36. //
  37. // TODO: Add any constructor code after InitializeComponent call
  38. //
  39. }
  40. /// <summary>
  41. /// Clean up any resources being used.
  42. /// </summary>
  43. protected override void Dispose( bool disposing )
  44. {
  45. if( disposing )
  46. {
  47. if (components != null) 
  48. {
  49. components.Dispose();
  50. }
  51. }
  52. base.Dispose( disposing );
  53. }
  54. #region Windows Form Designer generated code
  55. /// <summary>
  56. /// Required method for Designer support - do not modify
  57. /// the contents of this method with the code editor.
  58. /// </summary>
  59. private void InitializeComponent()
  60. {
  61.             this.label1 = new System.Windows.Forms.Label();
  62.             this.label2 = new System.Windows.Forms.Label();
  63.             this.btnExit = new System.Windows.Forms.Button();
  64.             this.btnClearAll = new System.Windows.Forms.Button();
  65.             this.btnGenerateCode = new System.Windows.Forms.Button();
  66.             this.stbMsg = new System.Windows.Forms.StatusBar();
  67.             this.txbFilename = new System.Windows.Forms.TextBox();
  68.             this.txbName = new System.Windows.Forms.TextBox();
  69.             this.SuspendLayout();
  70.             // 
  71.             // label1
  72.             // 
  73.             this.label1.Location = new System.Drawing.Point(32, 40);
  74.             this.label1.Name = "label1";
  75.             this.label1.Size = new System.Drawing.Size(56, 23);
  76.             this.label1.TabIndex = 0;
  77.             this.label1.Text = "姓名";
  78.             // 
  79.             // label2
  80.             // 
  81.             this.label2.Location = new System.Drawing.Point(32, 72);
  82.             this.label2.Name = "label2";
  83.             this.label2.Size = new System.Drawing.Size(56, 23);
  84.             this.label2.TabIndex = 0;
  85.             this.label2.Text = "文件名";
  86.             // 
  87.             // btnExit
  88.             // 
  89.             this.btnExit.Location = new System.Drawing.Point(216, 136);
  90.             this.btnExit.Name = "btnExit";
  91.             this.btnExit.TabIndex = 2;
  92.             this.btnExit.Text = "退  出";
  93.             this.btnExit.Click += new System.EventHandler(this.btnExit_Click);
  94.             // 
  95.             // btnClearAll
  96.             // 
  97.             this.btnClearAll.Location = new System.Drawing.Point(136, 136);
  98.             this.btnClearAll.Name = "btnClearAll";
  99.             this.btnClearAll.TabIndex = 2;
  100.             this.btnClearAll.Text = "清  空";
  101.             this.btnClearAll.Click += new System.EventHandler(this.btnClearAll_Click);
  102.             // 
  103.             // btnGenerateCode
  104.             // 
  105.             this.btnGenerateCode.Location = new System.Drawing.Point(56, 136);
  106.             this.btnGenerateCode.Name = "btnGenerateCode";
  107.             this.btnGenerateCode.TabIndex = 2;
  108.             this.btnGenerateCode.Text = "生  成";
  109.             this.btnGenerateCode.Click += new System.EventHandler(this.btnGenerateCode_Click);
  110.             // 
  111.             // stbMsg
  112.             // 
  113.             this.stbMsg.Location = new System.Drawing.Point(0, 205);
  114.             this.stbMsg.Name = "stbMsg";
  115.             this.stbMsg.Size = new System.Drawing.Size(348, 20);
  116.             this.stbMsg.TabIndex = 3;
  117.             // 
  118.             // txbFilename
  119.             // 
  120.             this.txbFilename.Location = new System.Drawing.Point(104, 72);
  121.             this.txbFilename.Name = "txbFilename";
  122.             this.txbFilename.Size = new System.Drawing.Size(152, 21);
  123.             this.txbFilename.TabIndex = 2;
  124.             this.txbFilename.Text = "who a u.html";
  125.             // 
  126.             // txbName
  127.             // 
  128.             this.txbName.Location = new System.Drawing.Point(104, 40);
  129.             this.txbName.Name = "txbName";
  130.             this.txbName.Size = new System.Drawing.Size(152, 21);
  131.             this.txbName.TabIndex = 1;
  132.             this.txbName.Text = "who a u";
  133.             // 
  134.             // Form1
  135.             // 
  136.             this.AutoScaleBaseSize = new System.Drawing.Size(6, 14);
  137.             this.ClientSize = new System.Drawing.Size(348, 225);
  138.             this.Controls.AddRange(new System.Windows.Forms.Control[] {
  139.                                                                           this.stbMsg,
  140.                                                                           this.btnExit,
  141.                                                                           this.btnClearAll,
  142.                                                                           this.btnGenerateCode,
  143.                                                                           this.txbFilename,
  144.                                                                           this.label2,
  145.                                                                           this.txbName,
  146.                                                                           this.label1});
  147.             this.FormBorderStyle = System.Windows.Forms.FormBorderStyle.FixedSingle;
  148.             this.Name = "Form1";
  149.             this.StartPosition = System.Windows.Forms.FormStartPosition.CenterScreen;
  150.             this.Text = "定制网页";
  151.             this.ResumeLayout(false);
  152.         }
  153. #endregion
  154. /// <summary>
  155. /// The main entry point for the application.
  156. /// </summary>
  157. [STAThread]
  158. static void Main() 
  159. {
  160. Application.Run(new Form1());
  161. }
  162.         private void btnGenerateCode_Click(object sender, System.EventArgs e)
  163.         {
  164.             name = txbName.Text.Trim();
  165.             filename = txbFilename.Text.Trim();
  166.             if((txbName.Text == "") || (txbFilename.Text == ""))
  167.             {
  168.                 stbMsg.Text = "请先输入"姓名"和"文件名"";
  169.                 txbName.Focus();
  170.             }
  171.             else
  172.             {
  173.                 if(GenerateCode())
  174.                 {
  175.                     stbMsg.Text = "已创建文件" + filename;
  176.                 }
  177.                 else
  178.                 {
  179.                     stbMsg.Text = "创建文件失败!";
  180.                 }
  181.             }
  182.         }
  183.         private void btnClearAll_Click(object sender, System.EventArgs e)
  184.         {
  185.             txbName.Text = "";
  186.             txbFilename.Text = "";
  187.             stbMsg.Text = "清空所有输入框";
  188.         }
  189.         private bool GenerateCode()
  190.         {
  191.             // 文件存在时是否覆盖
  192.             FileInfo f = new FileInfo(filename);
  193.             if(f.Exists)
  194.             {
  195.                 DialogResult s = MessageBox.Show("该文件已经存在,是否覆盖?" , "警告", MessageBoxButtons.YesNo, MessageBoxIcon.Exclamation);
  196.                 if(String.Format("{0}" , s) == "Yes")
  197.                 {
  198.                     f.Delete();
  199.                 }
  200.                 else
  201.                 {
  202.                     s = MessageBox.Show("请输入不同的文件名!" , "警告");
  203.                     txbFilename.Focus();
  204.                     return false;
  205.                 }
  206.             }
  207.             // 写文件
  208.             FileStream outputfile = null;
  209.             try
  210.             {
  211.                 outputfile = new FileStream(filename,FileMode.OpenOrCreate, FileAccess.Write);
  212.                 writer = new StreamWriter(outputfile);
  213.                 writer.BaseStream.Seek(0, SeekOrigin.End);
  214.                 DoWrite ("<HTML>");
  215.                 DoWrite ("<HEAD>");
  216.                 DoWrite ("<TITLE>");
  217.                 DoWrite (name + "介绍");
  218.                 DoWrite ("</TITLE>");
  219.                 DoWrite ("</HEAD>");
  220.                 DoWrite ("<BODY BGCOLOR='Tan'>");
  221.                 DoWrite ("<CENTER>");
  222.                 DoWrite ("<BR><BR><H2>欢迎参观 " + name + "的页面</H2>");
  223.                 DoWrite ("<BR><H4><A HREF='http://www.myclass.com'>校友录首页</A></H4>");
  224.                 DoWrite ("</CENTER>");
  225.                 DoWrite ("</BODY>");
  226.                 DoWrite ("</HTML>");
  227.                 writer.Close();
  228.             }
  229.             catch(Exception ex)
  230.             {
  231.                 Console.WriteLine("Exception GenerateCode = "+ex);
  232.                 stbMsg.Text = "Error";
  233.                 outputfile = null;
  234.                 writer = null;
  235.                 return false;
  236.             }
  237.             return true;
  238.         }
  239.         private void DoWrite (String line)
  240.         {
  241.             writer.WriteLine(line);
  242.             writer.Flush();
  243.         }
  244.         private void btnExit_Click(object sender, System.EventArgs e)
  245.         {
  246.             this.Close();
  247.         }
  248. }
  249. }