Solved

How to get the user-home directory in Windows in a Delphi program ?

Posted on 2001-09-04
6
1,206 Views
Last Modified: 2010-05-18
Dear all,
I am writting a Delphi program and would like to get the windows(95/98/NT/2000...) user-home directory in a Delphi program. Such as "c:\windows\profiles\username\","c:\Document and Setting\username\", etc..
Can somebody help me ?

Thanks a lot.
0
Comment
Question by:threshold
  • 3
  • 2
6 Comments
 
LVL 3

Accepted Solution

by:
Slavak earned 50 total points
ID: 6453745
There is no "user-home directory" on Windows. All data that you can see under "c:\windows\profiles\username\" or "c:\Document and Setting\username\" placed there by default, but it can be changed. But you can get path to  specific user directory (like "My documents", "Startup", "SendTo" etc.) by call to SHGetSpecialFolderLocation API function.

Example, getting "My documents":

Uses Shlobj;

procedure TForm1.Button1Click(Sender: TObject);
Var
 PI : PItemIDList;
 A  : array[0..200] of Char;
begin
 SHGetSpecialFolderLocation(Handle, CSIDL_PERSONAL, PI);
 SHGetPathFromIDList(PI, A);
 Caption := A;
end;
 

0
 
LVL 14

Expert Comment

by:DragonSlayer
ID: 6456093
if you are using Win2K, there is an environment variable called USERPROFILE that points to the 'home directory'

use GetEnvironmentVariable to get the value for USERPROFILE
0
 
LVL 3

Expert Comment

by:rondi
ID: 6456199
You could just extend Slavak's suggestion:
delete the last part of the path, so you get the home directory - ie. delete the folder name after the last
backslash

Uses Shlobj, Windows;

function GetPersonalFolder(H: HWND): string;
var
  PI: PItemIDList;
  A: array[0..MAX_PATH-1] of Char;
  s: string;
  i,L: integer;
begin
  SHGetSpecialFolderLocation(H, CSIDL_PERSONAL, PI);
  SHGetPathFromIDList(PI, A);
  s := StrPas(A);
  L := Length(s);
  i := L;
  while s[i] <> '\' do
    i := Pred(i);
  Result := Copy(s,1,L-i);
end;
0
Free Tool: IP Lookup

Get more info about an IP address or domain name, such as organization, abuse contacts and geolocation.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

 
LVL 3

Expert Comment

by:Slavak
ID: 6456924
Rondi, if you want "delete last part of the path" use LastDelimiter function:

procedure TForm1.Button1Click(Sender: TObject);
Var
 PI : PItemIDList;
 A  : array[0..MAX_PATH] of Char;
 N  : Integer;
begin
 SHGetSpecialFolderLocation(Handle, CSIDL_PERSONAL, PI);
 SHGetPathFromIDList(PI, A);
 Caption := Copy(A, 1, LastDelimiter('\', A) - 1);
end;

But this work for default configuration ONLY. For example, right click on "My documents", choose properties. Now you have a dialog with "Target folder location" (I work on Win2K, so it can be another name) and can change the folder location.



0
 
LVL 3

Expert Comment

by:rondi
ID: 6456967
Surely the SHGetSpecialFolderLocation will get the current
folder location even if you change it ??

Yeah, LastDelimiter's definitely better. (workin in D2 :(
0
 
LVL 3

Expert Comment

by:Slavak
ID: 6456984
"Surely the SHGetSpecialFolderLocation will get the current
folder location even if you change it ??"

You can try it, but as I think it 99.99%. :)

0

Featured Post

Networking for the Cloud Era

Join Microsoft and Riverbed for a discussion and demonstration of enhancements to SteelConnect:
-One-click orchestration and cloud connectivity in Azure environments
-Tight integration of SD-WAN and WAN optimization capabilities
-Scalability and resiliency equal to a data center

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

A lot of questions regard threads in Delphi.   One of the more specific questions is how to show progress of the thread.   Updating a progressbar from inside a thread is a mistake. A solution to this would be to send a synchronized message to the…
Introduction The parallel port is a very commonly known port, it was widely used to connect a printer to the PC, if you look at the back of your computer, for those who don't have newer computers, there will be a port with 25 pins and a small print…
A short tutorial showing how to set up an email signature in Outlook on the Web (previously known as OWA). For free email signatures designs, visit https://www.mail-signatures.com/articles/signature-templates/?sts=6651 If you want to manage em…

856 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question