-
Application Data Dir
Hey guys,
I'm making a program (I won't bore you with details) that needs to open a file, however the file that needs opening is in "C:\D&S\Username\Application Data\Program" (this file was created by a different program I didn't make), now this is easy enough as a personal program, I just point to my application data directory. However when the time comes to release it, other users will not have my username, and this leaves me stuck (until now hopefully).
How do I find the current users Application Data directory without using .net/VC++ code (I'm using dev-cpp, and mscorlib.dll doesn't like dev-cpp)? I tried the explorer-esque "%userprofile%\Application Data" method, obviously to no avail.
My code so far (simplified):
Code:
#include <Other Header Files>
#include <fstream>
#include <string>
using namespace std;
...
void myfunction()
{
//Variables
fstream myfile;
...
//Open File and make sure it is open before proceeding
myfile.open("c:\\documents and settings\\owner\\application data\\program\\config.cfg");
if (!myfile.is_open())
exit(1);
...
}
Thanks in advance,
-- El Sid
Programming today is a race between software engineers striving to build bigger and better idiot-proof programs, and the Universe trying to produce bigger and better idiots. So far, the Universe is winning.
-
-
No no no no, I think I phrased my question badly. I need to know the User's "Application Data" Directory a la:

I mean "Application Data" as in the directory in Documents and settings where Programs can store config files etc on a per user basis, not the directory the app is in.
Sorry :o
Programming today is a race between software engineers striving to build bigger and better idiot-proof programs, and the Universe trying to produce bigger and better idiots. So far, the Universe is winning.
-
This will give you some virtual directories but Application Data is missing in my case. Anyway it's worth investingating
int folder;
switch (cmbSpecial->ItemIndex)
{
case 0:
folder = CSIDL_BITBUCKET;
break;
case 1:
folder = CSIDL_CONTROLS;
break;
case 2:
folder = CSIDL_DESKTOP;
break;
case 3:
folder = CSIDL_DRIVES;
break;
case 4:
folder = CSIDL_FONTS;
break;
case 5:
folder = CSIDL_NETHOOD;
break;
case 6:
folder = CSIDL_PRINTERS;
break;
case 7:
folder = CSIDL_PROGRAMS;
break;
case 8:
folder = CSIDL_RECENT;
break;
case 9:
folder = CSIDL_SENDTO;
break;
case 10:
folder = CSIDL_STARTMENU;
break;
case 11:
folder = CSIDL_STARTUP;
break;
case 12:
folder = CSIDL_TEMPLATES;
break;
default:
folder = -1;
break;
}
if (folder >= 0)
{
LPITEMIDLIST pidl;
if (SHGetSpecialFolderLocation(Handle,folder,&pidl) == NOERROR)
{
char buf[MAX_PATH];
SHGetPathFromIDList(pidl,buf);
// Label1->Caption = buf;
DirectoryListBox1->Directory = buf;
FileListBox1->Directory = DirectoryListBox1->Directory;
}
}
-
Why not ask the user to browse to the directory and, in effect, TELL your program what is the application/data directory? [just ask once and save the data in the Registry or in a "startup" file known to your executable]
-
I think one of simpler solution is:
Code:
const char * const app_data_dir = getenv("APPDATA");
I hope this helps.
-
You can also search through the directories like C:\ to locate the directory yourself
-
I agree with viorel. Environment variables are the simplest solution to this programming task.
Danny Kalev
-
OK thanks for the replies,
I'll try Viorel's way, although I'm looking for compatability with Windows 98, is anyone in the know about whether Windows 98 has this particular Env. Variable?
Oh, and am I going to need a particular header for this?
----------
I've got another (fairly foolproof) way of doing it if Viorel's way won't work involving obtaining the Windows Version and Username, although that way is fairly long winded compared to Viorel's.
Again, thanks for the replies, much appreciated :)
Programming today is a race between software engineers striving to build bigger and better idiot-proof programs, and the Universe trying to produce bigger and better idiots. So far, the Universe is winning.
-
If you are looking to get the windows user name you can use something like this:
char UserName[100];
DWORD sz=sizeof(UserName);
BOOL result = GetUserName(UserName, &sz);
this will return the user name to variable UserName
include the header <Winbase.h>
Similarly, you can use the function GetComputerName
You can search on MSDN or either one of these functions and it'll give you whole bunch of other similar functions as well :)
-
I've got the Username version all revved up and ready to go, thanks anyway :), however the procedure is much more complicated than the getenv() function, however I'm unaware whether the particular env. variable I'm after (APPDATA) is available in Win98.
Thanks for your time though :)
Programming today is a race between software engineers striving to build bigger and better idiot-proof programs, and the Universe trying to produce bigger and better idiots. So far, the Universe is winning.
-
You can check all the evnironment variables that are defines for a process programmatically. If Windows 98 has this variable (perhaps under a different name), you need not look any further. Alternatively, the registry is another option but you may find that very old vesrions of Windows (3.11, and perhaps 95) don't suppurt it either. Is an problem for your application?
Danny Kalev
-
Win95 and earlier are not a problem, however I'm trying to put in some functions that are specific for Win98 and involve files in the user's application data directory.
If no-one can answer, I'll use my crude version for the time being, and see if I can get hold of someone who still has a Windows 98 Machine :)
Programming today is a race between software engineers striving to build bigger and better idiot-proof programs, and the Universe trying to produce bigger and better idiots. So far, the Universe is winning.
-
I think you can type 'set' in a command prompt to see the environment variables on 98 (or any version??)
Similar Threads
-
By krishna in forum VB Classic
Replies: 1
Last Post: 12-01-2002, 06:42 AM
-
By Krishna in forum vb.announcements
Replies: 0
Last Post: 11-25-2002, 11:03 PM
-
By Kathleen Dollard in forum .NET
Replies: 14
Last Post: 05-03-2002, 12:10 AM
-
By Michael Lambino in forum VB Classic
Replies: 12
Last Post: 06-14-2001, 02:19 PM
-
Replies: 3
Last Post: 03-01-2001, 04:24 AM
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
Forum Rules
|
Top DevX Stories
Easy Web Services with SQL Server 2005 HTTP Endpoints
JavaOne 2005: Java Platform Roadmap Focuses on Ease of Development, Sun Focuses on the "Free" in F.O.S.S.
Wed Yourself to UML with the Power of Associations
Microsoft to Add AJAX Capabilities to ASP.NET
IBM's Cloudscape Versus MySQL
|
Bookmarks