CFileDialog自动改变当前目录问题解决方案

本文记录了一个关于使用CFileDialog时遇到的路径变化问题及两种解决办法:一是手动保存原始路径;二是通过设置对话框的Flags选项OFN_NOCHANGEDIR来避免路径被更改。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

今天碰到一个奇怪的问题,代码如下

// “文件对话框”执行前获取程序所在路径
	char szFilePath[MAX_PATH];
	GetCurrentDirectory(MAX_PATH, szFilePath);
	CString strFilePath = szFilePath;
	AfxMessageBox(strFilePath);
	
	CFileDialog dlg(TRUE);
	dlg.DoModal();

	// “文件对话框”执行后获取程序所在路径
	GetCurrentDirectory(MAX_PATH, szFilePath);
	strFilePath = szFilePath;
	AfxMessageBox(strFilePath);

我发现“文件对话框”执行前后,获取程序所在路径不一致,具体描述如下。假设我的可执行文件Test.exe

位于D:\下,则程序执行第一段得到的当前目录确实是D:\,程序执行打开”文件对话框“时,我选择打开

C:\Debug\1.txt后,再获取程序所在的路径,这时不再是D:\,而是C:\Debug\了。

下面说下2种解决方案

(1)笨方法:打开”文件对话框“执行前先将可执行文件所在路径保存到一个变量中,这样即使执行了

打开”文件对话框,也可以访问到执行打开”文件对话框“的那个路径

(2)其实,可以设置打开”文件对话框“不让其改变当前路径,具体代码如下,不解释

注意CFileDialog成员m_ofn中的Flags选项设置

// “文件对话框”执行前获取程序所在路径
	char szFilePath[MAX_PATH];
	GetCurrentDirectory(MAX_PATH, szFilePath);
	CString strFilePath = szFilePath;
	AfxMessageBox(strFilePath);
	
	CFileDialog dlg(TRUE);
	dlg.m_ofn.Flags |= OFN_NOCHANGEDIR;
	dlg.DoModal();

	// “文件对话框”执行后获取程序所在路径
	GetCurrentDirectory(MAX_PATH, szFilePath);
	strFilePath = szFilePath;
	AfxMessageBox(strFilePath);



