NETCF开发之文件与目录(7)

1.1.1 确定目录是否存在

System.IO命名空间的Directory类包含一个叫做Exists的方法。无论指定的目录是否存在,Exists方法都返回值。该方法的声明如下:

<group id="_x0000_s1089" style="WIDTH: 414pt; HEIGHT: 23.4pt; mso-position-horizontal-relative: char; mso-position-vertical-relative: line" editas="canvas" coordorigin="2526,7642" coordsize="7200,408"><lock v:ext="edit" aspectratio="t"></lock><shapetype id="_x0000_t75" coordsize="21600,21600" o:spt="75" o:preferrelative="t" path="m@4@5l@4@11@9@11@9@5xe" filled="f" stroked="f"><stroke joinstyle="miter"></stroke><formulas><f eqn="if lineDrawn pixelLineWidth 0"></f><f eqn="sum @0 1 0"></f><f eqn="sum 0 0 @1"></f><f eqn="prod @2 1 2"></f><f eqn="prod @3 21600 pixelWidth"></f><f eqn="prod @3 21600 pixelHeight"></f><f eqn="sum @0 0 1"></f><f eqn="prod @6 1 2"></f><f eqn="prod @7 21600 pixelWidth"></f><f eqn="sum @8 21600 0"></f><f eqn="prod @7 21600 pixelHeight"></f><f eqn="sum @10 21600 0"></f></formulas><path o:extrusionok="f" gradientshapeok="t" o:connecttype="rect"></path><lock v:ext="edit" aspectratio="t"></lock></shapetype><shape id="_x0000_s1090" style="LEFT: 2526px; WIDTH: 7200px; POSITION: absolute; TOP: 7642px; HEIGHT: 408px" o:preferrelative="f" type="#_x0000_t75"><font size="3"><font face="Times New Roman"><fill o:detectmouseclick="t"></fill><path o:extrusionok="t" o:connecttype="none"></path><lock v:ext="edit" text="t"></lock></font></font></shape><rect id="_x0000_s1091" style="LEFT: 2526px; WIDTH: 7200px; POSITION: absolute; TOP: 7642px; HEIGHT: 408px" fillcolor="#eaeaea" strokecolor="#eaeaea"><textbox style="mso-next-textbox: #_x0000_s1091"><table cellspacing="0" cellpadding="0" width="100%"><tbody><tr> <td style="BORDER-LEFT-COLOR: #d4d0c8; BORDER-BOTTOM-COLOR: #d4d0c8; BORDER-TOP-COLOR: #d4d0c8; BACKGROUND-COLOR: transparent; BORDER-RIGHT-COLOR: #d4d0c8"> <div> <p class="MsoNormal" style="MARGIN: 0cm 0cm 0pt; LINE-HEIGHT: 14pt"><span lang="EN-US" style="FONT-SIZE: 9pt"><font face="Times New Roman">Public Shared Function Exists(ByVal path As String) As Boolean<p></p></font></span></p> <p class="MsoNormal" style="MARGIN: 0cm 0cm 0pt; LINE-HEIGHT: 14pt"><span lang="EN-US" style="FONT-SIZE: 9pt"><p><font face="Times New Roman"></font></p></span></p> </div> </td> </tr></tbody></table></textbox></rect><wrap type="none"></wrap><anchorlock></anchorlock></group>

Exists方法返回一个Boolean值。如果目录存在,返回True,否则返回False。在PocketPC应用程序中可以使用以下这行代码调用Exists方法:

<group id="_x0000_s1086" style="WIDTH: 414pt; HEIGHT: 23.4pt; mso-position-horizontal-relative: char; mso-position-vertical-relative: line" editas="canvas" coordorigin="2526,7642" coordsize="7200,408"><lock v:ext="edit" aspectratio="t"></lock><shape id="_x0000_s1087" style="LEFT: 2526px; WIDTH: 7200px; POSITION: absolute; TOP: 7642px; HEIGHT: 408px" o:preferrelative="f" type="#_x0000_t75"><font size="3"><font face="Times New Roman"><fill o:detectmouseclick="t"></fill><path o:extrusionok="t" o:connecttype="none"></path><lock v:ext="edit" text="t"></lock></font></font></shape><rect id="_x0000_s1088" style="LEFT: 2526px; WIDTH: 7200px; POSITION: absolute; TOP: 7642px; HEIGHT: 408px" fillcolor="#eaeaea" strokecolor="#eaeaea"><textbox style="mso-next-textbox: #_x0000_s1088"><table cellspacing="0" cellpadding="0" width="100%"><tbody><tr> <td style="BORDER-LEFT-COLOR: #d4d0c8; BORDER-BOTTOM-COLOR: #d4d0c8; BORDER-TOP-COLOR: #d4d0c8; BACKGROUND-COLOR: transparent; BORDER-RIGHT-COLOR: #d4d0c8"> <div> <p class="MsoNormal" style="MARGIN: 0cm 0cm 0pt; LINE-HEIGHT: 14pt"><span lang="EN-US" style="FONT-SIZE: 9pt"><font face="Times New Roman">MsgBox(System.IO.Directory.Exists("\Program Files\My Directory"))<p></p></font></span></p> <p class="MsoNormal" style="MARGIN: 0cm 0cm 0pt; LINE-HEIGHT: 14pt"><span lang="EN-US" style="FONT-SIZE: 9pt"><p><font face="Times New Roman"></font></p></span></p> </div> </td> </tr></tbody></table></textbox></rect><wrap type="none"></wrap><anchorlock></anchorlock></group>

1.1.2 创建目录

Pocket PC应用程序本身可以创建目录。Pocket PC应用程序能够利用目录来组织和存储由应用程序收集或使用的数据。因此Pocket PC应用程序应该提供允许用户组织和存储数据的功能,让用户自己指定文件夹来更好地满足需求。

使用Directory类的CreateDirectory方法创建目录和子目录。该方法的声明为:

<group id="_x0000_s1083" style="WIDTH: 414pt; HEIGHT: 23.4pt; mso-position-horizontal-relative: char; mso-position-vertical-relative: line" editas="canvas" coordorigin="2526,7642" coordsize="7200,408"><lock v:ext="edit" aspectratio="t"></lock><shape id="_x0000_s1084" style="LEFT: 2526px; WIDTH: 7200px; POSITION: absolute; TOP: 7642px; HEIGHT: 408px" o:preferrelative="f" type="#_x0000_t75"><font size="3"><font face="Times New Roman"><fill o:detectmouseclick="t"></fill><path o:extrusionok="t" o:connecttype="none"></path><lock v:ext="edit" text="t"></lock></font></font></shape><rect id="_x0000_s1085" style="LEFT: 2526px; WIDTH: 7200px; POSITION: absolute; TOP: 7642px; HEIGHT: 408px" fillcolor="#eaeaea" strokecolor="#eaeaea"><textbox style="mso-next-textbox: #_x0000_s1085"><table cellspacing="0" cellpadding="0" width="100%"><tbody><tr> <td style="BORDER-LEFT-COLOR: #d4d0c8; BORDER-BOTTOM-COLOR: #d4d0c8; BORDER-TOP-COLOR: #d4d0c8; BACKGROUND-COLOR: transparent; BORDER-RIGHT-COLOR: #d4d0c8"> <div> <p class="MsoNormal" style="MARGIN: 0cm 0cm 0pt; LINE-HEIGHT: 14pt"><span lang="EN-US" style="FONT-SIZE: 9pt"><font face="Times New Roman">Public Shared Function CreateDirectory(ByVal path As String) As DirectoryInfo<p></p></font></span></p> </div> </td> </tr></tbody></table></textbox></rect><wrap type="none"></wrap><anchorlock></anchorlock></group>

