0

I am not exactly sure if the title captures what exactly I want to ask so here it is:

I just made my first desktop application that uses a XML file to store data. The XML File is stored somewhere in C:/Users/myname/Documents/adjsklf/asdjfklasd/.... on my own machine. How do I go about creating this file in a specific location C:/Users/theirname/Documents/myAppName/data.xml? Or more specifically, how do I get the "theirname" file name? For machines that have multiple users, how do I get the filename that belongs to the user who actually is using the app?

Also, when I first started this application, I wasn't really thinking of deploying this application. So I made an explicit constructor of a class, dataManipulation, that manipulates all the data in my XML file. What happens is that when my program runs, it executes MainWindow, which at the same time constructs my dataManipulation object with the path I want. However, since now I have a few friends who want to try out this app, I need to be able to detect whether the file exists first using the path I mentioned earlier. What's the best way to achieve that?

Thank you so much!

KitKat
  • 11
  • 5
  • Look at `SHGetKnownFolderPath`. Do you really need to store it in Documents rather than AppData? – Simple Feb 10 '14 at 15:27
  • @Simple, actually, now that I think about it, AppData would be better! Would SHGetKnownFolderPath work for AppData as well? – KitKat Feb 10 '14 at 15:34
  • You can use `FOLDERID_RoamingAppData` or `FOLDERID_LocalAppData` as the first parameter. – Simple Feb 10 '14 at 15:41

1 Answers1

2

Use QStandardPaths::writableLocation.

There are many ways to perform file opening checks. If you want to do it in Qt-like way, take a look at QFile and do similarily. In constructor it only sets the path, but the file is opened only in QFile::open, and its return value indicates whether it was successfully opened. You can create init() method in your data manipulation class, check its return value and show a message to user if needed.

Pavel Strakhov
  • 39,123
  • 5
  • 88
  • 127