[Software]_[windows]_[file operation method commonly used in product development]

Source: Internet
Author: User


Scene:

1. When developing a Windows product, many things need to be packaged themselves, as it does not have a good object model like cocoa, which allows access to file-related methods through classes. Like copying folders?

To know whether Win32 provides a copy folder This function is really through Baidu. MSDN is really bad.

2. Open the Select Folder window and so on when the interface is developed.

3. Set file creation time and modification time, etc.

4. Can also be transplanted in the product.


Bas_utility_file.h:

#ifndef __bas_utility_file_h#define __bas_utility_file_h#include "bas_exp.h" #include <stdint.h> #include < windows.h> #include "stdio.h" typedef struct BASFILEINFO1{WCHAR_T* dir;wchar_t* filename;wchar_t* ext;} Basfileinfo;class lib_basic basutilityfile{public:static BOOL copydir (lpctstr lpszsrcdir, LPCTSTR lpszDstDir); static wchar_t* zlgetformatsizefrombytes (uint64_t size); static wchar_t* getfilepathnewname (const wchar_t* path);// utf8static bool Isfileexist (const char* path), static bool Isfileexist (const wchar_t* path), Static int64_t GetFileSize ( const wchar_t* path);//datetaken:yyyy-mm-dd HH:mm:ss; HH is a 1-24-hour system. static bool Setfilecreateandmodifytime (const wchar_t* path,const wchar_t* Date_taken);//0: Directory//1: File name//2: Extension static Basfileinfo getfileinfo (const wchar_t* Path,tchar c); static wchar_t* Selectfolder ();}; #endif


