How do I get username and appname for file path
-
How am I able to define a path like "C:/Users/<USER>/AppData/Local/<APPNAME>", for different username and app? How do I set this to automatically get the user and the appname? Thank you.
-
Hi
You can use https://doc.qt.io/qt-5.9/qstandardpaths.html
and QStandardPaths::AppDataLocation -
how about in c++, not using qstandardpath?
-
@LovelyGrace said in How do I get username and appname for file path:
qstandardpath
Why don't you want to use it?
-
im not sure if its gonna work with fsteam. my program looks like this:
fsteam file;
file.open(path); -
@LovelyGrace
hi
Well you can always use
https://doc.qt.io/qt-5/qdir.html#toNativeSeparators -
and https://doc.qt.io/qt-5/qstring.html#toStdString
if
fsteam
does not accept QStrings -
I also assume that, do u have anysuggestion using fstream?
-
@LovelyGrace
yes use QFile.Sry never heard of fstream...my bad, I readfsteam
instead. -
@LovelyGrace said in How do I get username and appname for file path:
do u have any suggestion using fstream?
Hi.
What you mean ?
How it works or ?
http://www.cplusplus.com/doc/tutorial/files/ -
I still like to use fstream but I don't know how am I able to set the path to C:/Users/<USER>/AppData/Local/<APPNAME>", as user and appname is unknown.
-
what I did is like this:
QString appdata = getenv("appdata");
// appdata += "\DoDLog.log";
// fstream stud;
// stud.open(appdata.toStdString().c_str(), ios::app);but it goes to appdata/roaming/DoDLog.log
how can I set to appdata/local/<myapplication>/DoDLog.log -
but it goes to appdata/roaming/DoDLog.log
how can I set to appdata/local/<myapplication>/DoDLog.logDid you look through/try the various entries from https://doc.qt.io/qt-5.9/qstandardpaths.html#StandardLocation-enum ? (I have no idea why you want to use
getenv()
orfstream
or whatever.) E.g. from the example paths returned shown in the table, perhaps:AppConfigLocation "C:/Users/<USER>/AppData/Local/<APPNAME>", "C:/ProgramData/<APPNAME>"
-
There's an API for this (If your application is Windows-only). Dont know if it works in your case but I would try this:
https://stackoverflow.com/questions/11587426/get-current-username-in-c-on-windowsBut I guess QStandardPath and friends are better ways...
@LovelyGrace said in How do I get username and appname for file path:
as user and appname is unknown.
Why is the current user name unknown? If your app is running on a machine, there is always an active (logged-in) user. If you are planning to read / write data to other user's homedir, you need to know their names or you use every subfolder in "C:\Users...."
Why are you using QString, but dont want to use QFile or QStandardPath?
Btw, like @JonB already said, QStandardPath provides PathVariables that will lead to your destination:
DataLocation "C:/Users/<USER>/AppData/Local/<APPNAME>", "C:/ProgramData/<APPNAME>"
or
AppConfigLocation "C:/Users/<USER>/AppData/Local/<APPNAME>", "C:/ProgramData/<APPNAME>"