调用CreateDirectory方法时,必须为该方法提供所创建目录的完整路径。调用CreateDirectory方法的示例如下:

<group id="_x0000_s1080" style="WIDTH: 414pt; HEIGHT: 23.4pt; mso-position-horizontal-relative: char; mso-position-vertical-relative: line" editas="canvas" coordorigin="2526,7642" coordsize="7200,408"><lock v:ext="edit" aspectratio="t"></lock><shape id="_x0000_s1081" style="LEFT: 2526px; WIDTH: 7200px; POSITION: absolute; TOP: 7642px; HEIGHT: 408px" o:preferrelative="f" type="#_x0000_t75"><font size="3"><font face="Times New Roman"><fill o:detectmouseclick="t"></fill><path o:extrusionok="t" o:connecttype="none"></path><lock v:ext="edit" text="t"></lock></font></font></shape><rect id="_x0000_s1082" style="LEFT: 2526px; WIDTH: 7200px; POSITION: absolute; TOP: 7642px; HEIGHT: 408px" fillcolor="#eaeaea" strokecolor="#eaeaea"><textbox style="mso-next-textbox: #_x0000_s1082"><table cellspacing="0" cellpadding="0" width="100%"><tbody><tr> <td style="BORDER-LEFT-COLOR: #d4d0c8; BORDER-BOTTOM-COLOR: #d4d0c8; BORDER-TOP-COLOR: #d4d0c8; BACKGROUND-COLOR: transparent; BORDER-RIGHT-COLOR: #d4d0c8"> <div> <p class="MsoNormal" style="MARGIN: 0cm 0cm 0pt; LINE-HEIGHT: 14pt"><span lang="EN-US" style="FONT-SIZE: 9pt"><font face="Times New Roman">System.IO.Directory.CreateDirectory("\Program Files\My Directory")<p></p></font></span></p> </div> </td> </tr></tbody></table></textbox></rect><wrap type="none"></wrap><anchorlock></anchorlock></group>

在上面这个示例里,将创建目录\Program Files\My Directory。与所有相关的IO操作一样,创建目录时可能产生运行时错误。需要在编程时添加错误处理的代码。错误处理代码如清单11-8所示。

清单11-8:错误处理代码

<group id="_x0000_s1077" style="WIDTH: 414pt; HEIGHT: 176.8pt; mso-position-horizontal-relative: char; mso-position-vertical-relative: line" editas="canvas" coordorigin="2526,7642" coordsize="7200,3083"><lock v:ext="edit" aspectratio="t"></lock><shape id="_x0000_s1078" style="LEFT: 2526px; WIDTH: 7200px; POSITION: absolute; TOP: 7642px; HEIGHT: 3083px" o:preferrelative="f" type="#_x0000_t75"><font size="3"><fill o:detectmouseclick="t"></fill><path o:extrusionok="t" o:connecttype="none"></path><lock v:ext="edit" text="t"></lock></font></shape><rect id="_x0000_s1079" style="LEFT: 2526px; WIDTH: 7200px; POSITION: absolute; TOP: 7642px; HEIGHT: 3083px" fillcolor="#eaeaea" strokecolor="#eaeaea"><textbox style="mso-next-textbox: #_x0000_s1079"><table cellspacing="0" cellpadding="0" width="100%"><tbody><tr> <td style="BORDER-LEFT-COLOR: #d4d0c8; BORDER-BOTTOM-COLOR: #d4d0c8; BORDER-TOP-COLOR: #d4d0c8; BACKGROUND-COLOR: transparent; BORDER-RIGHT-COLOR: #d4d0c8"> <div> <p class="MsoNormal" style="MARGIN: 0cm 0cm 0pt; LINE-HEIGHT: 14pt"><span lang="EN-US" style="FONT-SIZE: 9pt"><font face="Times New Roman">……<p></p></font></span></p> <p class="MsoNormal" style="MARGIN: 0cm 0cm 0pt; TEXT-INDENT: 9pt; LINE-HEIGHT: 14pt; mso-char-indent-count: 1.0"><span lang="EN-US" style="FONT-SIZE: 9pt"><font face="Times New Roman">' </font></span><span style="FONT-SIZE: 9pt; FONT-FAMILY: 宋体; mso-ascii-font-family: 'Times New Roman'; mso-hansi-font-family: 'Times New Roman'">创建目录</span><span lang="EN-US" style="FONT-SIZE: 9pt"><p></p></span></p> <p class="MsoNormal" style="MARGIN: 0cm 0cm 0pt; LINE-HEIGHT: 14pt"><span lang="EN-US" style="FONT-SIZE: 9pt"><font face="Times New Roman"><span style="mso-spacerun: yes"> </span>Try<p></p></font></span></p> <p class="MsoNormal" style="MARGIN: 0cm 0cm 0pt; LINE-HEIGHT: 14pt"><span lang="EN-US" style="FONT-SIZE: 9pt"><font face="Times New Roman"><span style="mso-spacerun: yes"> </span>System.IO.Directory.CreateDirectory("\Program Files\My Directory")<p></p></font></span></p> <p class="MsoNormal" style="MARGIN: 0cm 0cm 0pt; LINE-HEIGHT: 14pt"><span lang="EN-US" style="FONT-SIZE: 9pt"><font face="Times New Roman"><span style="mso-spacerun: yes"> </span>Msgbox("The directory was created.")<p></p></font></span></p> <p class="MsoNormal" style="MARGIN: 0cm 0cm 0pt; LINE-HEIGHT: 14pt"><span lang="EN-US" style="FONT-SIZE: 9pt"><p><font face="Times New Roman"></font></p></span></p> <p class="MsoNormal" style="MARGIN: 0cm 0cm 0pt; TEXT-INDENT: 9pt; LINE-HEIGHT: 14pt; mso-char-indent-count: 1.0"><span lang="EN-US" style="FONT-SIZE: 9pt"><font face="Times New Roman">' </font></span><span style="FONT-SIZE: 9pt; FONT-FAMILY: 宋体; mso-ascii-font-family: 'Times New Roman'; mso-hansi-font-family: 'Times New Roman'">例外发生</span><span lang="EN-US" style="FONT-SIZE: 9pt"><p></p></span></p> <p class="MsoNormal" style="MARGIN: 0cm 0cm 0pt; LINE-HEIGHT: 14pt"><span lang="EN-US" style="FONT-SIZE: 9pt"><font face="Times New Roman"><span style="mso-spacerun: yes"> </span>Catch ex As Exception<p></p></font></span></p> <p class="MsoNormal" style="MARGIN: 0cm 0cm 0pt; LINE-HEIGHT: 14pt"><span lang="EN-US" style="FONT-SIZE: 9pt"><font face="Times New Roman"><span style="mso-spacerun: yes"> </span>MsgBox("</font></span><span style="FONT-SIZE: 9pt; FONT-FAMILY: 宋体; mso-ascii-font-family: 'Times New Roman'; mso-hansi-font-family: 'Times New Roman'">下列错误发生:</span><span lang="EN-US" style="FONT-SIZE: 9pt"><font face="Times New Roman">" &amp; ex.Message)<p></p></font></span></p> <p class="MsoNormal" style="MARGIN: 0cm 0cm 0pt; LINE-HEIGHT: 14pt"><span lang="EN-US" style="FONT-SIZE: 9pt"><font face="Times New Roman">End Try<p></p></font></span></p> <p class="MsoNormal" style="MARGIN: 0cm 0cm 0pt; LINE-HEIGHT: 14pt"><span lang="EN-US" style="FONT-SIZE: 9pt"><font face="Times New Roman">……<p></p></font></span></p> </div> </td> </tr></tbody></table></textbox></rect><wrap type="none"></wrap><anchorlock></anchorlock></group>

