开发Windows项目有二种方式:非服务和服务两种。两者,服务类型的程序的执行权限比较高。最近开发非服务类型的项目,调用c++的open函数创建日志文件,原以为是十拿九稳的事,结果出于意料,是NULL值。在排除路径参数、所在磁盘空间内存不足、文件被占用等情况下。最后将问题定位执行权限上。如果能够找到路径绕开权限问题,该是多么幸福的事啊!得高人指点,如愿以偿,下面我带大家看看。
1、文档
Here's a custom wrapper function around the SHGetFolderPath API to help you get any of the standard folders for all or the currently logged Windows user.
2、示例(DataRoot就是All User可以访问,对文件进行读写操作权限的目录)
char commonAppDataPath[MAX_PATH];
// [Current User]\My Documents
0: specialFolder := CSIDL_PERSONAL;
// All Users\Application Data
1: specialFolder := CSIDL_COMMON_APPDATA;
// [User Specific]\Application Data
2: specialFolder := CSIDL_LOCAL_APPDATA;
// Program Files
3: specialFolder := CSIDL_PROGRAM_FILES;
// All Users\Documents
4: specialFolder := CSIDL_COMMON_DOCUMENTS;
BOOL re = SHGetSpecialFolderPathA(NULL, commonAppDataPath, CSIDL_COMMON_APPDATA, FALSE);
string DataRoot = (string)commonAppDataPath + "\\Company Name\\Product Name";
参考链接:
(1) https://www.2cto.com/kf/201203/123234.html
(2) https://www.thoughtco.com/store-user-and-application-data-in-the-correct-location-1058164
本文探讨了在Windows开发中遇到的文件创建权限问题,并提供了解决方案。通过使用SHGetFolderPathAPI获取特殊目录路径,如AllUsersApplicationData,以绕过权限限制,实现跨用户读写操作。
1099

被折叠的 条评论
为什么被折叠?



