Form1.cs 6.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196
  1. using System;
  2. using System.Collections.Generic;
  3. using System.ComponentModel;
  4. using System.Data;
  5. using System.Drawing;
  6. using System.Linq;
  7. using System.Text;
  8. using System.Threading.Tasks;
  9. using System.Windows.Forms;
  10. using WordAnalyze;
  11. using Logger = Log.Log;
  12. namespace WindowsFormsApp1
  13. {
  14. public partial class Form1 : Form
  15. {
  16. private WordAnalyze.Analyze Analyzer = null;
  17. public Form1()
  18. {
  19. InitializeComponent();
  20. Analyzer = new Analyze();
  21. Logger.Init("log.log");
  22. init_listView();
  23. init_textBox();
  24. }
  25. // 初始化显示的列表
  26. private void init_listView()
  27. {
  28. listView1.View = View.Details;
  29. Console.WriteLine(listView1.Columns.Count);
  30. // 第一列,文件名
  31. if (0 == listView1.Columns.Count)
  32. {
  33. listView1.Columns.Add("文件名");
  34. Console.WriteLine(listView1.Columns.Count);
  35. }
  36. Console.WriteLine(listView1.Columns.Count);
  37. listView1.Columns[0].Text = "文件名";
  38. listView1.Columns[0].Width = 200;
  39. listView1.Columns[0].TextAlign = HorizontalAlignment.Left;
  40. // 第二列,状态
  41. if (1 == listView1.Columns.Count)
  42. {
  43. listView1.Columns.Add("状态");
  44. }
  45. listView1.Columns[1].Text = "状态";
  46. listView1.Columns[1].Width = 50;
  47. listView1.Columns[1].TextAlign = HorizontalAlignment.Left;
  48. // 第三列,错误信息,成功则忽略
  49. if (2 == listView1.Columns.Count)
  50. {
  51. listView1.Columns.Add("信息");
  52. }
  53. listView1.Columns[2].Text = "信息";
  54. listView1.Columns[2].Width = 300;
  55. listView1.Columns[2].TextAlign = HorizontalAlignment.Left;
  56. }
  57. // 初始化存储文件夹的显示框
  58. private void init_textBox()
  59. {
  60. textBox1.Enabled = false;
  61. }
  62. // 开始处理word文档
  63. private void button1_Click(object sender, EventArgs e)
  64. {
  65. if (fileNames == null || fileNames.Length < 1)
  66. {
  67. Logger.D("event start, but no file to analyse");
  68. return;
  69. }
  70. var errString = Analyzer.Application();
  71. if(errString != "")
  72. {
  73. Logger.E("analyze new word analyze object error happened: {0}", errString);
  74. return;
  75. }
  76. button1.Enabled = false;
  77. listview_draw(fileNames);
  78. for (int i = 0; i < fileNames.Length; i ++)
  79. {
  80. execStatus(i);
  81. Logger.D("startanalyze with filename({0})", fileNames[i]);
  82. if (listView1.Items.Count < i + 1)
  83. {
  84. Logger.D("analyze with filename({0}) success but item with index({1}) is unexpected", fileNames[i], i);
  85. continue;
  86. }
  87. if (listView1.Items[i].SubItems.Count != 3)
  88. {
  89. Logger.D("analyze with filename({0}) success but item with index({1}) contain unexpected subitem length({2})", fileNames[i], i, listView1.Items[i].SubItems.Count);
  90. continue;
  91. }
  92. var result = Analyzer.AnalyzeFile(fileNames[i]);
  93. string status = "成功";
  94. if (result != "")
  95. {
  96. status = "失败";
  97. }
  98. listView1.Items[i].SubItems[1].Text = status;
  99. listView1.Items[i].SubItems[2].Text = result;
  100. Logger.D("analyze with filename({0}) success with result({1})", fileNames[i], result);
  101. }
  102. try
  103. {
  104. Analyzer.Close();
  105. }catch(System.Exception err)
  106. {
  107. Logger.E("analyze quit but error happened: {0}", err.Message.ToString());
  108. }
  109. button1.Enabled = true;
  110. }
  111. private void folderBrowserDialog1_HelpRequest(object sender, EventArgs e)
  112. {
  113. }
  114. private void openFileDialog1_FileOk(object sender, CancelEventArgs e)
  115. {
  116. }
  117. private void button2_Click(object sender, EventArgs e)
  118. {
  119. // 选择文件
  120. OpenFileDialog openFileDialog = new OpenFileDialog();
  121. openFileDialog.Filter = "doc files (*.doc)|*.doc| docx files (*.docx)|*.docx";
  122. openFileDialog.Multiselect = true;
  123. openFileDialog.Title = "选择doc或者docx文件";
  124. if (openFileDialog.ShowDialog() == DialogResult.OK)
  125. {
  126. fileNames = openFileDialog.FileNames;
  127. // 先清空列表,再渲染列表
  128. listview_draw(fileNames);
  129. }
  130. }
  131. private void Form1_Load(object sender, EventArgs e)
  132. {
  133. }
  134. private void listview_draw(string[] fileNames)
  135. {
  136. listView1.Items.Clear();
  137. for (int i = 0; i < fileNames.Length; i++)
  138. {
  139. ListViewItem item = new ListViewItem();
  140. item.Text = fileNames[i];
  141. item.SubItems.Add("等待中");
  142. item.SubItems.Add("");
  143. listView1.Items.Add(item);
  144. }
  145. }
  146. private void execStatus(int index)
  147. {
  148. var item = listView1.Items[index];
  149. item.SubItems[1].Text = "执行中";
  150. }
  151. private void listView1_SelectedIndexChanged(object sender, EventArgs e)
  152. {
  153. }
  154. private void button3_Click(object sender, EventArgs e)
  155. {
  156. FolderBrowserDialog folderBrowserDialog = new FolderBrowserDialog();
  157. if(folderBrowserDialog.ShowDialog() == DialogResult.OK)
  158. {
  159. AimFolder = folderBrowserDialog.SelectedPath;
  160. textBox1.Text = AimFolder;
  161. Analyzer.SetPath(AimFolder);
  162. }
  163. }
  164. private void textBox1_TextChanged(object sender, EventArgs e)
  165. {
  166. }
  167. }
  168. }