C ++-如何获取用户文件夹? [英] C++ - How to get the user folder?

查看:50
本文介绍了C ++-如何获取用户文件夹?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的程序有问题...

I'm having a little problem with my program...

我必须使用应用程序数据创建一个文件,但是我不知道如何访问%USER%%APPDATA%目录...

I have to create a file with the application data, but I don't know how to access the %USER% or %APPDATA% directories...

我尝试使用应用程序文件夹,但是如果我将应用程序安装在 D:\ Program Files \(Organization)\(APPName)中,则无法在此目录中写入新文件,如果我没有管理员权限,只能阅读或修改...

I've tried to use the application folder, but if I install the application in D:\Program Files\(Organization)\(APPName) I cannot write new files in this directory, I can just read or modify if I don't have admin privileges...

因此,问题是:如何使用应用程序访问%USER%文件夹或获取ADMIN特权...PS .:我在C ++ Builder中使用VCL

So, the question is: How to access the %USER% folder or Get ADMIN privileges with the application... PS.: I'm using VCL in C++ Builder

推荐答案

一种经典的方法是使用getenv读取环境变量:

One classic way is to read the environment variables using getenv:

char *user = getenv("USER");
char *appdata = getenv("APPDATA");

关于用户权限和在这些位置执行文件读/写/创建,您当然可以在应用程序运行所在的用户文件夹中.在其他文件夹中,您需要以目标用户或管理员身份运行它.另外,在程序文件"或程序文件(x86)"中安装了应用程序后,系统将不允许您在此处进行写操作.在"trustedinstaller"用户凭据下执行安装,并在安装过程中为当前用户"或所有用户"设置最终用户权限.

Regarding user rights and performing file read/write/create in these locations, you certainly can in the user folder the application is running as. In other folders, you'll need to run it as either the target user or administrator. Also, after installing an application in Program Files or Program Files (x86), the system will not allow you to write there. Installations are performed under the 'trustedinstaller' user credentials and the final user rights are set during installation for 'current user' or 'all users'.

希望这会有所帮助.

这篇关于C ++-如何获取用户文件夹?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