1.1.3 移动目录

使用Directory类的Move方法可以把目录和目录下的内容一并移到新的目录。该方法的声明为:

<group id="_x0000_s1074" style="WIDTH: 414pt; HEIGHT: 23.4pt; mso-position-horizontal-relative: char; mso-position-vertical-relative: line" editas="canvas" coordorigin="2526,7642" coordsize="7200,408"><lock v:ext="edit" aspectratio="t"></lock><shape id="_x0000_s1075" style="LEFT: 2526px; WIDTH: 7200px; POSITION: absolute; TOP: 7642px; HEIGHT: 408px" o:preferrelative="f" type="#_x0000_t75"><font size="3"><font face="Times New Roman"><fill o:detectmouseclick="t"></fill><path o:extrusionok="t" o:connecttype="none"></path><lock v:ext="edit" text="t"></lock></font></font></shape><rect id="_x0000_s1076" style="LEFT: 2526px; WIDTH: 7200px; POSITION: absolute; TOP: 7642px; HEIGHT: 408px" fillcolor="#eaeaea" strokecolor="#eaeaea"><textbox style="mso-next-textbox: #_x0000_s1076"><table cellspacing="0" cellpadding="0" width="100%"><tbody><tr> <td style="BORDER-LEFT-COLOR: #d4d0c8; BORDER-BOTTOM-COLOR: #d4d0c8; BORDER-TOP-COLOR: #d4d0c8; BACKGROUND-COLOR: transparent; BORDER-RIGHT-COLOR: #d4d0c8"> <div> <p class="MsoNormal" style="MARGIN: 0cm 0cm 0pt; LINE-HEIGHT: 14pt"><span lang="EN-US" style="FONT-SIZE: 9pt"><font face="Times New Roman">Public Shared Sub Move(ByVal sourceDirName As String, ByVal destDirName As String)<p></p></font></span></p> </div> </td> </tr></tbody></table></textbox></rect><wrap type="none"></wrap><anchorlock></anchorlock></group>

源目录的路径由参数sourceDirName指定,destDirName标识目的目录的路径。调用Move方法的示例如下:

<group id="_x0000_s1071" style="WIDTH: 414pt; HEIGHT: 23.4pt; mso-position-horizontal-relative: char; mso-position-vertical-relative: line" editas="canvas" coordorigin="2526,7642" coordsize="7200,408"><lock v:ext="edit" aspectratio="t"></lock><shape id="_x0000_s1072" style="LEFT: 2526px; WIDTH: 7200px; POSITION: absolute; TOP: 7642px; HEIGHT: 408px" o:preferrelative="f" type="#_x0000_t75"><font size="3"><font face="Times New Roman"><fill o:detectmouseclick="t"></fill><path o:extrusionok="t" o:connecttype="none"></path><lock v:ext="edit" text="t"></lock></font></font></shape><rect id="_x0000_s1073" style="LEFT: 2526px; WIDTH: 7200px; POSITION: absolute; TOP: 7642px; HEIGHT: 408px" fillcolor="#eaeaea" strokecolor="#eaeaea"><textbox style="mso-next-textbox: #_x0000_s1073"><table cellspacing="0" cellpadding="0" width="100%"><tbody><tr> <td style="BORDER-LEFT-COLOR: #d4d0c8; BORDER-BOTTOM-COLOR: #d4d0c8; BORDER-TOP-COLOR: #d4d0c8; BACKGROUND-COLOR: transparent; BORDER-RIGHT-COLOR: #d4d0c8"> <div> <p class="MsoNormal" style="MARGIN: 0cm 0cm 0pt; LINE-HEIGHT: 14pt"><span lang="EN-US" style="FONT-SIZE: 9pt"><font face="Times New Roman">System.IO.Directory.Move("\Program Files\My Directory","\Program Files\New Directory")<p></p></font></span></p> </div> </td> </tr></tbody></table></textbox></rect><wrap type="none"></wrap><anchorlock></anchorlock></group>

如果源目录不存在或目的目录已经存在,Move方法将抛出一个例外。清单11-9演示如何从代码处理例外的示例。在调用Move方法之前,调用MoveDirectory方法例行检查源目录和目的目录。Move方法本身包括在Try/Catch结构之内,处理任何其它可能发生的例外。

清单11-9:移动目录时增加例外处理

