Pocket PC应用程序通常会包含文件管理的功能。例如访问文本配置文件、XML文档或数据库以及拷贝、移动、删除和使用文件属性。下面就介绍如何在Pocket PC 环境里利用System.IO 命名空间的File类来实现文件管理。
与Directory和DirectoryInfo类类似,System.IO命名空间包括File和FileInfo两个类。这两个类之间的差别是File类提供静态方法,而FileInfo类提供实例方法。相比之下,File类的功能更强大更方便。表11-6列出在Pocket PC应用程序开发中常用的File类方法。
表11-6:File类用于管理文件的方法
方法 |
描述 |
Copy |
拷贝指定文件。 |
Delete |
删除指定文件。 |
Exists |
检查指定文件是否存在。 |
Move |
移动指定文件到新的目录。 |
System.IO命名空间的 File类提供一个Exists方法。顾名思义,该方法用于判断指定的文件是否存在。该方法的声明语法如下:
<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><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_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">Public Shared Function Exists(ByVal path As String) As Boolean<p></p></font></span></p> </div> </td> </tr></tbody></table></textbox></rect><wrap type="none"></wrap><anchorlock></anchorlock></group>
Exists方法返回一个布尔值。如果文件存在,返回True,否则返回False。以下代码演示调用File类Exists方法。
<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">MsgBox(System.IO.File.Exists("\Program Files\My Directory\working.tmp"))<p></p></font></span></p> </div> </td> </tr></tbody></table></textbox></rect><wrap type="none"></wrap><anchorlock></anchorlock></group>
使用Copy方法很容易实现文件的复制操作。该方法提供两个重载版本。假设目标文件不存在,第一个重载版本的声明语法如下所示:
<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 Sub Copy(ByVal sourceFileName As String, ByVal destFileName As String)<p></p></font></span></p> </div> </td> </tr></tbody></table></textbox></rect><wrap type="none"></wrap><anchorlock></anchorlock></group>
Copy方法的第一个重载版本的调用方式如下所示:
<group id="_x0000_s1053" 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_s1054" 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_s1055" style="LEFT: 2526px; WIDTH: 7200px; POSITION: absolute; TOP: 7642px; HEIGHT: 408px" 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">System.IO.File.Copy("\Program Files\My Directory\working.tmp", "\Program Files\My Directory\new.tmp")<p></p></font></span></p> </div> </td> </tr></tbody></table></textbox></rect><wrap type="none"></wrap><anchorlock></anchorlock></group>
使用Copy方法的第一个重载版本复制文件,应该使用System.IO.File.Exists方法确认目标文件不存在,否则将抛出一个IOException例外。
File类的第二个重载版本可以指定目标文件如果存在的话,是否覆盖目标文件。声明语法如下所示:
<group id="_x0000_s1050" style="WIDTH: 414pt; HEIGHT: 85.8pt; mso-position-horizontal-relative: char; mso-position-vertical-relative: line" editas="canvas" coordorigin="2526,7642" coordsize="7200,1496"><lock v:ext="edit" aspectratio="t"></lock><shape id="_x0000_s1051" style="LEFT: 2526px; WIDTH: 7200px; POSITION: absolute; TOP: 7642px; HEIGHT: 1496px" 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: 1496px" 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 Sub Copy(_<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>ByVal sourceFileName 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"><span style="mso-spacerun: yes"> </span>ByVal destFileName 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"><span style="mso-spacerun: yes"> </span>ByVal overwrite 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"><font face="Times New Roman">)<p></p></font></span></p> </div> </td> </tr></tbody></table></textbox></rect><wrap type="none"></wrap><anchorlock></anchorlock></group>
第三个参数overwrite接收一个布尔值。设置该参数为True,标识覆盖目标文件。调用方式如下所示:
<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">System.IO.File.Copy("\Program Files\My Directory\working.tmp", "\Program Files\My Directory\new.tmp", True)<p></p></font></span></p> </div> </td> </tr></tbody></table></textbox></rect><wrap type="none"></wrap><anchorlock></anchorlock></group>
除了能够复制文件外,还可以移动文件。File类提供一个Move方法。该方法的声明语法如下:
<group id="_x0000_s1044" 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_s1045" 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_s1046" style="LEFT: 2526px; WIDTH: 7200px; POSITION: absolute; TOP: 7642px; HEIGHT: 408px" 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">Public Shared Sub Move(ByVal sourceFileName As String, ByVal destFileName As String)<p></p></font></span></p> </div> </td> </tr></tbody></table></textbox></rect><wrap type="none"></wrap><anchorlock></anchorlock></group>
参数sourceFilename指定源文件的路径,参数desFileName指定目标文件的路径。该方法的调用方式如下所示:
<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">System.IO.File.Move("\Program Files\My Directory\working.tmp", "\Program Files\Archive\working.tmp")<p></p></font></span></p> </div> </td> </tr></tbody></table></textbox></rect><wrap type="none"></wrap><anchorlock></anchorlock></group>
使用Move方法时需要注意,如果目标文件存在,将抛出一个IOException例外。因此在移动文件之前,应该使用System.IO.File.Exists方法确认目标文件不存在。清单11-13演示如何正确的移动文件。
清单11-13:移动文件
<group id="_x0000_s1038" 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_s1039" style="LEFT: 2526px; WIDTH: 7200px; POSITION: absolute; TOP: 7642px; HEIGHT: 10882px" 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: 10882px" 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: #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 MoveFile(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; TEXT-INDENT: 21pt"><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.File.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; TEXT-INDENT: 21pt"><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.File.Exists(strTarget) 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; TEXT-INDENT: 21pt"><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.File.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>
删除文件可以使用File类的Delete方法来实现,该方法的声明语法如下:
<group id="_x0000_s1035" 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_s1036" 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_s1037" style="LEFT: 2526px; WIDTH: 7200px; POSITION: absolute; TOP: 7642px; HEIGHT: 408px" fillcolor="#eaeaea" strokecolor="#eaeaea"><textbox style="mso-next-textbox: #_x0000_s1037"><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 Delete(ByVal path As String)<p></p></font></span></p> </div> </td> </tr></tbody></table></textbox></rect><wrap type="none"></wrap><anchorlock></anchorlock></group>
删除文件时需要指定文件的完整路径。调用方式如下所示:
<group id="_x0000_s1032" 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_s1033" 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_s1034" style="LEFT: 2526px; WIDTH: 7200px; POSITION: absolute; TOP: 7642px; HEIGHT: 408px" fillcolor="#eaeaea" strokecolor="#eaeaea"><textbox style="mso-next-textbox: #_x0000_s1034"><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.File.Delete("\Program Files\My Directory\working.tmp")<p></p></font></span></p> </div> </td> </tr></tbody></table></textbox></rect><wrap type="none"></wrap><anchorlock></anchorlock></group>
通过FileInfo类的Attributes属性可以获得指定文件的当前属性。注意我说地是FileInfo类。在.NET Compact Framework里,File类没有提供这样的方法。
在该属性返回的值里包含存档、隐藏、只读和系统属性标记。以下代码演示如何访问文件属性:
<group id="_x0000_s1029" style="WIDTH: 414pt; HEIGHT: 70.2pt; mso-position-horizontal-relative: char; mso-position-vertical-relative: line" editas="canvas" coordorigin="2526,7642" coordsize="7200,1224"><lock v:ext="edit" aspectratio="t"></lock><shape id="_x0000_s1030" style="LEFT: 2526px; WIDTH: 7200px; POSITION: absolute; TOP: 7642px; HEIGHT: 1224px" 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_s1031" style="LEFT: 2526px; WIDTH: 7200px; POSITION: absolute; TOP: 7642px; HEIGHT: 1224px" fillcolor="#eaeaea" strokecolor="#eaeaea"><textbox style="mso-next-textbox: #_x0000_s1031"><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 myFile As System.IO.FileInfo<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; LINE-HEIGHT: 14pt"><span lang="EN-US" style="FONT-SIZE: 9pt"><font face="Times New Roman">myFile = New System.IO.FileInfo("\Program Files\My Directory\working.tmp")<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">MsgBox(myFile.Attributes.ToString)<p></p></font></span></p> </div> </td> </tr></tbody></table></textbox></rect><wrap type="none"></wrap><anchorlock></anchorlock></group>
使用FileInfo类的Attributes属性可以设置文件属性。设置文件属性的示例代码如清单11-14所示。首先创建一个指定文件的FileInfo类实例,然后,通过该实例给文件增加只读属性。
清单11-14:移动文件
<group id="_x0000_s1026" style="WIDTH: 414pt; HEIGHT: 117pt; mso-position-horizontal-relative: char; mso-position-vertical-relative: line" editas="canvas" coordorigin="2526,7642" coordsize="7200,2040"><lock v:ext="edit" aspectratio="t"></lock><shape id="_x0000_s1027" style="LEFT: 2526px; WIDTH: 7200px; POSITION: absolute; TOP: 7642px; HEIGHT: 2040px" 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_s1028" style="LEFT: 2526px; WIDTH: 7200px; POSITION: absolute; TOP: 7642px; HEIGHT: 2040px" fillcolor="#eaeaea" strokecolor="#eaeaea"><textbox style="mso-next-textbox: #_x0000_s1028"><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 myFile As System.IO.FileInfo<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">myFile = New System.IO.FileInfo("\Program Files\My Directory\working.tmp")<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">myFile.Attributes = myFile.Attributes + IO.FileAttributes.ReadOnly<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">MsgBox(myFile.Attributes.ToString)<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>