MFC 檢測檔案存在、擷取當前路徑、建立檔案夾

來源:互聯網
上載者:User

標籤:orm   cfile   com   檔案名稱   ever   work   word   db檔案   cst   

直接上代碼:

///////查看執行檔案的路徑下面是否有db檔案夾,若沒有則建立/////////

CFileFind cff;
if (cff.FindFile(_T("db")) == 0)     //當前路徑,沒找到db檔案夾
{
  CString strText;
  TCHAR exepath[MAX_PATH]={0};
  ::GetModuleFileName(NULL,exepath,MAX_PATH);   //擷取exe路徑,存於exepath中
  strText=exepath;                  
  strText = strText.Left(strText.ReverseFind(‘\\‘));   //找到路徑中倒數第一個/,並將其後面的字元掐掉,注意逸出字元的表達\\
  strText += _T("\\");
  strText += _T("db");      //新的路徑,新的檔案名稱
CreateDirectory(strText,NULL); // 產生db檔案夾
}

 

以下為轉載,未核實

1. 擷取Debug或Release所在的路徑
CString GetModuleDir() 

 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 ); 
}

2. 擷取當前工作路徑(dsp所在路徑)

//擷取工作路徑
CString GetWorkDir() 
{  
 char pFileName[MAX_PATH]; 
 int nPos = GetCurrentDirectory( MAX_PATH, pFileName); 
 
 CString csFullPath(pFileName);  
 if( nPos < 0 ) 
  return CString(""); 
 else 
  return csFullPath; 
}

以下為轉載,未核實

C/MFC如何獲得應用程式當前路徑(整理)

第一種方法:DWORD GetCurrentDirectory(
  DWORD nBufferLength,  // size, in characters, of directory buffer
  LPTSTR lpBuffer       // pointer to buffer for current directory
);
BOOL SetCurrentDirectory(
  LPCTSTR lpPathName   // pointer to name of new current directory
);
第二種方法用GetModuleFileName得到應用程式的檔案名稱(第一個參數為NULL)
再用_splitpath分析檔案名稱得到路徑例如://得到當前路徑
 /*char buf[100];
 GetCurrentDirectory(sizeof(buf),buf);
 MessageBox(buf);
 HINSTANCE hInst=NULL;
 hInst=AfxGetApp()->m_hInstance;
 char path_buffer[_MAX_PATH];
 GetModuleFileName(hInst,path_buffer,sizeof(path_buffer));//得到exe檔案的全路徑
 //分離路徑和檔案名稱。
    char drive[_MAX_DRIVE];
    char dir[_MAX_DIR];
    char fname[_MAX_FNAME];
    char ext[_MAX_EXT];
 _splitpath( path_buffer, drive, dir, fname, ext );
 CString Path;
 Path.Format("%s%s",drive,dir);char path[300];strcpy(path,drive);
strcat(path,dir); 又或:TCHAR exeFullPath[MAX_PATH];
    CString strPath;
    GetModuleFileName(NULL,exeFullPath,MAX_PATH);
    strPath=(CString)exeFullPath;
     int position=strPath.ReverseFind(‘\\‘);
    strPath=strPath.Left(position+1); 

    TCHAR FilePath[MAX_PATH];
    GetModuleFileName(NULL,FilePath,MAX_PATH);
    (_tcsrchr(FilePath,‘\\‘))[1] = 0; 
    lstrcat(FilePath,_T("MY.ini")); 

第三種方法:VC中__argv[0]可以得到exe的程式名,然後用_splitpath可以分解得到程式路徑。 第四種方法#include<direct.h>char buf[_MAX_PATH];_getcwd(buf,_MAX_PATH);第四種是得到作業系統所在的目錄char buf[100];
 GetSystemDirectory(buf,100);
 MessageBox(buf); 以下未核實http://www.cnblogs.com/project/archive/2010/12/02/1894494.html

MFC 檢測檔案存在、擷取當前路徑、建立檔案夾

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

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.