<group id="_x0000_s1068" style="WIDTH: 414pt; HEIGHT: 624pt; mso-position-horizontal-relative: char; mso-position-vertical-relative: line" editas="canvas" coordorigin="2526,7642" coordsize="7200,10882"><lock v:ext="edit" aspectratio="t"></lock><shape id="_x0000_s1069" style="LEFT: 2526px; WIDTH: 7200px; POSITION: absolute; TOP: 7642px; HEIGHT: 10882px" o:preferrelative="f" type="#_x0000_t75"><font size="3"><fill o:detectmouseclick="t"></fill><path o:extrusionok="t" o:connecttype="none"></path><lock v:ext="edit" text="t"></lock></font></shape><rect id="_x0000_s1070" style="LEFT: 2526px; WIDTH: 7200px; POSITION: absolute; TOP: 7642px; HEIGHT: 10882px" fillcolor="#eaeaea" strokecolor="#eaeaea"><textbox style="mso-next-textbox: #_x0000_s1070"><table cellspacing="0" cellpadding="0" width="100%"><tbody><tr> <td style="BORDER-LEFT-COLOR: #d4d0c8; BORDER-BOTTOM-COLOR: #d4d0c8; BORDER-TOP-COLOR: #d4d0c8; BACKGROUND-COLOR: transparent; BORDER-RIGHT-COLOR: #d4d0c8"> <div> <p class="MsoNormal" style="MARGIN: 0cm 0cm 0pt; TEXT-INDENT: 21pt"><span lang="EN-US" style="FONT-SIZE: 9pt"><font face="Times New Roman">' </font></span><span style="FONT-SIZE: 9pt; FONT-FAMILY: 宋体; mso-ascii-font-family: 'Times New Roman'; mso-hansi-font-family: 'Times New Roman'">声明返回状态结构体</span><span lang="EN-US" style="FONT-SIZE: 9pt"><p></p></span></p> <p class="MsoNormal" style="MARGIN: 0cm 0cm 0pt; TEXT-INDENT: 21pt"><span lang="EN-US" style="FONT-SIZE: 9pt"><font face="Times New Roman">Public Structure ReturnStatus<p></p></font></span></p> <p class="MsoNormal" style="MARGIN: 0cm 0cm 0pt 21pt; TEXT-INDENT: 21pt"><span lang="EN-US" style="FONT-SIZE: 9pt"><font face="Times New Roman">Dim Success As Boolean<p></p></font></span></p> <p class="MsoNormal" style="MARGIN: 0cm 0cm 0pt 21pt; TEXT-INDENT: 21pt"><span lang="EN-US" style="FONT-SIZE: 9pt"><font face="Times New Roman">Dim Message As String<p></p></font></span></p> <p class="MsoNormal" style="MARGIN: 0cm 0cm 0pt; TEXT-INDENT: 21pt"><span lang="EN-US" style="FONT-SIZE: 9pt"><font face="Times New Roman">End Structure<p></p></font></span></p> <p class="MsoNormal" style="MARGIN: 0cm 0cm 0pt"><span lang="EN-US" style="FONT-SIZE: 9pt"><p><font face="Times New Roman"></font></p></span></p> <p class="MsoNormal" style="MARGIN: 0cm 0cm 0pt; TEXT-INDENT: 21pt"><span lang="EN-US" style="FONT-SIZE: 9pt"><font face="Times New Roman">Function MoveDirectory(ByVal strSource As String, ByVal strTarget As String) As ReturnStatus<p></p></font></span></p> <p class="MsoNormal" style="MARGIN: 0cm 0cm 0pt 21pt; TEXT-INDENT: 21pt"><span lang="EN-US" style="FONT-SIZE: 9pt"><font face="Times New Roman">Dim MyReturn As ReturnStatus<p></p></font></span></p> <p class="MsoNormal" style="MARGIN: 0cm 0cm 0pt"><span lang="EN-US" style="FONT-SIZE: 9pt"><p><font face="Times New Roman"></font></p></span></p> <p class="MsoNormal" style="MARGIN: 0cm 0cm 0pt 21pt; TEXT-INDENT: 21pt"><span lang="EN-US" style="FONT-SIZE: 9pt"><font face="Times New Roman">If Not (System.IO.Directory.Exists(strSource)) Then<p></p></font></span></p> <p class="MsoNormal" style="MARGIN: 0cm 0cm 0pt 42pt; TEXT-INDENT: 21pt"><span lang="EN-US" style="FONT-SIZE: 9pt"><font face="Times New Roman">MyReturn.Success = False<p></p></font></span></p> <p class="MsoNormal" style="MARGIN: 0cm 0cm 0pt 42pt; TEXT-INDENT: 21pt"><span lang="EN-US" style="FONT-SIZE: 9pt"><font face="Times New Roman">MyReturn.Message = "</font></span><span style="FONT-SIZE: 9pt; FONT-FAMILY: 宋体; mso-ascii-font-family: 'Times New Roman'; mso-hansi-font-family: 'Times New Roman'">源目录不存在</span><span lang="EN-US" style="FONT-SIZE: 9pt"><font face="Times New Roman">"<p></p></font></span></p> <p class="MsoNormal" style="MARGIN: 0cm 0cm 0pt 42pt; TEXT-INDENT: 21pt"><span lang="EN-US" style="FONT-SIZE: 9pt"><font face="Times New Roman">Return MyReturn<p></p></font></span></p> <p class="MsoNormal" style="MARGIN: 0cm 0cm 0pt 42pt; TEXT-INDENT: 21pt"><span lang="EN-US" style="FONT-SIZE: 9pt"><font face="Times New Roman">Exit Function<p></p></font></span></p> <p class="MsoNormal" style="MARGIN: 0cm 0cm 0pt 21pt; TEXT-INDENT: 21pt"><span lang="EN-US" style="FONT-SIZE: 9pt"><font face="Times New Roman">End If<p></p></font></span></p> <p class="MsoNormal" style="MARGIN: 0cm 0cm 0pt"><span lang="EN-US" style="FONT-SIZE: 9pt"><p><font face="Times New Roman"></font></p></span></p> <p class="MsoNormal" style="MARGIN: 0cm 0cm 0pt 21pt; TEXT-INDENT: 21pt"><span lang="EN-US" style="FONT-SIZE: 9pt"><font face="Times New Roman">If System.IO.Directory.Exists(strTarget) Then<p></p></font></span></p> <p class="MsoNormal" style="MARGIN: 0cm 0cm 0pt"><span lang="EN-US" style="FONT-SIZE: 9pt"><font face="Times New Roman"><span style="mso-spacerun: yes"> </span>MyReturn.Success = False<p></p></font></span></p> <p class="MsoNormal" style="MARGIN: 0cm 0cm 0pt"><span lang="EN-US" style="FONT-SIZE: 9pt"><font face="Times New Roman"><span style="mso-spacerun: yes"> </span>MyReturn.Message = "</font></span><span style="FONT-SIZE: 9pt; FONT-FAMILY: 宋体; mso-ascii-font-family: 'Times New Roman'; mso-hansi-font-family: 'Times New Roman'">目标目录已存在</span><span lang="EN-US" style="FONT-SIZE: 9pt"><font face="Times New Roman">"<p></p></font></span></p> <p class="MsoNormal" style="MARGIN: 0cm 0cm 0pt"><span lang="EN-US" style="FONT-SIZE: 9pt"><font face="Times New Roman"><span style="mso-spacerun: yes"> </span>Return MyReturn<p></p></font></span></p> <p class="MsoNormal" style="MARGIN: 0cm 0cm 0pt"><span lang="EN-US" style="FONT-SIZE: 9pt"><font face="Times New Roman"><span style="mso-spacerun: yes"> </span><span style="mso-spacerun: yes"></span>Exit Function<p></p></font></span></p> <p class="MsoNormal" style="MARGIN: 0cm 0cm 0pt 21pt; TEXT-INDENT: 21pt"><span lang="EN-US" style="FONT-SIZE: 9pt"><font face="Times New Roman">End If<p></p></font></span></p> <p class="MsoNormal" style="MARGIN: 0cm 0cm 0pt"><span lang="EN-US" style="FONT-SIZE: 9pt"><p><font face="Times New Roman"></font></p></span></p> <p class="MsoNormal" style="MARGIN: 0cm 0cm 0pt 21pt; TEXT-INDENT: 21pt"><span lang="EN-US" style="FONT-SIZE: 9pt"><font face="Times New Roman">Try<p></p></font></span></p> <p class="MsoNormal" style="MARGIN: 0cm 0cm 0pt 42pt; TEXT-INDENT: 21pt"><span lang="EN-US" style="FONT-SIZE: 9pt"><font face="Times New Roman">System.IO.Directory.Move(strSource, strTarget)<p></p></font></span></p> <p class="MsoNormal" style="MARGIN: 0cm 0cm 0pt 42pt; TEXT-INDENT: 21pt"><span lang="EN-US" style="FONT-SIZE: 9pt"><font face="Times New Roman">MyReturn.Success = True<p></p></font></span></p> <p class="MsoNormal" style="MARGIN: 0cm 0cm 0pt 42pt; TEXT-INDENT: 21pt"><span lang="EN-US" style="FONT-SIZE: 9pt"><font face="Times New Roman">MyReturn.Message = strTarget<p></p></font></span></p> <p class="MsoNormal" style="MARGIN: 0cm 0cm 0pt 42pt; TEXT-INDENT: 21pt"><span lang="EN-US" style="FONT-SIZE: 9pt"><font face="Times New Roman">Return MyReturn<p></p></font></span></p> <p class="MsoNormal" style="MARGIN: 0cm 0cm 0pt 21pt; TEXT-INDENT: 21pt"><span lang="EN-US" style="FONT-SIZE: 9pt"><font face="Times New Roman">Catch ex As Exception<p></p></font></span></p> <p class="MsoNormal" style="MARGIN: 0cm 0cm 0pt 42pt; TEXT-INDENT: 21pt"><span lang="EN-US" style="FONT-SIZE: 9pt"><font face="Times New Roman">MyReturn.Success = False<p></p></font></span></p> <p class="MsoNormal" style="MARGIN: 0cm 0cm 0pt 42pt; TEXT-INDENT: 21pt"><span lang="EN-US" style="FONT-SIZE: 9pt"><font face="Times New Roman">MyReturn.Message = ex.Message<p></p></font></span></p> <p class="MsoNormal" style="MARGIN: 0cm 0cm 0pt 42pt; TEXT-INDENT: 21pt"><span lang="EN-US" style="FONT-SIZE: 9pt"><font face="Times New Roman">Return MyReturn<p></p></font></span></p> <p class="MsoNormal" style="MARGIN: 0cm 0cm 0pt 21pt; TEXT-INDENT: 21pt"><span lang="EN-US" style="FONT-SIZE: 9pt"><font face="Times New Roman">End Try<p></p></font></span></p> <p class="MsoNormal" style="MARGIN: 0cm 0cm 0pt; TEXT-INDENT: 21pt"><span lang="EN-US" style="FONT-SIZE: 9pt"><font face="Times New Roman">End Function<p></p></font></span></p> </div> </td> </tr></tbody></table></textbox></rect><wrap type="none"></wrap><anchorlock></anchorlock></group>

