?
Solved

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

Posted on 2001-09-04
6
Medium Priority
?
1,436 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 150 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
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!

 
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

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…
In this brief tutorial Pawel from AdRem Software explains how you can quickly find out which services are running on your network, or what are the IP addresses of servers responsible for each service. Software used is freeware NetCrunch Tools (https…
In this video, Percona Solution Engineer Rick Golba discuss how (and why) you implement high availability in a database environment. To discuss how Percona Consulting can help with your design and architecture needs for your database and infrastr…
Suggested Courses

801 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