Solved

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

Posted on 2001-09-04
6
1,249 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
Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
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

Free Tool: Path Explorer

An intuitive utility to help find the CSS path to UI elements on a webpage. These paths are used frequently in a variety of front-end development and QA automation tasks.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

Suggested Solutions

Objective: - This article will help user in how to convert their numeric value become words. How to use 1. You can copy this code in your Unit as function 2. than you can perform your function by type this code The Code   (CODE) The Im…
Introduction Raise your hands if you were as upset with FireMonkey as I was when I discovered that there was no TListview.  I use TListView in almost all of my applications I've written, and I was not going to compromise by resorting to TStringGrid…
How to Install VMware Tools in Red Hat Enterprise Linux 6.4 (RHEL 6.4) Step-by-Step Tutorial

749 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