1.1.4 删除目录

你可以使用Directory类的Delete方法删除目录。在.NET Compact Framework里,该方法提供两个重载版本。第一个重载版本只删除目录。声明语法如下所示:

<group id="_x0000_s1065" style="WIDTH: 414pt; HEIGHT: 23.4pt; mso-position-horizontal-relative: char; mso-position-vertical-relative: line" editas="canvas" coordorigin="2526,7642" coordsize="7200,408"><lock v:ext="edit" aspectratio="t"></lock><shape id="_x0000_s1066" style="LEFT: 2526px; WIDTH: 7200px; POSITION: absolute; TOP: 7642px; HEIGHT: 408px" o:preferrelative="f" type="#_x0000_t75"><font size="3"><font face="Times New Roman"><fill o:detectmouseclick="t"></fill><path o:extrusionok="t" o:connecttype="none"></path><lock v:ext="edit" text="t"></lock></font></font></shape><rect id="_x0000_s1067" style="LEFT: 2526px; WIDTH: 7200px; POSITION: absolute; TOP: 7642px; HEIGHT: 408px" fillcolor="#eaeaea" strokecolor="#eaeaea"><textbox style="mso-next-textbox: #_x0000_s1067"><table cellspacing="0" cellpadding="0" width="100%"><tbody><tr> <td style="BORDER-LEFT-COLOR: #d4d0c8; BORDER-BOTTOM-COLOR: #d4d0c8; BORDER-TOP-COLOR: #d4d0c8; BACKGROUND-COLOR: transparent; BORDER-RIGHT-COLOR: #d4d0c8"> <div> <p class="MsoNormal" style="MARGIN: 0cm 0cm 0pt; LINE-HEIGHT: 14pt"><span lang="EN-US" style="FONT-SIZE: 9pt"><font face="Times New Roman">Overloads Public Shared Sub Delete(ByVal path As String)<p></p></font></span></p> </div> </td> </tr></tbody></table></textbox></rect><wrap type="none"></wrap><anchorlock></anchorlock></group>

Delete方法的调用语法如下所示:

<group id="_x0000_s1062" style="WIDTH: 414pt; HEIGHT: 23.4pt; mso-position-horizontal-relative: char; mso-position-vertical-relative: line" editas="canvas" coordorigin="2526,7642" coordsize="7200,408"><lock v:ext="edit" aspectratio="t"></lock><shape id="_x0000_s1063" style="LEFT: 2526px; WIDTH: 7200px; POSITION: absolute; TOP: 7642px; HEIGHT: 408px" o:preferrelative="f" type="#_x0000_t75"><font size="3"><font face="Times New Roman"><fill o:detectmouseclick="t"></fill><path o:extrusionok="t" o:connecttype="none"></path><lock v:ext="edit" text="t"></lock></font></font></shape><rect id="_x0000_s1064" style="LEFT: 2526px; WIDTH: 7200px; POSITION: absolute; TOP: 7642px; HEIGHT: 408px" fillcolor="#eaeaea" strokecolor="#eaeaea"><textbox style="mso-next-textbox: #_x0000_s1064"><table cellspacing="0" cellpadding="0" width="100%"><tbody><tr> <td style="BORDER-LEFT-COLOR: #d4d0c8; BORDER-BOTTOM-COLOR: #d4d0c8; BORDER-TOP-COLOR: #d4d0c8; BACKGROUND-COLOR: transparent; BORDER-RIGHT-COLOR: #d4d0c8"> <div> <p class="MsoNormal" style="MARGIN: 0cm 0cm 0pt; LINE-HEIGHT: 14pt"><span lang="EN-US" style="FONT-SIZE: 9pt"><font face="Times New Roman">System.IO.Directory.Delete("\Program Files\My Directory")<p></p></font></span></p> </div> </td> </tr></tbody></table></textbox></rect><wrap type="none"></wrap><anchorlock></anchorlock></group>

