Common MFC folder and directory operations

Source: Internet
Author: User

Cstring ccallschedulingsystemdlg: getfilepath (void)

{

Hmodule module = getmodulehandle (0 );

Char pfilename [max_path];

Getmodulefilename (module, pfilename, max_path );


Cstring csfullpath (pfilename );

Int NPOs = csfullpath. reversefind (_ T ('\\'));

If (NPOs <0)

Return cstring ("");

Else

Return csfullpath. Left (NPOs );

}


Char * ccallschedulingsystemdlg: encodetoutf8 (const char * mbcsstr)

{

// The Chinese and English paths that can be played

Int wcslen;

Wchar_t * wszstring;

Int u8len;

Char * szu8;

// Pre-convert to get the size of the required space

Wcslen =: multibytetowidechar (cp_acp, null, mbcsstr, strlen (mbcsstr), null, 0 );

// Leave a space for '\ 0' to allocate space. multibytetowidechar does not give' \ 0' Space

Wszstring = new wchar_t [wcslen + 1];

// Conversion

: Multibytetowidechar (cp_acp, null, mbcsstr, strlen (mbcsstr), wszstring, wcslen );

// Add '\ 0' at the end'

Wszstring [wcslen] = '\ 0 ';

/*************************************** *********************************/

/* Unicode to utf8 */

/*************************************** *********************************/

// Unicode to utf8

// Pre-convert to get the size of the required space. The function used this time is opposite to the above name

U8len =: widechartomultibyte (cp_utf8, null, wszstring, wcslen (wszstring), null, 0, null, null );

// Same as above, the allocated space should be reserved for '\ 0'

// Although utf8 is a unicode compression format, it is also a multi-byte string, so it can be saved as char

Szu8 = new char [u8len + 1];

// Conversion

// The strlen for Unicode is wcslen.

: Widechartomultibyte (cp_utf8, null, wszstring, wcslen (wszstring), szu8, u8len, null, null );

// Add '\ 0' at the end'

Szu8 [u8len] = '\ 0 ';

Free (wszstring );

Return szu8;

}


Bool ccallschedulingsystemdlg: emptydirectory (cstring & Spath)

{

Cfilefind finder;

Cstring swildcard = Spath + "//*.*";

Bool bfound;

Bool bworking = finder. findfile (swildcard );

Bfound = bworking;

While (bworking)

{

Bworking = finder. findnextfile ();

If (finder. isdots () continue;

If (finder. isdirectory ())

{

Cstring S = finder. getfilepath ();

Emptydirectory (s );

Removedirectory (finder. getfilepath ());

Continue;

}

_ Tunlink (finder. getfilepath (); // note that _ tunlink is used here.

}

Return bfound;

}




This article from the "Know what it does not" blog, please be sure to keep this source http://shenyantao.blog.51cto.com/1957956/1543253

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.