Solved

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

Posted on 2001-09-04
6
1,293 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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

Technology Partners: 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!

Question has a verified solution.

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

The uses clause is one of those things that just tends to grow and grow. Most of the time this is in the main form, as it's from this form that all others are called. If you have a big application (including many forms), the uses clause in the in…
In this tutorial I will show you how to use the Windows Speech API in Delphi. I will only cover basic functions such as text to speech and controlling the speed of the speech. SAPI Installation First you need to install the SAPI type library, th…
Finds all prime numbers in a range requested and places them in a public primes() array. I've demostrated a template size of 30 (2 * 3 * 5) but larger templates can be built such 210  (2 * 3 * 5 * 7) or 2310  (2 * 3 * 5 * 7 * 11). The larger templa…
Exchange organizations may use the Journaling Agent of the Transport Service to archive messages going through Exchange. However, if the Transport Service is integrated with some email content management application (such as an antispam), the admini…

732 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