오픈 소스 윈도우 프로그램 통합개발환경 라자루스에서 내 문서 폴더 위치를 구하는 방법입니다. 라자루스는 델파이 개발환경을 리눅스용으로도 만들자 해서 시작된 프로젝트라서 아마도 최근 버전의 델파이에서도 같은 방법으로 구할 수 있지 않을까 생각됩니다.
How to get the "My documents" folder location in lazarus
예제를 위한 폼 디자인은 간단합니다. Button1은 내문서 폴더 위치를 구해서 labDocuments에 출력합니다. 전체 소스는 아래와 같습니다.
unit Unit1;
{$mode objfpc}{$H+}
interface
uses
Classes, SysUtils, Forms, Controls, Graphics, Dialogs, StdCtrls;
type
{ TForm1 }
TForm1 = class(TForm)
Button1: TButton;
labDocuments: TLabel;
Label1: TLabel;
procedure Button1Click(Sender: TObject);
private
public
end;
var
Form1: TForm1;
implementation
{$R *.lfm}
uses
shlobj;
{ TForm1 }
function GetMyDocumentsDir: string;
var
r: Boolean;
path: array[0..Max_Path] of Char;
begin
r := ShGetSpecialFolderPath(0, path, CSIDL_PERSONAL, False) ;
if not r
then
raise Exception.Create('Could not find MyDocuments folder location.') ;
Result := Path;
end;
procedure TForm1.Button1Click(Sender: TObject);
begin
labDocuments.Caption := GetMyDocumentsDir;
end;
end.
GetMyDocumetsDir 함수가 내문서 폴더 위치를 반환하는데요, 아래는 실행한 모습입니다.
폴더 이름에 한글이 있으면 깨져 보일 수 있습니다. utf8 함수를 이용하여 한글이 제대로 나오도록 수정하는 루틴은 아래와 같습니다.
unit Unit1;
{$mode objfpc}{$H+}
interface
uses
Classes, SysUtils, Forms, Controls, Graphics, Dialogs, StdCtrls;
type
{ TForm1 }
TForm1 = class(TForm)
Button1: TButton;
labDocuments: TLabel;
Label1: TLabel;
procedure Button1Click(Sender: TObject);
private
public
end;
var
Form1: TForm1;
implementation
{$R *.lfm}
uses
shlobj, lazutf8;
{ TForm1 }
function LocalAnsiToUtf8( strAnsi: string): string;
begin
SetCodePage( RawByteString( strAnsi), 949, FALSE);
result := AnsiToUtf8( strAnsi);
end;
function GetMyDocumentsDir: string;
var
r: Boolean;
path: array[0..Max_Path] of Char;
begin
r := ShGetSpecialFolderPath(0, path, CSIDL_PERSONAL, False) ;
if not r
then
raise Exception.Create('Could not find MyDocuments folder location.') ;
Result := LocalAnsiToUtf8( Path);
end;
procedure TForm1.Button1Click(Sender: TObject);
begin
labDocuments.Caption := GetMyDocumentsDir;
end;
end.
한글 출력을 위해 LocalAnsiToUtf8() 함수를 추가했습니다. 이 함수를 사용하려면 uses절에 lazutf8 유닛을 추가해야 합니다.
내문서 폴더 위치가 한글 깨짐 없이 출력됩니다.
'컴퓨터 > 프로그래밍' 카테고리의 다른 글
삼성 덱스 화면 캡쳐 방법 (0) | 2022.05.16 |
---|---|
라자루스 사용자 AppData 폴더 위치 구하기 (0) | 2022.05.03 |
C언어 파일 복사 함수 filecopy() (0) | 2022.05.01 |