Delete方法的第二个重载版本删除目录,包括目录下的所有子目录和文件。声明语法是:

<group id="_x0000_s1059" style="WIDTH: 414pt; HEIGHT: 23.4pt; mso-position-horizontal-relative: char; mso-position-vertical-relative: line" editas="canvas" coordorigin="2526,7642" coordsize="7200,408"><lock v:ext="edit" aspectratio="t"></lock><shape id="_x0000_s1060" style="LEFT: 2526px; WIDTH: 7200px; POSITION: absolute; TOP: 7642px; HEIGHT: 408px" o:preferrelative="f" type="#_x0000_t75"><font size="3"><font face="Times New Roman"><fill o:detectmouseclick="t"></fill><path o:extrusionok="t" o:connecttype="none"></path><lock v:ext="edit" text="t"></lock></font></font></shape><rect id="_x0000_s1061" style="LEFT: 2526px; WIDTH: 7200px; POSITION: absolute; TOP: 7642px; HEIGHT: 408px" fillcolor="#eaeaea" strokecolor="#eaeaea"><textbox style="mso-next-textbox: #_x0000_s1061"><table cellspacing="0" cellpadding="0" width="100%"><tbody><tr> <td style="BORDER-LEFT-COLOR: #d4d0c8; BORDER-BOTTOM-COLOR: #d4d0c8; BORDER-TOP-COLOR: #d4d0c8; BACKGROUND-COLOR: transparent; BORDER-RIGHT-COLOR: #d4d0c8"> <div> <p class="MsoNormal" style="MARGIN: 0cm 0cm 0pt; LINE-HEIGHT: 14pt"><span lang="EN-US" style="FONT-SIZE: 9pt"><font face="Times New Roman">Overloads Public Shared Sub Delete(ByVal path As String, ByVal recursive As Boolean)<p></p></font></span></p> </div> </td> </tr></tbody></table></textbox></rect><wrap type="none"></wrap><anchorlock></anchorlock></group>

设置参数recursiveTrue,则删除目录及其包括的所有子目录和文件。

1.1.5 获取目录下面的子目录

使用Directory 类的GetDirectories方法获得指定目录下面的子目录。该方法返回一个字符串数组,数组里的每个数据项对应一个单独的子目录名。

GetDirectories方法提供两个重载版本。第一个重载版本返回指定目录里包含的所有子目录。该重载版本只接收一个参数,该参数指定当前目录的路径。声明语法如下所示:

<group id="_x0000_s1056" style="WIDTH: 414pt; HEIGHT: 23.4pt; mso-position-horizontal-relative: char; mso-position-vertical-relative: line" editas="canvas" coordorigin="2526,7642" coordsize="7200,408"><lock v:ext="edit" aspectratio="t"></lock><shape id="_x0000_s1057" style="LEFT: 2526px; WIDTH: 7200px; POSITION: absolute; TOP: 7642px; HEIGHT: 408px" o:preferrelative="f" type="#_x0000_t75"><font size="3"><font face="Times New Roman"><fill o:detectmouseclick="t"></fill><path o:extrusionok="t" o:connecttype="none"></path><lock v:ext="edit" text="t"></lock></font></font></shape><rect id="_x0000_s1058" style="LEFT: 2526px; WIDTH: 7200px; POSITION: absolute; TOP: 7642px; HEIGHT: 408px" fillcolor="#eaeaea" strokecolor="#eaeaea"><textbox style="mso-next-textbox: #_x0000_s1058"><table cellspacing="0" cellpadding="0" width="100%"><tbody><tr> <td style="BORDER-LEFT-COLOR: #d4d0c8; BORDER-BOTTOM-COLOR: #d4d0c8; BORDER-TOP-COLOR: #d4d0c8; BACKGROUND-COLOR: transparent; BORDER-RIGHT-COLOR: #d4d0c8"> <div> <p class="MsoNormal" style="MARGIN: 0cm 0cm 0pt; LINE-HEIGHT: 14pt"><span lang="EN-US" style="FONT-SIZE: 9pt"><font face="Times New Roman">Overloads Public Shared Function GetDirectories(ByVal path As String) As String()<p></p></font></span></p> </div> </td> </tr></tbody></table></textbox></rect><wrap type="none"></wrap><anchorlock></anchorlock></group>

GetDirectories方法的第一个重载版本的调用方式如下所示

<group id="_x0000_s1053" style="WIDTH: 414pt; HEIGHT: 39pt; mso-position-horizontal-relative: char; mso-position-vertical-relative: line" editas="canvas" coordorigin="2526,7642" coordsize="7200,680"><lock v:ext="edit" aspectratio="t"></lock><shape id="_x0000_s1054" style="LEFT: 2526px; WIDTH: 7200px; POSITION: absolute; TOP: 7642px; HEIGHT: 680px" o:preferrelative="f" type="#_x0000_t75"><font size="3"><font face="Times New Roman"><fill o:detectmouseclick="t"></fill><path o:extrusionok="t" o:connecttype="none"></path><lock v:ext="edit" text="t"></lock></font></font></shape><rect id="_x0000_s1055" style="LEFT: 2526px; WIDTH: 7200px; POSITION: absolute; TOP: 7642px; HEIGHT: 680px" fillcolor="#eaeaea" strokecolor="#eaeaea"><textbox style="mso-next-textbox: #_x0000_s1055"><table cellspacing="0" cellpadding="0" width="100%"><tbody><tr> <td style="BORDER-LEFT-COLOR: #d4d0c8; BORDER-BOTTOM-COLOR: #d4d0c8; BORDER-TOP-COLOR: #d4d0c8; BACKGROUND-COLOR: transparent; BORDER-RIGHT-COLOR: #d4d0c8"> <div> <p class="MsoNormal" style="MARGIN: 0cm 0cm 0pt; LINE-HEIGHT: 14pt"><span lang="EN-US" style="FONT-SIZE: 9pt"><font face="Times New Roman">Dim strDirectories() as String<p></p></font></span></p> <p class="MsoNormal" style="MARGIN: 0cm 0cm 0pt; LINE-HEIGHT: 14pt"><span lang="EN-US" style="FONT-SIZE: 9pt"><font face="Times New Roman">strDirectories = System.IO.Directory.GetDirectories("\Program Files\My Directory")<p></p></font></span></p> </div> </td> </tr></tbody></table></textbox></rect><wrap type="none"></wrap><anchorlock></anchorlock></group>

