如何将特定Windows用户路径替换为正在运行的用户
How to Replace Specific Windows Username Path with The User Running
所以我要做的是让路径中的用户名是它运行的用户名
例如,如果我正在运行这个程序的Windows用户名为Bob,那么我想在Bob的桌面上创建这个文件。而不是代码中设置的那个
我已经尽力解释清楚了,谢谢。
#include <fstream>
#include <ostream>
using namespace std;
int main()
{
std::ofstream fs("C:\Users/SAMPLE_USERNAME/Desktop/omg it works.txt"); //makes the text file
fs<<"Testing this thing!"; //writes to the text file
fs.close();
return 0;
}
我使用Code::Blocks,如果有帮助的话。
既然你提到了一个Windows用户,我假设你我想要一个Windows的解决方案。可以获取用户名使用Win32函数SHGetKnownFolderPath
。但是,请注意如果你想让你的应用程序支持Windows XP,那么你可以这么做必须使用旧的(已弃用的)函数SHGetFolderPath
。你可以使用这个示例:
wchar_t *pszDesktopFolderPath;
if(S_OK == SHGetKnownFolderPath(FOLDERID_Desktop, KF_FLAG_DEFAULT, NULL, pszDesktopFolderPath))
{
// pszDesktopFolderPath now points to the path to desktop folder
...
// After you are done with it, delete the buffer for the path like this
CoTaskMemFree(pszDesktopFolderPath);
}
这里是一个使用SHGetFolderPath
的例子:
LPTSTR pszDesktopFolderPath[MAX_PATH];
if(S_OK == SHGetFolderPathA(NULL, CSIDL_DESKTOPDIRECTORY, NULL, SHGFP_TYPE_CURRENT, pszDesktopFolderPath))
{
// pszDesktopFolderPath now points to the path to desktop folder, no need to manually release memory
}
请注意,第三个参数表示用户的令牌试图获取桌面位置。当你的应用程序是由将权限从标准用户帐户提升到管理员帐户,上述函数返回的路径是到桌面的路径管理员用户的。如果您想获得标准用户的路径,您可以必须获取该用户的令牌并将其作为第三个参数传递。
相关文章:
- 为什么即使使用-cudart-static进行编译,库用户仍然需要链接到cuda运行时
- 如何根据用户在C++中的输入运行不同数量的 for 循环
- 我可以根据用户输入在运行时生成"循环"吗?
- 使用 (cin) 用户输入将其粘贴到 std::system 中,并在另一个终端中运行带有输入的命令
- 是否可以在运行时调用用于选择要调用的用户定义文本的逻辑?
- C++ .我正在尝试通过使用开关内的数组来获取用户输入,但是当我运行代码时,它显示分段错误?
- 如何让程序只运行用户输入的号码?
- 在运行时使用用户定义的文本
- 是否有任何 C 函数或 API 来获取当前登录用户下运行的进程列表
- 指定在用户固定我的应用程序时用于运行应用程序的命令参数
- 如何让访问www-data用户运行使用 /dev /ttys1的流程
- 如何在背景中运行vtkrenderwindow而不向用户显示
- 根据用户输入或条件重新运行 c++ 程序
- 以下代码运行后,如何在程序再次运行之前清除用户声明的变量 empName
- 根据用户的输入在运行时创建对象
- 作为不同用户运行的两个exe如何使用COM(组件对象模型)进行通信
- Ubuntu,libftdi特权,非root用户运行程序时seg错误
- 直接阻止用户运行程序
- 以另一个用户运行程序
- 尝试执行"cc1"时 gcc 错误:execvp:使用非 root 用户运行时没有这样的文件或目录