Android smartphone

Showing posts with label Files. Show all posts
Showing posts with label Files. Show all posts

2009-10-12

How to use GetTempFileName to create a unique temporary file?

This tips comes from our partner SwissDelphiCenter.ch

mailto:info@delphitips.com.

Author:
Gustav Evertsson

function GetTempFile(const Extension: string): string;
var
Buffer: array[0..MAX_PATH] OF Char;
begin
GetTempPath(Sizeof(Buffer)-1,Buffer);
GetTempFileName(Buffer,'~',0,Buffer);
result := StrPas(Buffer);
end;

procedure TForm1.Button1Click(Sender: TObject);
begin
ShowMessage(GetTempFile('.tmp'));
// The temp. file looks like C:\WINDOWS\TEMP\~61D5.TMP
end;

{Note:
The GetTempFile function just returns a unique filename but
doesn't create the file. }

How to show Directories in a TTreeview?

This tips comes from our partner SwissDelphiCenter.ch

mailto:info@delphitips.com

Author:
Gustav Evertsson
Ratings: 0, Votes: 0

procedure TForm1.GetDirectories(Tree: TTreeView; Directory: string; Item: TTreeNode; IncludeFiles: boolean);
var
SearchRec: TSearchRec;
ItemTemp: TTreeNode;
begin

Tree.Items.BeginUpdate;
if Directory[length(Directory)] <> '\' then Directory := Directory + '\';
if FindFirst(Directory + '*.*', faDirectory, SearchRec) = 0 then
begin
repeat
if (SearchRec.Attr and faDirectory = faDirectory) and (SearchRec.Name[1] <> '.')

then
begin
if (SearchRec.Attr and faDirectory > 0) then
Item := Tree.Items.AddChild(Item, SearchRec.Name);

ItemTemp := Item.Parent;
GetDirectories(Tree, Directory + SearchRec.Name, Item, IncludeFiles);
Item := ItemTemp;
end else

if IncludeFiles then
if SearchRec.Name[1] <> '.' then
Tree.Items.AddChild(Item, SearchRec.Name);
until FindNext(SearchRec) <> 0;
FindClose(SearchRec);
end;
Tree.Items.EndUpdate;
end;

procedure TForm1.Button1Click(Sender: TObject);
begin
GetDirectories(TreeView1, 'C:\', nil, True);
end;

How to retrieve the UNC Paths?

This tips comes from our partner SwissDelphiCenter.ch

mailto: info@delphitips.com

Author:
Gustav Evertsson
Ratings: 0, Votes: 0

ExpandUNCFileName returns the full path of the FileName
with the network drive portion in UNC format.
The pathname in the UNC-Format has the format:
\\Servername\sharename

//Example
Label1.Caption := ExpandUNCFileName('K:\sharename.tmp'));

{where "K" is a Network Drive.}

How to get the owner of a file ?

This tips comes from our partner SwissDelphiCenter.ch

mailto:info@delphitips.com

Author:
Gustav Evertsson
Ratings: 0, Votes: 0
// When you create a file or directory, you become the owner of it.
// With GetFileOwner you get the owner of a file.
function GetFileOwner(FileName: string; var Domain, Username: string): Boolean;
var
SecDescr: PSecurityDescriptor;
SizeNeeded, SizeNeeded2: DWORD;
OwnerSID: PSID;
OwnerDefault: BOOL;
OwnerName, DomainName: PChar;
OwnerType: SID_NAME_USE;
begin
GetFileOwner := False;
GetMem(SecDescr,1024);
GetMem(OwnerSID,SizeOf(PSID));
GetMem(OwnerName,1024);
GetMem(DomainName,1024);
try
if not GetFileSecurity(PChar(FileName),
OWNER_SECURITY_INFORMATION,
SecDescr,1024,SizeNeeded) then
Exit;
if not GetSecurityDescriptorOwner(SecDescr,
OwnerSID,OwnerDefault) then
Exit;
SizeNeeded := 1024;
SizeNeeded2 := 1024;
if not LookupAccountSID(nil,OwnerSID,OwnerName,
SizeNeeded,DomainName,SizeNeeded2,OwnerType) then
Exit;
Domain := DomainName;
Username := OwnerName;
finally
FreeMem(SecDescr);
FreeMem(OwnerName);
FreeMem(DomainName);
end;
GetFileOwner := True;
end;

procedure TForm1.Button1Click(Sender: TObject);
var
Domain, Username: string;
begin
GetFileOwner('YourFile.xyz',domain,username);
ShowMessage(username + '@' + domain);
end;

// Note: This only works under NT.

How to check to see if a drive is ready

Gustav Evertsson
Ratings: 0, Votes: 0

The following function accepts a drive letter as a parameter, and it will return a boolean value that indicates whether or not there is a disk in the drive.

function DiskInDrive(Drive: Char): Boolean;
var
ErrorMode: word;
begin
{ make it upper case }

if Drive in ['a'..'z'] then Dec(Drive, $20);


{ make sure it's a letter }

if not (Drive in ['A'..'Z']) then
raise EConvertError.Create('Not a valid drive ID');


{ turn off critical errors }
ErrorMode := SetErrorMode(SEM_FailCriticalErrors);


try
{ drive 1 = a, 2 = b, 3 = c, etc. }
if DiskSize(Ord(Drive) - $40) = -1 then
Result := False
else
Result := True;
finally
{ restore old error mode }
SetErrorMode(ErrorMode);
end;
end;

How do I convert "Long File Name.pas" to "longfi~1.pas"?

Author: Gustav Evertsson

mailto:info@delphitips.com


Get the shot file name:

Function GetShortFileName(Const FileName : String) : String;
var
aTmp: array[0..255] of char;
begin
if GetShortPathName(PChar(FileName),aTmp,Sizeof(aTmp)-1)=0 then
Result:= FileName
else
Result:=StrPas(aTmp);

end;

and the long name:

Function GetLongFileName(Const FileName : String) : String;
var
aInfo: TSHFileInfo;
begin
if SHGetFileInfo(PChar(FileName),0,aInfo,Sizeof(aInfo),SHGFI_DISPLAYNAME)<>0 then
Result:= String(aInfo.szDisplayName)
else
Result:= FileName;
end;