File path specification to Desktop folder regardless of user?

This is a discussion on File path specification to Desktop folder regardless of user? within the Windows Programming forums, part of the Platform Specific Boards category; Is there a file path string to use with fopen() that will create a file in a user's Desktop folder ...

  1. #1
    Registered User
    Join Date
    Apr 2008
    Posts
    4

    File path specification to Desktop folder regardless of user?

    Is there a file path string to use with fopen() that will create a file in a user's Desktop folder no matter which user logs in and runs the program?

    For example something like:
    fopen("C:\home...\folder...\Desktop\file.txt","w")

  2. #2
    Registered User Codeplug's Avatar
    Join Date
    Mar 2003
    Posts
    4,607
    This should get you started:
    SHGetSpecialFolderPath function (Windows)
    CSIDL (Windows) (CSIDL_DESKTOPDIRECTORY)

    gg

  3. #3
    Registered User
    Join Date
    Apr 2008
    Posts
    4
    Quote Originally Posted by Codeplug View Post
    This should get you started:
    SHGetSpecialFolderPath function (Windows)
    CSIDL (Windows) (CSIDL_DESKTOPDIRECTORY)

    gg
    Thanks for the response.

    Coming from a UNIX programming environment the only thing I'm able to get from these references is that it is (may be?) possible to determine the Desktop folder path somehow. But it looks like implementing the information assumes additional background knowledge of Windows programming. So I'm still searching for the answer. A code example would be most helpful.

  4. #4
    Registered User
    Join Date
    Apr 2013
    Posts
    1,166
    Can you use an environment variable such as %HOMEPATH% in a file name? If not, you will have to use something like environ() to get the partial file name path.

  5. #5
    Registered User Codeplug's Avatar
    Join Date
    Mar 2003
    Posts
    4,607
    Code:
    #include<stdio.h>
    #include<windows.h>
    #include<ShlObj.h>
    
    int main()
    {
        char desktop[MAX_PATH] = {0};
    
        if (!SHGetSpecialFolderPathA(0, desktop, CSIDL_DESKTOPDIRECTORY, FALSE))
        {
            printf("SHGetSpecialFolderPath() failed, le=%u\n", GetLastError());
            return 1;
        }
    
        printf("Desktop Path = %s\n", desktop);
        return 0;
    }
    Reading Windows man-pages (MSDN) takes some getting used to. If you don't know a typedef that's being used: Windows Data Types (Windows)

    gg

  6. #6
    Registered User
    Join Date
    Apr 2008
    Posts
    4
    Good stuff! Thanks, Codeplug

    Quote Originally Posted by Codeplug View Post
    Code:
    #include<stdio.h>
    #include<windows.h>
    #include<ShlObj.h>
    
    int main()
    {
        char desktop[MAX_PATH] = {0};
    
        if (!SHGetSpecialFolderPathA(0, desktop, CSIDL_DESKTOPDIRECTORY, FALSE))
        {
            printf("SHGetSpecialFolderPath() failed, le=%u\n", GetLastError());
            return 1;
        }
    
        printf("Desktop Path = %s\n", desktop);
        return 0;
    }
    Reading Windows man-pages (MSDN) takes some getting used to. If you don't know a typedef that's being used: Windows Data Types (Windows)

    gg

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. searching for/creating a file in a user-specified folder
    By bored_guy in forum C Programming
    Replies: 2
    Last Post: 01-13-2010, 04:01 AM
  2. I need a path code to desktop
    By jamietrent in forum C# Programming
    Replies: 2
    Last Post: 09-13-2009, 10:43 AM
  3. (HELP) I need the path for Desktop
    By jamietrent in forum C# Programming
    Replies: 5
    Last Post: 09-12-2009, 08:30 PM
  4. get application data folder path
    By skuallpa in forum C++ Programming
    Replies: 4
    Last Post: 06-12-2009, 03:19 PM
  5. Desktop Path??
    By Unregistered in forum Windows Programming
    Replies: 4
    Last Post: 02-22-2002, 08:29 AM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21