strDirectories是一个String类型的数组。.NET Compact Framework里,如果\Program Files\My Directory下面不包括任何子目录的话将抛出例外。因此操作目录和文件时应该增加错误处理的代码。

GetDirectories方法的第二个重载版本只返回匹配查询条件的子目录,查询条件由该重载版本的第二个参数指定。声明语法如下所示:

<group id="_x0000_s1050" style="WIDTH: 414pt; HEIGHT: 39pt; mso-position-horizontal-relative: char; mso-position-vertical-relative: line" editas="canvas" coordorigin="2526,7642" coordsize="7200,680"><lock v:ext="edit" aspectratio="t"></lock><shape id="_x0000_s1051" style="LEFT: 2526px; WIDTH: 7200px; POSITION: absolute; TOP: 7642px; HEIGHT: 680px" o:preferrelative="f" type="#_x0000_t75"><font size="3"><font face="Times New Roman"><fill o:detectmouseclick="t"></fill><path o:extrusionok="t" o:connecttype="none"></path><lock v:ext="edit" text="t"></lock></font></font></shape><rect id="_x0000_s1052" style="LEFT: 2526px; WIDTH: 7200px; POSITION: absolute; TOP: 7642px; HEIGHT: 680px" fillcolor="#eaeaea" strokecolor="#eaeaea"><textbox style="mso-next-textbox: #_x0000_s1052"><table cellspacing="0" cellpadding="0" width="100%"><tbody><tr> <td style="BORDER-LEFT-COLOR: #d4d0c8; BORDER-BOTTOM-COLOR: #d4d0c8; BORDER-TOP-COLOR: #d4d0c8; BACKGROUND-COLOR: transparent; BORDER-RIGHT-COLOR: #d4d0c8"> <div> <p class="MsoNormal" style="MARGIN: 0cm 0cm 0pt; LINE-HEIGHT: 14pt"><span lang="EN-US" style="FONT-SIZE: 9pt"><font face="Times New Roman">Overloads Public Shared Function GetDirectories(ByVal path As String, ByVal searchPattern As String) As String()<p></p></font></span></p> </div> </td> </tr></tbody></table></textbox></rect><wrap type="none"></wrap><anchorlock></anchorlock></group>

GetDirectories方法的第二个重载版本的调用方式如下所示

<group id="_x0000_s1047" style="WIDTH: 414pt; HEIGHT: 39pt; mso-position-horizontal-relative: char; mso-position-vertical-relative: line" editas="canvas" coordorigin="2526,7642" coordsize="7200,680"><lock v:ext="edit" aspectratio="t"></lock><shape id="_x0000_s1048" style="LEFT: 2526px; WIDTH: 7200px; POSITION: absolute; TOP: 7642px; HEIGHT: 680px" o:preferrelative="f" type="#_x0000_t75"><font size="3"><font face="Times New Roman"><fill o:detectmouseclick="t"></fill><path o:extrusionok="t" o:connecttype="none"></path><lock v:ext="edit" text="t"></lock></font></font></shape><rect id="_x0000_s1049" style="LEFT: 2526px; WIDTH: 7200px; POSITION: absolute; TOP: 7642px; HEIGHT: 680px" fillcolor="#eaeaea" strokecolor="#eaeaea"><textbox style="mso-next-textbox: #_x0000_s1049"><table cellspacing="0" cellpadding="0" width="100%"><tbody><tr> <td style="BORDER-LEFT-COLOR: #d4d0c8; BORDER-BOTTOM-COLOR: #d4d0c8; BORDER-TOP-COLOR: #d4d0c8; BACKGROUND-COLOR: transparent; BORDER-RIGHT-COLOR: #d4d0c8"> <div> <p class="MsoNormal" style="MARGIN: 0cm 0cm 0pt; LINE-HEIGHT: 14pt"><span lang="EN-US" style="FONT-SIZE: 9pt"><font face="Times New Roman">Dim strDirectories() as String<p></p></font></span></p> <p class="MsoNormal" style="MARGIN: 0cm 0cm 0pt; LINE-HEIGHT: 14pt"><span lang="EN-US" style="FONT-SIZE: 9pt"><font face="Times New Roman">strDirectories = System.IO.Directory.GetDirectories("\Program Files\My Directory","tmp*")<p></p></font></span></p> </div> </td> </tr></tbody></table></textbox></rect><wrap type="none"></wrap><anchorlock></anchorlock></group>

在上面这个示例里,只返回以“tmp”为前缀的子目录。

前面已经介绍过GetDirectories方法返回一个字符串数组。数组的数据项对应子目录的名称,包括子目录的完整路径。通过循环该数组访问所有子目录,如清单11-10所示。

清单11-10:循环子目录数组