Bas_utility_file.cpp:
#include <string.h> #include <sstream> #include <ShlObj.h> #include "basic/bas_utility_file.h" # Include "Basic/bas_utility_string.h" bool Basutilityfile::setfilecreateandmodifytime (const wchar_t* path,const WCHAR _t* date_taken) {wchar_t* date = Wcsdup (Date_taken); wchar_t* pos = WCSCHR (date,l '-'); WORD year = 0; WORD month = 0; WORD day = 0; WORD hour = 0; WORD minute = 0; WORD seconds = 0;*pos = 0x0;year = _wtoi (date); wchar_t* start = Pos+1;pos = WCSCHR (start,l '-'); *pos = 0x0;month = _wtoi (st ART); start = Pos+1;pos = WCSCHR (start,0x20); *pos = 0x0;day = _wtoi (start); start = Pos+1;pos = Wcschr (start,l ': '); *pos = 0x 0;hour = _wtoi (start), hour = (hour = =)? 0:hour;start = Pos+1;pos = WCSCHR (start,l ': '); *pos = 0x0;minute = _wtoi (start); s Tart = Pos+1;seconds = _wtoi (start); HANDLE file = CreateFile (Path,generic_read | Generic_write,0,null,open_existing,file_attribute_normal,null); if (FILE! = invalid_handle_value) {FILETIME ft; SYSTEMTIME ST,STUTC;  BOOL F;//hour:0-23getsystemtime (&st);            Gets the current system timest.wyear = Year;st.wmonth = Month;st.wday = Day;st.whour = Hour;st.wminute = mi Nute;st.wsecond = Seconds;st.wdayofweek = 0;st.wmilliseconds = 0; Tzspecificlocaltimetosystemtime (NULL,&AMP;ST,&AMP;STUTC);  SystemTimeToFileTime (&AMP;STUTC, &ft);  Converts the current system time to file time FORMATF = setfiletime (file,//sets Last-write time of the file &ft,null,//to the converted current system time &ft); CloseHandle (file); free (date); return true;} return false;} BOOL Basutilityfile::copydir (LPCTSTR lpszsrcdir, LPCTSTR lpszdstdir) {shfileopstruct SFO; ZeroMemory (&AMP;SFO, sizeof (SFO)); Sfo.wfunc = fo_copy; Sfo.pfrom = Lpszsrcdir; Sfo.pto = Lpszdstdir; Sfo.fflags = Fof_silent | fof_noconfirmation | Fof_noconfirmmkdir; int ret = SHFileOperation (&AMP;SFO); if (ret = = 0) return TRUE; else return FALSE;    }wchar_t* basutilityfile::zlgetformatsizefrombytes (uint64_t size) {double kb = (double) size/1024; Std::wstring result;wchar_t buf[16];        if (KB < 1) {swprintf (buf,l "%.2f B", (double) size); Result.append (BUF);    Return Wcsdup (Result.c_str ());    } double MB = kb/1024;        if (MB < 1) {swprintf (buf,l "%.2f kb", KB); Result.append (BUF);    Return Wcsdup (Result.c_str ());    } double GB = mb/1024;        if (GB < 1) {swprintf (buf,l "%.2f mb", MB); Result.append (BUF);    Return Wcsdup (Result.c_str ());    }swprintf (buf,l "%.2f GB", GB); Result.append (BUF); Return Wcsdup (Result.c_str ());} wchar_t* Basutilityfile::selectfolder () {TCHAR szpath[max_path]={0}; TCHAR Szdir[max_path]={0};memset (szdir,0,sizeof (szdir)); GetCurrentDirectory (Max_path,szpath); Browseinfo Bi;bi.hwndowner = Null;bi.pidlroot=null;bi.iimage=null;bi.ulflags = Bif_newdialogstyle;bi.pszdisplayname =szdir;bi.lpsztitle=szpath;bi.lparam=null;bi.lpfn=null;if (! SHGetPathFromIDList (SHBrowseForFolder (&AMP;BI), szdir)) {return NULL;} Szdir[wcslen (szdir)] = L ' \ \ '; return Wcsdup (Szdir);} Basfileinfo Basutilityfile::getfileinfo (cOnst wchar_t* Path1,tchar c) {std::wstring path (path1); size_t pos = 0; Basfileinfo info;if (pos = path.find_last_of (c))! = Std::wstring::npos) {Info.dir = Wcsdup (Path.substr (0,pos+1). C_STR ( ));p os++;} Else{info.dir = Wcsdup (L "");p OS = 0;} size_t dot = path.find_last_of (L '. '); if (dot! = Std::wstring::npos) {info.filename = Wcsdup (Path.substr (Pos,dot-pos). C_STR ()); info.ext = Wcsdup (path.substr (dot). C_str ());} Else{info.filename = Wcsdup (Path.substr (POS). C_STR ()); info.ext = Wcsdup (L "");} return info;}    int64_t basutilityfile::getfilesize (const wchar_t* path) {file* FILE = _wfopen (path,l "RB");        if (file) {fseek (file, 0, seek_end);        int64_t size = ftell (file);        fclose (file);    return size; } return 0;} BOOL Basutilityfile::isfileexist (const char* path) {wchar_t* path1 = basutilitystring::convertutf8tounicode (path); BOOL res = isfileexist (path1); free (path1); return res;} BOOL Basutilityfile::isfileexist (const wchar_t* path) {if (_waccess (path,0)! =-1) {return true;} return FALSE;} wchar_t* basutilityfile::getfilepathnewname (const wchar_t* path1) {if (! Isfileexist (path1)) {return wcsdup (path1);}    Std::wstring path (path1);    int count = 1; std::wstring temp;size_t dot = path.find_last_of (L "."); size_t slash = path.find_last_of (L "\"); std::wstring Dir_part = Path.substr (0,slash+1); std::wstring Postfix_part = PATH.SUBSTR (dot); std::wstring Name_part = Path.substr (slash+1,dot-slash-1); int kmaxcount = 1000;while (Count < Kmaxcount) {std::wstring new_path;std::wstringstream wss;wss << L "(" << count << L ")"; new_path.append (Dir_part). Append (Name_part). Append (Wss.str ()). append (Postfix_part); if (! Isfileexist (New_path.c_str ())) {return wcsdup (New_path.c_str ());}    ++count;} Return Wcsdup (Path.c_str ());}



Copyright NOTICE: This article for Bo Master original article, without Bo Master permission not reproduced.

[Software]_[windows]_[file operation method commonly used in product development]

Related Article

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.