程式只要改變執行環境,所在的路徑也會隨之而變
為了要讓程式到任何的環境都能夠順利地執行
狗哥在這邊分享一個方法
在MFC的資料夾中新增一個System.ini檔,
路徑 : C:\Users\XX\XX\XX\Debug\Parameters\System.ini ( Parameters資料夾自行新增 )
然而建立這個ini檔案要做什麼
請參考下圖
此時應該頓時明白了,原來使用者可以到這裡面設定路徑
好讓程式到System.ini檔去讀取所需要執行的路徑
至於如何撈取此檔案中的路徑我會在下一節中做教學
不過問題來了,那至少也得讓我知道此System.ini的位置在哪吧??????????????????????
別驚,狗哥接著就教你如何取得System.ini的路徑
MFC裡面有個Global函數 : AfxGetApp()
CString str = AfxGetApp() -> m_pszHelpFilePath;
AfxMessagebox(str);
此時str得到一段路徑,除了最後的Test.HLP外
前面C:\ ~ Debug\都是我們需要的路徑
所以我們就把不必要的Test.HLP去掉吧
CString str;
str = AfxGetApp()->m_pszHelpFilePath;
CString path; // 路徑
CString temp; // 暫存資料夾名稱
CString ch; // 取得當前字元
int pos = 0; // 當前字元位置
int len = str.GetLength(); // 字串長度
// 從字串的第一位讀到最後一位
while (pos != len)
{
ch = str.GetAt(pos); // 取得str當前字元
temp = temp + ch; // 字元組成的資料夾名稱
pos++;
// 當ch讀入路徑的 "\"時則將temp存入path,然後清空temp
if (ch == "\\")
{
path= path+ temp;
temp = _T("");
}
} // while
path = path + _T("Parameters\\System.ini");
AfxMessageBox(path );
此時path得到System.ini的路徑囉~~~~
下一篇 如何解析System.ini檔