RELATEED CONSULTING
相关咨询
选择下列产品马上在线沟通
服务时间:8:30-17:00
你可能遇到了下面的问题
关闭右侧工具栏

新闻中心

这里有您想知道的互联网营销解决方案
MFC:获取可执行文件目录+写日志函数

获取可执行文件目录

inline CString GetExeDir()
{
    TCHAR szPath[ MAX_PATH ] = { 0 };
    GetModuleFileName( NULL, szPath, MAX_PATH );

    CString csFullPath( szPath );
    int nPos = csFullPath.ReverseFind( _T( '\\' ) );
    if( nPos < 0 )
        return CString( "" );
    else
        return csFullPath.Left( nPos );
}

写日志函数

#include  

inline void WriteLog( const CString& strLog )
{   
    setlocale( LC_CTYPE, ( "chs" ) );//写入中文

    CTime tm = CTime::GetCurrentTime();
    CString strLogDir = GetExeDir() + "/Log/" + tm.Format( "%Y-%m" );
    if( !PathIsDirectory( strLogDir ) )
        CreateDirectory( strLogDir, 0 );

    CString strFilePath = strLogDir + tm.Format( "/Sensitive-%Y-%m-%d.txt" );   
    CString strLogText = tm.Format( "[%Y-%m-%d %H:%M:%S]: " ) + strLog;

    CStdioFile  file;
    BOOL bOpen = file.Open( strFilePath, CFile::modeCreate | CFile::modeReadWrite | CFile::modeNoTruncate );
    if( !bOpen )
        return;
    file.SeekToEnd();
    file.WriteString( strLogText );
    file.WriteString( _T( "\r\n" ) );//换行
    file.Flush();
    file.Close();
}

分享名称:MFC:获取可执行文件目录+写日志函数
本文链接:http://sczitong.cn/article/pedigc.html