Application Data Dir


DevX Home    Today's Headlines   Articles Archive   Tip Bank   Forums   

Results 1 to 14 of 14

Thread: Application Data Dir

  1. #1
    Join Date
    Nov 2004
    Location
    Lincoln, England
    Posts
    100

    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.

  2. #2
    Join Date
    Nov 2003
    Posts
    4,118
    getcwd()
    Danny Kalev

  3. #3
    Join Date
    Nov 2004
    Location
    Lincoln, England
    Posts
    100
    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.

  4. #4
    Join Date
    Apr 2006
    Location
    Zagreb, Croatia
    Posts
    152
    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;

    }
    }

  5. #5
    Join Date
    Dec 2004
    Location
    San Bernardino County, California
    Posts
    1,468
    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]

  6. #6
    Join Date
    May 2006
    Posts
    176
    I think one of simpler solution is:
    Code:
    const char * const app_data_dir = getenv("APPDATA");
    I hope this helps.

  7. #7
    Join Date
    May 2006
    Posts
    48
    You can also search through the directories like C:\ to locate the directory yourself

  8. #8
    Join Date
    Nov 2003
    Posts
    4,118
    I agree with viorel. Environment variables are the simplest solution to this programming task.
    Danny Kalev

  9. #9
    Join Date
    Nov 2004
    Location
    Lincoln, England
    Posts
    100
    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.

  10. #10
    Join Date
    May 2006
    Posts
    48
    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 :)

  11. #11
    Join Date
    Nov 2004
    Location
    Lincoln, England
    Posts
    100
    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.

  12. #12
    Join Date
    Nov 2003
    Posts
    4,118
    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

  13. #13
    Join Date
    Nov 2004
    Location
    Lincoln, England
    Posts
    100
    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.

  14. #14
    Join Date
    Dec 2003
    Posts
    3,366
    I think you can type 'set' in a command prompt to see the environment variables on 98 (or any version??)

Similar Threads

  1. Replies: 1
    Last Post: 12-01-2002, 06:42 AM
  2. Inventory Management Application Using VB
    By Krishna in forum vb.announcements
    Replies: 0
    Last Post: 11-25-2002, 11:03 PM
  3. Data binding vs n-tier
    By Kathleen Dollard in forum .NET
    Replies: 14
    Last Post: 05-03-2002, 12:10 AM
  4. Passing Data to another instance of an application
    By Michael Lambino in forum VB Classic
    Replies: 12
    Last Post: 06-14-2001, 02:19 PM
  5. Java application - large data set
    By Chandan in forum Java
    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
  •  
HTML5 Development Center
 
 
FAQ
Latest Articles
Java
.NET
XML
Database
Enterprise
Questions? Contact us.
C++
Web Development
Wireless
Latest Tips
Open Source


   Development Centers

   -- Android Development Center
   -- Cloud Development Project Center
   -- HTML5 Development Center
   -- Windows Mobile Development Center