Download Reference Manual
The Developer's Library for D
About Wiki Forums Source Search Contact

Getting the user's home directory

Moderators: larsivi kris

Posted: 08/19/07 13:53:27

Hi,

for my application I need a place to save configuration files and such. The user's home directory would be the most sensible place for this.

I understand I could get the home directory using the facilities in tango.stdc.posix.unistd, but they seem to work only on Linux and Darwin, as the type declarations in tango.stdc.posix.sys.types are versioned out for all other systems.

I also understand that I could get the user's "Documents and Settings" directory via the Windows API, but if I'm not mistaken Tango currently doesn't provide access to the Windows API as Phobos does.

What can I do to get a directory in which I can safely write my files?

Thanks, Sebastian

Author Message

Posted: 08/19/07 20:19:44

there's some code to get the user home on linux etc, but nothing for Win32 at this time. I guess we should add that somewhere :)

Posted: 08/21/07 13:59:15

That would be great. I guess I'll just use the program directory in the meantime.

Thanks, Sebastian

Posted: 09/02/07 00:04:38

also, this might be of assistance: http://www.dsource.org/projects/tango.posix/browser/trunk/utils/Tilde.d

Posted: 09/07/07 11:15:10

Thanks, but this comment

103 	    else version(Win32)
104 	    {
105 	        // TODO: Put here real windows implementation.

seems to tell me it's not implemented for Windows yet. If I grasp things right, I would need to access some Windows API function in order to get the "Documents and Settings" folder on Windows. Is there any way to do this?

Thanks, Sebastian

Posted: 09/07/07 11:40:30

A proper Windows implementation may be somewhat convoluted, depending on how far back you want to have your support. I believe Windows 95 and similar had fun localizing the names of this place without a proper API to get it in a neutral way. I think this is in place now, though.

Posted: 09/08/07 17:46:44

Windows 2000 and up would be completely sufficient in terms of backward compatibility, and I think I could locate the MS documentation that tells me how to do it using the Windows API.

Rather than that, my question is: Is there a way I can use the Windows API with my DMD/tango combination? Is there a binding to this API other than the one that comes with Phobos?

Thanks, Sebastian

Posted: 09/08/07 18:02:41

To use the Win32 API in a D module, you'd need to import tango.sys.win32.UserGdi?

If the function definition is not there, please add it and create patch, or just tell us which declarations need to be added. After that, the hard part is figuring out where this HomeDirectory? module should live :)

Posted: 09/08/07 18:22:13

Thanks, I hadn't known about that module. :)

I'll try it as soon as I get a chance.

Posted: 09/20/07 18:11:02

You want:

http://msdn2.microsoft.com/en-us/library/ms647764.aspx

And link with shell32.lib

Note you should use: CSIDL_APPDATA or CSIDL_LOCAL_APPDATA otherwise you'll probably find your program won't work on vista, as they've moved everything round again.

Dunno how you'll get hold of the appropriate GUIDs via tango though. I'm betting you'll probably need to define them yourself.

Posted: 09/21/07 04:39:43 -- Modified: 09/21/07 04:41:56 by
yidabu

here is the complete example to get directory on Windows, use DFL to compatible between Phobos and Tango, Tango seems lack some functionality on Windows:

import dfl.all;

pragma(lib, "shell32.lib");
pragma(lib, "dfl.lib"); //use dfl to compatible between Phobos and Tango

//from win32.windef
const size_t MAX_PATH = 260;

// from win32.shlobj
extern(Windows) int SHGetSpecialFolderPathA(void*,char*,int, int);
alias SHGetSpecialFolderPathA SHGetSpecialFolderPath;
enum {
	CSIDL_DESKTOP = 0,
	CSIDL_INTERNET,
	CSIDL_PROGRAMS,
	CSIDL_CONTROLS,
	CSIDL_PRINTERS,
	CSIDL_PERSONAL,
	CSIDL_FAVORITES,
	CSIDL_STARTUP,
	CSIDL_RECENT,
	CSIDL_SENDTO,
	CSIDL_BITBUCKET,
	CSIDL_STARTMENU, // = 11
	CSIDL_DESKTOPDIRECTORY = 16,
	CSIDL_DRIVES,
	CSIDL_NETWORK,
	CSIDL_NETHOOD,
	CSIDL_FONTS,
	CSIDL_TEMPLATES,
	CSIDL_COMMON_STARTMENU,
	CSIDL_COMMON_PROGRAMS,
	CSIDL_COMMON_STARTUP,
	CSIDL_COMMON_DESKTOPDIRECTORY,
	CSIDL_APPDATA,
	CSIDL_PRINTHOOD,
	CSIDL_LOCAL_APPDATA,
	CSIDL_ALTSTARTUP,
	CSIDL_COMMON_ALTSTARTUP,
	CSIDL_COMMON_FAVORITES,
	CSIDL_INTERNET_CACHE,
	CSIDL_COOKIES,
	CSIDL_HISTORY,
	CSIDL_COMMON_APPDATA,
	CSIDL_WINDOWS,
	CSIDL_SYSTEM,
	CSIDL_PROGRAM_FILES,
	CSIDL_MYPICTURES,
	CSIDL_PROFILE,
	CSIDL_SYSTEMX86,
	CSIDL_PROGRAM_FILESX86,
	CSIDL_PROGRAM_FILES_COMMON,
	CSIDL_PROGRAM_FILES_COMMONX86,
	CSIDL_COMMON_TEMPLATES,
	CSIDL_COMMON_DOCUMENTS,
	CSIDL_COMMON_ADMINTOOLS,
	CSIDL_ADMINTOOLS,
	CSIDL_CONNECTIONS, // =49
	CSIDL_COMMON_MUSIC = 53,
	CSIDL_COMMON_PICTURES,
	CSIDL_COMMON_VIDEO,
	CSIDL_RESOURCES,
	CSIDL_RESOURCES_LOCALIZED,
	CSIDL_COMMON_OEM_LINKS,
	CSIDL_CDBURN_AREA, // = 59
	CSIDL_COMPUTERSNEARME = 61,
	CSIDL_FLAG_DONT_VERIFY = 0x4000,
	CSIDL_FLAG_CREATE = 0x8000,
	CSIDL_FLAG_MASK = 0xFF00
}


//
char[] getPath(int csidl)
{
	char[] result;
	char[][int] acsidl;	
	
	try
	{
		version(Unicode)
		{
			alias wchar mychar;		//my char
			alias dfl.internal.utf.fromUnicodez fromz;

		}
		else
		{
			alias char mychar;
			alias dfl.internal.utf.fromAnsiz fromz;
		}
		
		mychar[]  spath = new mychar[MAX_PATH];		//string of path
		
		if ( !SHGetSpecialFolderPath(null, spath.ptr ,csidl,true) ) return null;	
		result = fromz(spath.ptr);
	}
	catch(Exception e)
	{
		//printf("getPath catch %.*s\n",e.msg);
		return null;
	}
	return result;
}
//

void main()
{
    char[] s = getPath(CSIDL_APPDATA);
    msgBox(s);
    //C:\Documents and Settings\username\Application Data
}

Posted: 09/27/07 16:02:03

Can you create a ticket for this, please, so we don't lose track of it?

Posted: 06/04/08 16:33:16

Is this still on the todo list?

I know that list is rather long, but I know very little about the windows api, so I won't volunteer.

Posted: 06/04/08 17:03:52

If you create a ticket, it may still be on the TODO list :)

Posted: 06/05/08 11:20:17

http://www.dsource.org/projects/tango/ticket/1131