void LinkCommand::addopen() { // 创建文件选择对话框 CFileDialog fileDlg( TRUE, // TRUE 表示打开文件对话框 _T("CATProduct"), // 默认文件扩展名 NULL, // 初始文件名 OFN_FILEMUSTEXIST, // 必须存在的文件 _T("CATIA模型文件 (*.CATPart;*.CATProduct)|*.CATPart;*.CATProduct|所有文件 (*.*)|*.*||"), // 文件过滤器 NULL // 父窗口指针 ); fileDlg.m_ofn.lpstrTitle = _T("选择 CATIA 模型文件"); // 对话框标题 if (fileDlg.DoModal() != IDOK) // 如果用户取消选择 { return; } // 获取文件路径并转换为CATUnicodeString CString selectedPath = fileDlg.GetPathName(); CATUnicodeString catPath; catPath.BuildFromWChar(selectedPath.GetBuffer()); // 转换为CATUnicodeString selectedPath.ReleaseBuffer(); // 释放缓冲区 // 加载选中的文件 CATDocument* pDocToAssemble = NULL; HRESULT hr = CATDocumentServices::OpenDocument(catPath, pDocToAssemble); if (FAILED(hr) || !pDocToAssemble) { MessageBox(NULL, _T("模型加载失败"), _T("错误"), MB_ICONERROR); return; } // 获取选中文件的根产品(优化后的实现) CATIDocRoots* piDocRootsToAssemble = NULL; hr = pDocToAssemble->QueryInterface(IID_CATIDocRoots, (void**)&piDocRootsToAssemble); if (FAILED(hr) || !piDocRootsToAssemble) { pDocToAssemble->Release(); MessageBox(NULL, _T("无法获取文档的根产品"), _T("错误"), MB_ICONERROR); return; } CATListValCATBaseUnknown_var* pRootProductsToAssemble = piDocRootsToAssemble->GiveDocRoots(); CATIProduct_var spRootProductToAssemble = NULL_var; if (pRootProductsToAssemble && pRootProductsToAssemble->Size() > 0) { spRootProductToAssemble = (*pRootProductsToAssemble)[1]; } delete pRootProductsToAssemble; piDocRootsToAssemble->Release(); if (spRootProductToAssemble == NULL_var) { pDocToAssemble->Release(); MessageBox(NULL, _T("选中的文档中无根产品"), _T("错误"), MB_ICONERROR); return; } // 获取当前编辑器 CATFrmEditor* pEditor = CATFrmEditor::GetCurrentEditor(); if (!pEditor) { pDocToAssemble->Release(); MessageBox(NULL, L"无法获取编辑器窗口", L"错误", MB_ICONERROR); return; } // 获取当前编辑器中的文档 CATDocument* pCurrentProductDoc = pEditor->GetDocument(); if (!pCurrentProductDoc) { pDocToAssemble->Release(); MessageBox(NULL, L"没有活动文档", L"警告", MB_ICONWARNING); return; } // 检查当前文档类型是否为CATProduct CATUnicodeString currentDocType = pCurrentProductDoc->GetType(); if (currentDocType != "CATProduct") { pDocToAssemble->Release(); MessageBox(NULL, L"当前文档不是CATProduct类型", L"类型错误", MB_ICONERROR); return; } // 获取当前文档的根产品(优化后的实现) CATIDocRoots* piCurrentDocRoots = NULL; hr = pCurrentProductDoc->QueryInterface(IID_CATIDocRoots, (void**)&piCurrentDocRoots); if (FAILED(hr) || !piCurrentDocRoots) { pDocToAssemble->Release(); MessageBox(NULL, L"无法获取当前文档的根产品", L"错误", MB_ICONERROR); return; } CATListValCATBaseUnknown_var* pCurrentRootProducts = piCurrentDocRoots->GiveDocRoots(); CATIProduct_var spCurrentProductOnRoot = NULL_var; if (pCurrentRootProducts && pCurrentRootProducts->Size() > 0) { spCurrentProductOnRoot = (*pCurrentRootProducts)[1]; } delete pCurrentRootProducts; piCurrentDocRoots->Release(); if (spCurrentProductOnRoot == NULL_var) { pDocToAssemble->Release(); MessageBox(NULL, L"当前文档中无根产品", L"错误", MB_ICONERROR); return; } // 将选中的文件装配到当前Product文件中 CATIProduct_var spInstanceProd = spCurrentProductOnRoot->AddProduct(spRootProductToAssemble); if (spInstanceProd == NULL_var) { pDocToAssemble->Release(); MessageBox(NULL, _T("装配失败"), _T("错误"), MB_ICONERROR); return; } // 先执行刷新操作 if (spCurrentProductOnRoot != NULL_var) { CATIRedrawEvent_var spRedrawEvent = spCurrentProductOnRoot; if (spRedrawEvent != NULL_var) spRedrawEvent->Redraw(); } // 保存文档 if (pCurrentProductDoc) { CATDocumentServices::Save(*pCurrentProductDoc, FALSE); } /*CATSession* pSession = CATSession::GetPtrSession(); if(pSession) { CATIIniInteractiveSession* pInteract = NULL; pSession->QueryInterface(IID_CATIIniInteractiveSession, (void**)&pInteract); pInteract->GetCurrentDocumentPath(path); } // 获取当前文档路径:ml-citation{ref="3" data="citationList"} CATSession* pSession = CATSession::GetPtrSession(); if(!pSession) return; CATIIniInteractiveSession* pInteract = NULL; hr = pSession->QueryInterface(IID_CATIIniInteractiveSession, (void**)&pInteract); if(SUCCEEDED(hr)) { pInteract->Open(docPath, TRUE, NULL); // 异步打开:ml-citation{ref="6" data="citationList"} pInteract->Release(); }*/ // 释放资源 pDocToAssemble->Release(); // 最后显示提示信息 //MessageBox(NULL, _T("装配成功"), _T("信息"), MB_ICONINFORMATION); }在函数中刷新机制不够完善,模型和属性都不能查看
最新发布
06-19
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值