VC得到当前目录与得到应用程序目录的一个应用
2010-11-17 22:55
387 查看
得到当前目录的函数: GetCurrentDirectory
得到应用程序目录的函数是: GetModuleFileName
用法都很简单,不知道就看MSDN吧。
我先用这2个函数分别写一个实现同一个功能的函数。
你用这2个函数分别执行一下,发现并没有任何的不一样。
但是当你在同一个程序里面,哪怕是不同的界面,如果你打开一个文件选择或保存对话框的时候,用GetCurrentDirectory函数的这种方法就不行了。因为当前目录被你的文件选择或保存对话框修改了。
这时用GetModuleFileName函数的方法就很有必要了,但要注意的是GetModuleFileName得到的应用路径是包括EXE名的,所以EXE名还是要去掉的。(前提是database.ini和EXE文件是在同个目录下)
得到应用程序目录的函数是: GetModuleFileName
用法都很简单,不知道就看MSDN吧。
我先用这2个函数分别写一个实现同一个功能的函数。
以下是代码片段: // 函 数 名:ReadIni // 功能描述:读配置文件database.ini(用来配置oracle数据库的用户名、密码和服务名的),成功返回true,否则返回false // 输入参数:void // 输出参数:bool // 创建日期:2006-5-25 // 修改日期:2006-5-25 // 作 者:joinclear // 附加说明:无 bool ReadIni() { char databuf[1024]; char filepath[1024]; memset(databuf,0,1024); memset(filepath,0,1024); GetCurrentDirectory(1024,filepath); strcat(filepath ,"//" ); strcat(filepath ,"database.ini" ); CFile file; if ( !file.Open(filepath,CFile::modeRead)) { return false; } file.Close(); memset(databuf,0,1024); GetPrivateProfileString("oracle","user id","",databuf,1024,filepath); m_sUserId = databuf; memset(databuf,0,1024); GetPrivateProfileString("oracle","password","",databuf,1024,filepath); m_sPassword = databuf; memset(databuf,0,1024); GetPrivateProfileString("oracle","data source","",databuf,1024,filepath); m_sDataSource = databuf; memset(databuf,0,1024); memset(filepath,0,1024); return true; } bool ReadIni() { char databuf[1024]; CString sPath; GetModuleFileName(NULL,sPath.GetBufferSetLength(1023),1024); sPath.ReleaseBuffer(); int nPos; nPos = sPath.ReverseFind(’//’); sPath = sPath.Left(nPos); CString str = sPath + "//database.ini"; char filepath[1024]; strcpy(filepath,(LPSTR)(LPCTSTR)str); CFile file; if ( !file.Open(filepath,CFile::modeRead)) { return false; } file.Close(); memset(databuf,0,1024); GetPrivateProfileString("oracle","user id","",databuf,1024,filepath); m_sUserId = databuf; memset(databuf,0,1024); GetPrivateProfileString("oracle","password","",databuf,1024,filepath); m_sPassword = databuf; memset(databuf,0,1024); GetPrivateProfileString("oracle","data source","",databuf,1024,filepath); m_sDataSource = databuf; memset(databuf,0,1024); memset(filepath,0,1024); return true; } |
但是当你在同一个程序里面,哪怕是不同的界面,如果你打开一个文件选择或保存对话框的时候,用GetCurrentDirectory函数的这种方法就不行了。因为当前目录被你的文件选择或保存对话框修改了。
这时用GetModuleFileName函数的方法就很有必要了,但要注意的是GetModuleFileName得到的应用路径是包括EXE名的,所以EXE名还是要去掉的。(前提是database.ini和EXE文件是在同个目录下)
相关文章推荐
- VC得到当前目录与得到应用程序目录的一个应用
- VC下写入txt的一点总结&&如何获得当前应用程序的目录&&获得系统日期时间
- 获取应用程序的当前目录(VC)
- VC++中如何获得一个当前进程的运行目录
- VC新建目录CreateDirectory、得到exe存放路径GetModuleFileName、获取当前目录GetCurrentDirectory、CFileDialog设置打开默认路径
- 若要使他人能够在远程计算机上查看此特定错误消息的详细信息,请在位于当前 Web 应用程序根目录下的“web.config”配置文件中创建一个 <customErrors> 标记。然后应将此 <cust
- VC++中如何获得一个当前进程的运行目录
- js得到当前web应用的根目录
- c#获取应用程序目录,C#中得到程序当前工作目录和执行目录方法大全
- 编写一个应用程序,首先用getcwd函数取得当前工作目录,然后在当前工作目录下,利用mkdir函数创
- VC新建目录CreateDirectory、得到exe存放路径GetModuleFileName、获取当前目录GetCurrentDirectory、CFileDialog设置打开默认路径
- c#获取应用程序目录,C#中得到程序当前工作目录和执行目录方法大全
- SharePoint 2013 如何获取当前站点对应的“应用目录”中的“适用于SharePoint的应用程序”列表
- vc得到当前目录和系统目录,从字符串中提取数字
- 【Android应用开发】-(10)一个应用程序开发的蜕变过程
- C#中得到程序当前工作目录和执行目录的五种方法
- 得到文件的大小和目录的路径(控制台应用程序)
- GetModuleFileName函数得到当前应用程序路径
- Android应用开发———Android应用程序工程的目录结构