<group id="_x0000_s1044" style="WIDTH: 414pt; HEIGHT: 195pt; mso-position-horizontal-relative: char; mso-position-vertical-relative: line" editas="canvas" coordorigin="2526,7642" coordsize="7200,3400"><lock v:ext="edit" aspectratio="t"></lock><shape id="_x0000_s1045" style="LEFT: 2526px; WIDTH: 7200px; POSITION: absolute; TOP: 7642px; HEIGHT: 3400px" o:preferrelative="f" type="#_x0000_t75"><font size="3"><fill o:detectmouseclick="t"></fill><path o:extrusionok="t" o:connecttype="none"></path><lock v:ext="edit" text="t"></lock></font></shape><rect id="_x0000_s1046" style="LEFT: 2526px; WIDTH: 7200px; POSITION: absolute; TOP: 7642px; HEIGHT: 3400px" fillcolor="#eaeaea" strokecolor="#eaeaea"><textbox style="mso-next-textbox: #_x0000_s1046"><table cellspacing="0" cellpadding="0" width="100%"><tbody><tr> <td style="BORDER-LEFT-COLOR: #d4d0c8; BORDER-BOTTOM-COLOR: #d4d0c8; BORDER-TOP-COLOR: #d4d0c8; BACKGROUND-COLOR: transparent; BORDER-RIGHT-COLOR: #d4d0c8"> <div> <p class="MsoNormal" style="MARGIN: 0cm 0cm 0pt; LINE-HEIGHT: 14pt"><span lang="EN-US" style="FONT-SIZE: 9pt"><font face="Times New Roman">……<p></p></font></span></p> <p class="MsoNormal" style="MARGIN: 0cm 0cm 0pt; TEXT-INDENT: 21pt; LINE-HEIGHT: 14pt"><span lang="EN-US" style="FONT-SIZE: 9pt"><font face="Times New Roman">Dim strDirectories() As String<p></p></font></span></p> <p class="MsoNormal" style="MARGIN: 0cm 0cm 0pt; TEXT-INDENT: 21pt; LINE-HEIGHT: 14pt"><span lang="EN-US" style="FONT-SIZE: 9pt"><font face="Times New Roman">Dim strDirectory As String<p></p></font></span></p> <p class="MsoNormal" style="MARGIN: 0cm 0cm 0pt; LINE-HEIGHT: 14pt"><span lang="EN-US" style="FONT-SIZE: 9pt"><p><font face="Times New Roman"></font></p></span></p> <p class="MsoNormal" style="MARGIN: 0cm 0cm 0pt; TEXT-INDENT: 21pt; LINE-HEIGHT: 14pt"><span lang="EN-US" style="FONT-SIZE: 9pt"><font face="Times New Roman">' </font></span><span style="FONT-SIZE: 9pt; FONT-FAMILY: 宋体; mso-ascii-font-family: 'Times New Roman'; mso-hansi-font-family: 'Times New Roman'">返回一个已排序的目录列表</span><span lang="EN-US" style="FONT-SIZE: 9pt"><p></p></span></p> <p class="MsoNormal" style="MARGIN: 0cm 0cm 0pt; TEXT-INDENT: 21pt; LINE-HEIGHT: 14pt"><span lang="EN-US" style="FONT-SIZE: 9pt"><font face="Times New Roman">strDirectories = System.IO.Directory.GetDirectories("\Program Files\My Directory")<p></p></font></span></p> <p class="MsoNormal" style="MARGIN: 0cm 0cm 0pt; LINE-HEIGHT: 14pt"><span lang="EN-US" style="FONT-SIZE: 9pt"><p><font face="Times New Roman"></font></p></span></p> <p class="MsoNormal" style="MARGIN: 0cm 0cm 0pt; TEXT-INDENT: 21pt; LINE-HEIGHT: 14pt"><span lang="EN-US" style="FONT-SIZE: 9pt"><font face="Times New Roman">' </font></span><span style="FONT-SIZE: 9pt; FONT-FAMILY: 宋体; mso-ascii-font-family: 'Times New Roman'; mso-hansi-font-family: 'Times New Roman'">显示目录</span><span lang="EN-US" style="FONT-SIZE: 9pt"><p></p></span></p> <p class="MsoNormal" style="MARGIN: 0cm 0cm 0pt; TEXT-INDENT: 21pt; LINE-HEIGHT: 14pt"><span lang="EN-US" style="FONT-SIZE: 9pt"><font face="Times New Roman">For Each strDirectory In strDirectories<p></p></font></span></p> <p class="MsoNormal" style="MARGIN: 0cm 0cm 0pt 21pt; TEXT-INDENT: 21pt; LINE-HEIGHT: 14pt"><span lang="EN-US" style="FONT-SIZE: 9pt"><font face="Times New Roman">Msgbox(strDirectory)<p></p></font></span></p> <p class="MsoNormal" style="MARGIN: 0cm 0cm 0pt; TEXT-INDENT: 21pt; LINE-HEIGHT: 14pt"><span lang="EN-US" style="FONT-SIZE: 9pt"><font face="Times New Roman">Next<p></p></font></span></p> <p class="MsoNormal" style="MARGIN: 0cm 0cm 0pt; LINE-HEIGHT: 14pt"><span lang="EN-US" style="FONT-SIZE: 9pt"><font face="Times New Roman">……<p></p></font></span></p> </div> </td> </tr></tbody></table></textbox></rect><wrap type="none"></wrap><anchorlock></anchorlock></group>

1.1.6 获取目录下面的文件

使用Directory类的GetFiles方法,可以获得指定目录下面的文件。与GetDirectories方法一样,GetFiles方法返回一个字符串数组,数组里的每个数据项对应一个单独的文件名。

GetFiles方法提供两个重载版本。第一个重载版本返回指定目录下面的所有文件,声明语法如下所示:

<group id="_x0000_s1041" style="WIDTH: 414pt; HEIGHT: 23.4pt; mso-position-horizontal-relative: char; mso-position-vertical-relative: line" editas="canvas" coordorigin="2526,7642" coordsize="7200,408"><lock v:ext="edit" aspectratio="t"></lock><shape id="_x0000_s1042" style="LEFT: 2526px; WIDTH: 7200px; POSITION: absolute; TOP: 7642px; HEIGHT: 408px" o:preferrelative="f" type="#_x0000_t75"><font size="3"><font face="Times New Roman"><fill o:detectmouseclick="t"></fill><path o:extrusionok="t" o:connecttype="none"></path><lock v:ext="edit" text="t"></lock></font></font></shape><rect id="_x0000_s1043" style="LEFT: 2526px; WIDTH: 7200px; POSITION: absolute; TOP: 7642px; HEIGHT: 408px" fillcolor="#eaeaea" strokecolor="#eaeaea"><textbox style="mso-next-textbox: #_x0000_s1043"><table cellspacing="0" cellpadding="0" width="100%"><tbody><tr> <td style="BORDER-LEFT-COLOR: #d4d0c8; BORDER-BOTTOM-COLOR: #d4d0c8; BORDER-TOP-COLOR: #d4d0c8; BACKGROUND-COLOR: transparent; BORDER-RIGHT-COLOR: #d4d0c8"> <div> <p class="MsoNormal" style="MARGIN: 0cm 0cm 0pt; LINE-HEIGHT: 14pt"><span lang="EN-US" style="FONT-SIZE: 9pt"><font face="Times New Roman">Overloads Public Shared Function GetFiles(ByVal path As String) As String()<p></p></font></span></p> </div> </td> </tr></tbody></table></textbox></rect><wrap type="none"></wrap><anchorlock></anchorlock></group>

GetFiles方法的第一个重载版本的调用方式如下所示

<group id="_x0000_s1038" style="WIDTH: 414pt; HEIGHT: 39pt; mso-position-horizontal-relative: char; mso-position-vertical-relative: line" editas="canvas" coordorigin="2526,7642" coordsize="7200,680"><lock v:ext="edit" aspectratio="t"></lock><shape id="_x0000_s1039" style="LEFT: 2526px; WIDTH: 7200px; POSITION: absolute; TOP: 7642px; HEIGHT: 680px" o:preferrelative="f" type="#_x0000_t75"><font size="3"><font face="Times New Roman"><fill o:detectmouseclick="t"></fill><path o:extrusionok="t" o:connecttype="none"></path><lock v:ext="edit" text="t"></lock></font></font></shape><rect id="_x0000_s1040" style="LEFT: 2526px; WIDTH: 7200px; POSITION: absolute; TOP: 7642px; HEIGHT: 680px" fillcolor="#eaeaea" strokecolor="#eaeaea"><textbox style="mso-next-textbox: #_x0000_s1040"><table cellspacing="0" cellpadding="0" width="100%"><tbody><tr> <td style="BORDER-LEFT-COLOR: #d4d0c8; BORDER-BOTTOM-COLOR: #d

你可能感兴趣的:(数据结构,编程,ext,F#)