Lenguaje de programación Borland Delphi
Con estas funciones se podrán obtener las carpetas de: Windows, Sistema, Archivos de programa, archivos comunes y directorio temporal:
...
function obtenerDirectorioWindows : TFileName;
var
WinDir: array [0..MAX_PATH-1] of char;
begin
SetString(Result, WinDir, GetWindowsDirectory(WinDir, MAX_PATH));
if Result = '' then
raise Exception.Create(SysErrorMessage(GetLastError));
end;
function obtenerDirectorioSistema : TFileName;
var
SysDir: array [0..MAX_PATH-1] of char;
begin
SetString(Result, SysDir, GetSystemDirectory(SysDir, MAX_PATH));
if Result = '' then
raise Exception.Create(SysErrorMessage(GetLastError));
end;
function obtenerDirectorioProgramas : TFileName;
begin
with TRegistry.Create do
try
RootKey := HKEY_LOCAL_MACHINE;
OpenKey('\SOFTWARE\Microsoft\Windows\CurrentVersion', false);
result := ReadString('ProgramFilesDir');
CloseKey;
finally
free;
end;
end;
function obtenerDirectorioArchivosComunes : TFileName;
begin
with TRegistry.Create do
try
RootKey := HKEY_LOCAL_MACHINE;
OpenKey('\SOFTWARE\Microsoft\Windows\CurrentVersion', false);
result := ReadString('CommonFilesDir');
CloseKey;
finally
free;
end;
end;
function obtenerDirectorioTemporal : TFileName;
var
TmpDir: array [0..MAX_PATH-1] of char;
begin
try
SetString(Result, TmpDir, GetTempPath(MAX_PATH, TmpDir));
if not DirectoryExists(Result) then
if not CreateDirectory(PChar(Result), nil) then begin
Result := IncludeTrailingBackslash(obtenerDirectorioWindows) + 'TEMP';
if not DirectoryExists(Result) then
if not CreateDirectory(Pointer(Result), nil) then begin
Result := ExtractFileDrive(Result) + '\TEMP';
if not DirectoryExists(Result) then
if not CreateDirectory(Pointer(Result), nil) then begin
Result := ExtractFileDrive(Result) + '\TMP';
if not DirectoryExists(Result) then
if not CreateDirectory(Pointer(Result), nil) then begin
raise Exception.Create(SysErrorMessage(GetLastError));
end;
end;
end;
end;
except
Result := '';
raise;
end;
end;
Ejemplo de utilización:
showmessage('La carpeta de Windows está en: ' + obtenerDirectorioWindows;
Publicado el: 2005-01-04