下面来看看程序的具体实现步骤。
首先是读取指定的目录并遍历其中的文件。这个功能很简单,我们只需要通过一个foreach循环遍历DirectoryInfo对象中的所有文件即可。在本例中,我们定义了一个文件夹选择控件,当用户点击Select按钮时弹出对话框用户指定程序要遍历的目录,将目录字符串存储在文本框中。点击Go按钮时开始遍历文件夹中的所有文件。程序将按行输出目录中Word文件名,不包含扩展名。
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.Windows.Forms;
9 using System.IO;
10
11 namespace ListFileName
12 {
13 public partial class ListFileName : Form
14 {
15 protected DirectoryInfo dirFolder = null;
16
17 public ListFileName()
18 {
19 InitializeComponent();
20 }
21
22 private void SelectPath()
23 {
24 if (folderBrowserDialog.ShowDialog() == DialogResult.OK)
25 {
26 this.tbPath.Text = folderBrowserDialog.SelectedPath;
27 }
28 }
29
30 // Double click the path textbox to open the folder browser dialog.
31 private void tbPath_DoubleClick(object sender, EventArgs e)
32 {
33 SelectPath();
34 }
35
36 // Open the folder browser dialog.
37 private void btSelect_Click(object sender, EventArgs e)
38 {
39 SelectPath();
40 }
41
42 // Start to run.
43 private void btGo_Click(object sender, EventArgs e)
44 {
45 this.btGo.Enabled = this.btSelect.Enabled = this.tbPath.Enabled = false;
46 this.tbOutput.Text = "";
47 string folderPath = this.tbPath.Text.Trim();
48
49 if (folderPath.Length == 0 || !Directory.Exists(folderPath))
50 {
51 MessageBox.Show("Please select a valid folder.", "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
52 return;
53 }
54 dirFolder = new DirectoryInfo(folderPath);
55
56 //Traversing file
57 foreach (FileInfo file in dirFolder.GetFiles())
58 {
59 // if the file type is word and not the word temp file.
60 if (file.Extension.IndexOf("doc") > 0 && file.Name.IndexOf("~$") < 0)
61 {
62 this.tbOutput.Text += file.Name.Substring(0, file.Name.LastIndexOf('.')).Trim() += "\r\n";
63 }
64 }
65
66 if (this.tbOutput.Text.Length > 2)
67 {
68 this.tbOutput.Text = this.tbOutput.Text.Substring(0, this.tbOutput.Text.Length - 2);
69 }
70
71 this.btGo.Enabled = this.btSelect.Enabled = this.tbPath.Enabled = false;
72 }
73 }
74 }
然后是增加读取Word文件的功能。这个需要使用COM组件,在工程中添加对Word COM组件的引用。
程序只选择读取Word文件的前三行,下面是具体的代码。2
3 protected Word.Application app = null;
4 protected Word.Document doc = null;
5
6 private string ReadTextFromWord(object fileName)
7 {
8 string sRst = string.Empty;
9 object isReadOnly = true;
10 object unknow = Type.Missing;
11 app.Visible = false;
12
13 if (app != null)
14 {
15
16 try
17 {
18 // Open a word document with read only mode.
19 doc = app.Documents.Open(ref fileName,
20 ref unknow, ref isReadOnly, ref unknow, ref unknow, ref unknow,
21 ref unknow, ref unknow, ref unknow, ref unknow, ref unknow,
22 ref unknow, ref unknow, ref unknow, ref unknow, ref unknow);
23
24 // Read the second paragraph text - Production's Engligh name.
25 sRst = (doc.Paragraphs.Count > 1 ? doc.Paragraphs[2].Range.Text.Trim() : "") + "\t";
26 // Read the third paragraph text - Production's Chinese name.
27 sRst += (doc.Paragraphs.Count > 2 ? doc.Paragraphs[3].Range.Text.Trim() : "") + "\t";
28 }
29 catch (Exception)
30 {
31 }
32 finally
33 {
34 // Close opened word document.
35 app.ActiveDocument.Close(ref unknow, ref unknow, ref unknow);
36 }
37 }
38
39 return sRst;
40 }
注意打开Word文档时可以以只读方式打开,这样打开的速度会稍微快一点,并且关闭的时候不会出现是否保存文件的对话框。任何情况下只要打开了Word文档,一定记住使用完后要将其关闭,并且在退出程序时也要将Word进程一并关掉。
2 private void ListFileName_FormClosed(object sender, FormClosedEventArgs e)
3 {
4 if (app != null)
5 {
6 object unknow = Type.Missing;
7 object saveChanges = Word.WdSaveOptions.wdDoNotSaveChanges;
8 app.Quit(ref saveChanges, ref unknow, ref unknow);
9 }
10 }
最后再来看看如何实现平滑进度条。这个需要使用到BackgroundWorker对象,该对象允许程序在多线程下执行,我们可以将界面UI和后台线程分开,这样当程序在执行一个时间较长的任务时不会导致主界面处于等待状态,用户仍然可以操作主界面上的元素,使程序看起来比较平滑。BackgroundWorker对象定义了一些属性和委托用来处理多线程间的信息同步,诸如后台线程在执行过程中如何通知主界面的线程更新进度条值等。
2
3 worker.WorkerReportsProgress = true;
4
5 worker.DoWork += new DoWorkEventHandler(worker_DoWork);
6 worker.ProgressChanged += new ProgressChangedEventHandler(worker_ProgressChanged);
7 worker.RunWorkerCompleted += new RunWorkerCompletedEventHandler(worker_RunWorkerCompleted);
在work_DoWork中调用具体执行的方法,worker_ProgressChanged方法用来实时更新进度条值,worker_RunWorkerCompleted方法用来处理当后台线程执行完操作后要处理的事情,如更新界面UI,将进度条的当前值更改为100%等。可以看看下面完整的程序代码:
本文转自Jaxu博客园博客,原文链接:http://www.cnblogs.com/jaxu/archive/2011/01/30/1947826.html,如需转载请自行联系原作者