Windows API Learning

Source: Internet
Author: User

# Include <stdio. h>
# Include <windows. h>
# Include <time. h>
# Include <shlobj. h>
/*
Author: Yuan Wen
Time: 2007-12-28
E-mail: wenwen227@126.com
Debugging environment: Windows XP + Dev-C ++
Function: common Windows API learning practices
1. Folder selection dialog box
2. Implementation of folder Traversal
3. How to display large files
Brief Introduction:
Traverse all files and folders in a specified folder
If no folder is selected, the folder where the current file is located is retrieved.
Count the path and size of the Maximum File
Collect the latest files

*/
Void browse (char * strdir );
Unsigned int filecount = 0; // number of files
Unsigned int dircount = 0; // number of directories
Unsigned int maxsize = 0; // Maximum File Size
Char maxfilepath [max_path];
Char * selectfolder (char * pathname );
Int main (INT argc, char * argv [])
{
Char filepath [max_path];
Clock_t start, finish;
Selectfolder (filepath );
I = strlen (filepath );
If (0 = I) // if no return path exists in the selection box, the default path is used.
{
Strcpy (filepath, argv [0]);
I = strlen (filepath );
For (; filepath [I]! = '//'; I --);
Filepath [I] = '/0 ';
}
Filepath [I] = '//';
Filepath [I + 1] = '*';
Filepath [I + 2] = '/0 ';
Start = clock ();
Browse (filepath); // filepath
Finish = clock ();
Printf ("Time consumed: % DMS file count: % d directory count: % d/N", finish-start, filecount, dircount, maxsize );
Printf ("Maximum File Path:/n % s/n", maxfilepath );
System ("pause ");
Return 0;
}
/*
Struct _ win32_find_dataa {
DWORD dwfileattributes; file attributes
Filetime ftcreationtime; Creation Time
Filetime ftlastaccesstime; Access time
Filetime ftlastwritetime; modification time
DWORD nfilesizehigh; file size in bytes
DWORD nfilesizelow; Low file size in bytes
DWORD dwreserved0; reserved
DWORD dwreserved1; reserved
Char cfilename [max_path]; file name
Char calternatefilename [14]; Unknown
}
*/
Void browse (char * strdir)
{
Win32_find_dataa WFD;
Handle m_hfle = findfirstfile (strdir, & WFD );
DWORD errcod = 0;
Char fullpath [max_path];
If (invalid_handle_value! = M_hfle)
{
Do
{
Strcpy (fullpath, strdir );
Fullpath [strlen (fullpath)-strlen ("*")] = '/0'; // remove //*
Strcat (fullpath, WFD. cfilename );
If (WFD. dwfileattributes & file_attribute_directory)
{// Directory
If (WFD. cfilename [0]! = '.')
{
Strcat (fullpath ,"//*");
// Printf (fullpath );
Browse (fullpath); // recursively scan sub-files in this folder
Dircount ++;
}
}
Else // File
{
// Printf ("% s filesize = % d/N", WFD. cfilename, WFD. nfilesizelow );
Filecount ++;
If (WFD. nfilesizelow> maxsize)
{
Maxsize = WFD. nfilesizelow;
Sprintf (maxfilepath, "% s", fullpath );
}
}
} While (findnextfile (m_hfle, & WFD ));
}

If (invalid_handle_value! = M_hfle)
{
Findclose (m_hfle );
}
}

Char * selectfolder (char * pathname)
{
Browseinfo Bi; // controls the structure of properties in the Browse dialog box
Lpitemidlist Itemid;
// Char sdir [max_path]; // max_path: System constant, which indicates the maximum path length.
Char Foldername [max_path];
Memset (& BI, 0, sizeof (browseinfo); // Reset
Pathname [0] = '/0 ';
// Memset (sdir, 0, max_path );
Bi. hwndowner = NULL; // parent window handle
Bi. pszdisplayname = Foldername; // buffer for storing the path
Bi. lpsztitle = "how are you? "; // Dialog box title
Bi. ulflags = bif_returnonlyfsdirs; // only display the file system folder
Itemid = shbrowseforfolder (& BI );
If (Itemid = NULL)
Return pathname;
Shgetpathfromidlist (Itemid, pathname );
Return pathname;
}

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.