Solved

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

Posted on 2001-09-04
6
1,098 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
Comment Utility
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
Comment Utility
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
Comment Utility
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
Highfive Gives IT Their Time Back

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

 
LVL 3

Expert Comment

by:Slavak
Comment Utility
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
Comment Utility
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
Comment Utility
"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

What Is Threat Intelligence?

Threat intelligence is often discussed, but rarely understood. Starting with a precise definition, along with clear business goals, is essential.

Join & Write a Comment

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…
Have you ever had your Delphi form/application just hanging while waiting for data to load? This is the article to read if you want to learn some things about adding threads for data loading in the background. First, I'll setup a general applica…
Get a first impression of how PRTG looks and learn how it works.   This video is a short introduction to PRTG, as an initial overview or as a quick start for new PRTG users.
Here's a very brief overview of the methods PRTG Network Monitor (https://www.paessler.com/prtg) offers for monitoring bandwidth, to help you decide which methods you´d like to investigate in more detail.  The methods are covered in more detail in o…

728 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

Need Help in Real-Time?

Connect with top rated Experts

10 Experts available now in Live!

Get 1:1 Help Now