鱼C论坛

 找回密码
 立即注册
查看: 2336|回复: 1

[技术交流] delphi得到当前目录的几个方法

[复制链接]
发表于 2012-4-4 10:46:52 | 显示全部楼层 |阅读模式

马上注册,结交更多好友,享用更多功能^_^

您需要 登录 才可以下载或查看,没有账号?立即注册

x
{ Getting the Windows Directory }

function GetWinDir: string;
var
dir: array [0..MAX_PATH] of Char;
begin
GetWindowsDirectory(dir, MAX_PATH);
Result := StrPas(dir);
end;

// or:

function WindowsDirectory: string;
var
WinDir: PChar;
begin
WinDir := StrAlloc(MAX_PATH);
GetWindowsDirectory(WinDir, MAX_PATH);
Result := string(WinDir);
if Result[Length(Result)] <> '\' then
Result := Result + '\';
StrDispose(WinDir);
end;

// or:

function GetWindowsDirectory(var S: String): Boolean;
var
Len: Integer;
begin
Len := Windows.GetWindowsDirectory(nil, 0);
if Len > 0 then
begin
SetLength(S, Len);
Len := Windows.GetWindowsDirectory(PChar(S), Len);
SetLength(S, Len);
Result := Len > 0;
end else
Result := False;
end;

{ Getting the System Directory }

function SystemDir: string;
var
dir: array [0..MAX_PATH] of Char;
begin
GetSystemDirectory(dir, MAX_PATH);
Result := StrPas(dir);
end;

// or:

function SystemDirectory: string;
var
SysDir: PChar;
begin
SysDir := StrAlloc(MAX_PATH);
GetSystemDirectory(SysDir, MAX_PATH);
Result := string(SysDir);
if Result[Length(Result)] <> '\' then
Result := Result + '\';
StrDispose(SysDir);
end;

// or:

function GetSystemDirectory(var S: String): Boolean;
var
Len: Integer;
begin
Len := Windows.GetSystemDirectory(nil, 0);
if Len > 0 then
begin
SetLength(S, Len);
Len := Windows.GetSystemDirectory(PChar(S), Len);
SetLength(S, Len);
Result := Len > 0;
end else
Result := False;
end;

{ Getting the Temporary Directory }

function GetTempDir: string;
var
Buffer: array[0..MAX_PATH] of Char;
begin
GetTempPath(SizeOf(Buffer) - 1, Buffer);
Result := StrPas(Buffer);
end;

// or:

function GetTempPath: string;
var
TmpDir: PChar;
begin
TmpDir := StrAlloc(MAX_PATH);
GetTempPath(TmpDir, MAX_PATH);
Result := string(TmpDir);
if Result[Length(Result)] <> '\' then
Result := Result + '\';
StrDispose(TmpDir);
end;

// or:

function GetTempPath(var S: String): Boolean;
var
Len: Integer;
begin
Len := Windows.GetTempPath(0, nil);
if Len > 0 then
begin
SetLength(S, Len);
Len := Windows.GetTempPath(Len, PChar(S));
SetLength(S, Len);
Result := Len > 0;
end else
Result := False;
end;

// ****************************

// Example Calls:

procedure TForm1.Button1Click(Sender: TObject);
begin
label1.Caption := GetWinDir;
label2.Caption := GetSysDir;
label3.Caption := GetTempDir;
end;
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
发表于 2015-2-1 16:39:19 | 显示全部楼层
支持楼主发扬Delphi
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复 支持 反对

使用道具 举报

您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

小黑屋|手机版|Archiver|鱼C工作室 ( 粤ICP备18085999号-1 | 粤公网安备 44051102000585号)

GMT+8, 2024-3-29 18:06

Powered by Discuz! X3.4

© 2001-2023 Discuz! Team.

快速回复 返回顶部 返回列表