Settings | Sign in | Sign up

There are currently 3 posts.

【解决办法】VC6程序设置开机启动后默认目录自动更正

Floor 1 巨大八爪鱼 3/30/13 20:23

在窗口类的构造函数中加上:

//获取程序目录
 SetCurrentDirectory("C:\\"); //模拟开机启动
 char ch[MAX_PATH];
 memset(ch,0,sizeof(ch));
 GetModuleFileName(AfxGetInstanceHandle(),ch,sizeof(ch));
 ch[strrchr(ch,'\\')-ch]='\0';
 m_path=ch;
 m_path+="\\";

SetCurrentDirectory(path);

Floor 2 巨大八爪鱼 3/30/13 20:25

1L的代码有几处错误,先更正一下。

适用于VC6:

//获取程序目录
 SetCurrentDirectory("C:\\"); //模拟开机启动
 char ch[MAX_PATH];
 memset(ch,0,sizeof(ch));
 GetModuleFileName(AfxGetInstanceHandle(),ch,sizeof(ch));
 ch[strrchr(ch,'\\')-ch]='\0';

CString path=ch;
 path+="\\";
SetCurrentDirectory(path);

 

 

以下适用于VC2008:

//获取程序目录
 SetCurrentDirectory(L"C:\\"); //模拟开机启动
 wchar_t ch[MAX_PATH];
 memset(ch,0,sizeof(ch));
 GetModuleFileName(AfxGetInstanceHandle(),ch,sizeof(ch));
 ch[wcsrchr(ch,'\\')-ch]='\0';
 CString path=ch;
 path+=L"\\";
 //AfxMessageBox(path);
 SetCurrentDirectory(path);

 

 

 

如果是单/多文档程序,可以在CMainFrame::CMainFrame()加入本代码

如果是对话框程序,可在对话框类的【构造函数】上加入本代码(不是InitDialog函数上)

Floor 3 巨大八爪鱼 2/14/17 21:59
【改进后的代码】
void InitDirectory(void)
{
    LPTSTR p;
    TCHAR szPath[MAX_PATH];
#if _DEBUG
    SetCurrentDirectory(TEXT("C:\\")); // 模拟开机启动
#endif
    GetModuleFileName(GetModuleHandle(NULL), szPath, MAX_PATH);
    p = _tcsrchr(szPath, TEXT('\\'));
    *(p + 1) = '\0';
    SetCurrentDirectory(szPath);
}

用到的头文件:
#include <tchar.h>
#include <Windows.h>

Content converter:

Reply the post
Content:
User: You are currently anonymous.
Captcha:
Unclear? Try another one.
©2010-2025 Purasbar Ver3.0 [Mobile] [Desktop]
Except where otherwise noted, content on this site is licensed under a Creative Commons Attribution-ShareAlike 3.0 Unported license.