📄 162-165.html
字号:
<td rowspan="8" align="right" valign="top"><img src="/images/iswbls.gif" width=1 height=400 alt="" border="0"></td>
<td><img src="/images/white.gif" width="5" height="1" alt="" border="0"></td>
<!-- end of ITK left NAV -->
<!-- begin main content -->
<td width="100%" valign="top" align="left">
<!-- END SUB HEADER -->
<!--Begin Content Column -->
<FONT FACE="Arial,Helvetica" SIZE="-1">
To access the contents, click the chapter and section titles.
</FONT>
<P>
<B>Essential Windows CE Application Programming</B>
<FONT SIZE="-1">
<BR>
<I>(Publisher: John Wiley & Sons, Inc.)</I>
<BR>
Author(s): Robert Burdick
<BR>
ISBN: 0471327476
<BR>
Publication Date: 03/01/99
</FONT>
<P>
<form name="Search" method="GET" action="http://search.earthweb.com/search97/search_redir.cgi">
<INPUT TYPE="hidden" NAME="Action" VALUE="Search">
<INPUT TYPE="hidden" NAME="SearchPage" VALUE="http://search.earthweb.com/search97/samples/forms/srchdemo.htm">
<INPUT TYPE="hidden" NAME="Collection" VALUE="ITK">
<INPUT TYPE="hidden" NAME="ResultTemplate" VALUE="itk-simple-intrabook.hts">
<INPUT TYPE="hidden" NAME="ViewTemplate" VALUE="view.hts">
<font face="arial, helvetica" size=2><b>Search this book:</b></font><br>
<INPUT NAME="queryText" size=50 VALUE=""> <input type="submit" name="submitbutton" value="Go!">
<INPUT type=hidden NAME="section_on" VALUE="on">
<INPUT type=hidden NAME="section" VALUE="http://www.itknowledge.com/reference/standard/0471327476/">
</form>
<!-- Empty Reference Subhead -->
<!--ISBN=0471327476//-->
<!--TITLE=Essential Windows CE Application Programming//-->
<!--AUTHOR=Robert Burdick//-->
<!--PUBLISHER=John Wiley & Sons, Inc.//-->
<!--IMPRINT=Wiley Computer Publishing//-->
<!--CHAPTER=6//-->
<!--PAGES=162-165//-->
<!--UNASSIGNED1//-->
<!--UNASSIGNED2//-->
<CENTER>
<TABLE BORDER>
<TR>
<TD><A HREF="159-162.html">Previous</A></TD>
<TD><A HREF="../ewtoc.html">Table of Contents</A></TD>
<TD><A HREF="165-168.html">Next</A></TD>
</TR>
</TABLE>
</CENTER>
<P><BR></P>
<H3><A NAME="Heading8"></A><FONT COLOR="#000077">Searching for Files</FONT></H3>
<P>It may seem a little strange to discuss searching for files in the Windows CE file system this early in the chapter. Certainly, operations such as creating and deleting files must be more fundamental than file searching!
</P>
<P>While this may be true in some sense, it is also the case that many file operations are iterative. For example, deleting a directory requires recursively deleting all files and subdirectories contained by the directory to be deleted. Such an iterative process entails file searching operations to look for files to delete.</P>
<P>As another example, consider how an application might determine if a particular directory is empty. There is no Windows CE API function <I>IsDirectoryEmpty</I>. Writing such an operation from scratch involves searching the directory in question to see if it contains any files.</P>
<P>In fact, file searching is so fundamental to many file system operations that understanding how these features are implemented requires that we first understand file searching under Windows CE.</P>
<P>Windows CE provides three functions for such operations: <I>FindFirstFile</I>, <I>FindNextFile,</I> and <I>FindClose</I>.</P>
<P>The first two of these functions return a data structure containing information about the files that they retrieve. Before discussing the find functions in detail, let’s first look at this structure.</P>
<BLOCKQUOTE>
<P><FONT SIZE="-1"><HR><B>NOTE: </B><B>F<SMALL>IND</SMALL>F<SMALL>IRST</SMALL>F<SMALL>ILE</SMALL>E<SMALL>X</SMALL></B>
<P><B>The function <I>FindFirstFileEx</I> is not supported under Windows CE.</B><HR></FONT>
</BLOCKQUOTE>
</P>
<P><FONT SIZE="+1"><B>The WIN32_FIND_DATA Structure</B></FONT></P>
<P>The WIN32_FIND_DATA structure is used by Windows CE to provide information about a file located by one of the find functions.
</P>
<!-- CODE //-->
<PRE>
typedef struct _WIN32_FIND_DATA
{
DWORD dwFileAttributes;
FILETIME ftCreationTime;
FILETIME ftLastAccessTime;
FILETIME ftLastWriteTime;
DWORD nFileSizeHigh;
DWORD nFileSizeLow;
DWORD dwOID;
TCHAR cFileName[ MAX_PATH ];
} WIN32_FIND_DATA;
</PRE>
<!-- END CODE //-->
<P>The members of this structure provide all descriptive information about the file, either directly, or by providing a means for extracting more information (such as through the <I>dwOID</I> member).</P>
<P><I>dwFileAttributes</I> contains the attributes of the file as described previously in Table 6.1. <I>ftCreationTime</I>, <I>ftLastAccessTime</I>, and <I>ftLastWriteTime</I> represent the times the file was created, last accessed, and last written to, respectively. <I>nFileSizeHigh</I> and <I>nFileSizeLow</I> are the high-order and low-order words of the total size of the file. The <I>dwOID</I> member contains the object identifier of the file. This means that whenever an application can get WIN32_FIND_DATA about a file, it can also get any of the CEFILEINFO data about the file with a simple call to <I>CeOidGetInfo</I>. Finally, <I>cFileName</I> is a null-terminated string containing the name of the file.</P>
<P>We will primarily be interested in how to use the <I>FindFirstFile</I> and <I>FindNextFile</I> functions to get WIN32_FIND_DATA information. It should be noted that Windows CE provides some additional functions for quickly accessing some of the data provided by this structure. In particular, <I>GetFileTime</I> retrieves the same information as provided by the FILETIME members of the WIN32_FIND_DATA structure. <I>GetFileSize</I> returns the size of a specified file.</P>
<P>In addition, there is a <I>SetFileTime</I> function to allow applications to modify the creation time, last access time, and last write time of a file.</P>
<P>Because of its similarity to the WIN32_FIND_DATA structure, I somewhat parenthetically mention the BY_HANDLE_FILE_INFORMATION structure. This is another data structure that contains much the same information about a file as WIN32_FIND_DATA. Given a handle to an open file, an application can get a BY_HANDLE_FILE_INFORMATION structure by calling <I>GetFileInformationByHandle</I>.</P>
<P><FONT SIZE="+1"><B>The <I>FindFirstFile</I> and <I>FindNextFile</I> Functions
</B></FONT></P>
<P>The <I>FindFirstFile</I> function is used to locate a specific file or directory. It can also be used to find the first file in a specified directory.</P>
<!-- CODE SNIP //-->
<PRE>
FindFirstFile(lpFileName, lpFindFileData);
</PRE>
<!-- END CODE SNIP //-->
<P>The <I>lpFileName</I> parameter contains a path and file name, or a directory name. <I>lpFindFileData</I> is used as a return value by the function. It is a pointer to a WIN32_FIND_DATA structure containing information about the located file.</P>
<P>If successful, <I>FindFirstFile</I> returns a <I>search handle</I>. This handle references an internal structure that is responsible for keeping track of the progress of a file search. We will see the utility of this search handle a bit later when we discuss the <I>FindNextFile</I> function. If <I>FindFirstFile</I> fails, it returns INVALID_HANDLE_VALUE.</P>
<P>The <I>lpFileName</I> parameter accepts wildcards. This is how you can tell <I>FindFirstFile</I> to differentiate between finding the first file in a specified directory and finding the directory itself.</P>
<P>For example, this line of code will try and find a directory called \MyFiles:</P>
<!-- CODE SNIP //-->
<PRE>
HANDLE hFile;
WIN32_FIND_DATA fd;
memset(&fd, 0, sizeof(fd));
hFile = FindFirstFile(TEXT("\\MyFiles"), &fd);
</PRE>
<!-- END CODE SNIP //-->
<P>On the other hand, one subtle change to the <I>FindFirstFile</I> call will find the first file in the \MyFiles directory:</P>
<!-- CODE SNIP //-->
<PRE>
hFile = FindFirstFile(TEXT("\\MyFiles\\*"), &fd);
</PRE>
<!-- END CODE SNIP //-->
<P><I>FindNextFile</I> is used to continue a search started by <I>FindFirstFile</I>. For example, if <I>FindFirstFile</I> finds the first file in a specified directory, <I>FindNextFile</I> will attempt to find the next file in that directory. Each successive call to <I>FindNextFile</I> uses the search handle that is updated with each find operation to keep track of the search progress. The syntax of <I>FindNextFile</I> is:</P>
<!-- CODE SNIP //-->
<PRE>
FindNextFile(hFindFile, lpFindFileData);
</PRE>
<!-- END CODE SNIP //-->
<P>The first parameter is the search handle returned by a previous call to <I>FindFirstFile</I>. The second parameter is the WIN32_FIND_DATA return value, just as in <I>FindFirstFile</I>.</P>
<P><I>FindNextFile</I> returns TRUE if successful and FALSE if it fails. As with all of the file system functions, a call to <I>GetLastError</I> can be used to get additional information about why the function call failed if the return value is FALSE.</P><P><BR></P>
<CENTER>
<TABLE BORDER>
<TR>
<TD><A HREF="159-162.html">Previous</A></TD>
<TD><A HREF="../ewtoc.html">Table of Contents</A></TD>
<TD><A HREF="165-168.html">Next</A></TD>
</TR>
</TABLE>
</CENTER>
<!-- all of the reference materials (books) have the footer and subfoot reveresed -->
<!-- reference_subfoot = footer -->
<!-- reference_footer = subfoot -->
<!-- BEGIN SUB FOOTER -->
<br><br>
</TD>
</TR>
</TABLE>
<table width="640" border=0 cellpadding=0 cellspacing=0>
<tr>
<td align="left" width=135><img src="/images/white.gif" width=100 height="1" alt="" border="0"></td>
<!-- END SUB FOOTER -->
<!-- all of the books have the footer and subfoot reveresed -->
<!-- reference_subfoot = footer -->
<!-- reference_footer = subfoot -->
<!-- FOOTER -->
<td width="515" align="left" bgcolor="#FFFFFF">
<font face="arial, helvetica" size="1"><b><a href="/products.html"><font color="#006666">Products</font></a> | <a href="/contactus.html"><font color="#006666">Contact Us</font></a> | <a href="/aboutus.html"><font color="#006666">About Us</font></a> | <a href="http://www.earthweb.com/corporate/privacy.html" target="_blank"><font color="#006666">Privacy</font></a> | <a href="http://www.itmarketer.com/" target="_blank"><font color="#006666">Ad Info</font></a> | <a href="/"><font color="#006666">Home</font></a></b>
<br><br>
Use of this site is subject to certain <a href="/agreement.html">Terms & Conditions</a>, <a href="/copyright.html">Copyright © 1996-1999 EarthWeb Inc.</a><br>
All rights reserved. Reproduction whole or in part in any form or medium without express written permision of EarthWeb is prohibited.</font><p>
</td>
</tr>
</table>
</BODY>
</HTML>
<!-- END FOOTER -->
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -