VC++ MFC 读取保存文件对话框,本程序根目录?
VC++ 读取保存文件对话框
在用读取对话框时 如何 设置成是本程序的根目录?
//显示文件打开对话框
CFileDialog dlg(TRUE, "EC ", "*.aes ",OFN_HIDEREADONLY|OFN_OVERWRITEPROMPT, "Aes Files(*.aes)|*.aes ");
if ( dlg.DoModal()!=IDOK ) return;
//获取文件的绝对路径
CString sFileName=dlg.GetPathName();
上面的代码是,显示文件打开对话框是在我的文档里面!
要在打开时就进入本程序的根目录,如何改上面的代码?
[解决办法]
找到了一个解决办法:
void CTestDlgDlg::OnOK()
{
// TODO: Add extra validation here
CFileDialog dlg(TRUE, "EC ", "*.aes ",OFN_HIDEREADONLY|OFN_OVERWRITEPROMPT, "Aes Files(*.aes)|*.aes ");
dlg.m_ofn.lpstrInitialDir = "E:\\ "; // 将这里的dlg.m_ofn.lpstrInitialDir赋值为本程序的根目录,我这里设为了E盘
if ( dlg.DoModal()!=IDOK ) return;
CString sFileName=dlg.GetPathName();
//CDialog::OnOK();
}
[解决办法]
int i;
char cc[256];
CString str, str1;
HMODULE hmodule = GetModuleHandle( "myprogram.exe ");
GetModuleFileName(hmodule, cc, 256);
str = cc;
i = str.Find( "\\ ");
str1 = str.Left(i+1);
SetCurrentDirectory(str1);
CFileDialog dlg(TRUE, "EC ", "*.aes ",OFN_HIDEREADONLY|OFN_OVERWRITEPROMPT, "Aes Files(*.aes)|*.aes ");
if ( dlg.DoModal()!=IDOK ) return;
//获取文件的绝对路径
CString sFileName=dlg.GetPathName();