1、文件查找的方法
function Searchfile(path: string): TStringList;
var
SearchRec: TSearchRec;
found: integer;
begin
Result := TStringList.Create;
found := FindFirst(path + '\' + '*.*', faAnyFile, SearchRec);
while found = 0 do
begin
if (SearchRec.Name <> '.') and (SearchRec.Name <> '..') and
(SearchRec.Attr <> faDirectory) then
Result.Add(SearchRec.Name);
found := FindNext(SearchRec);
end;
FindClose(SearchRec);
end;
2、通过文件名字获取Base64方法
function getBASE64_ByFile(const fileName: WideString): WideString;
var
str:TStringstream;
fstream:TFilestream;
begin
//将bmp,jpeg 或其他 文件,返回BASE64编码
result:='';
if fileExists(fileName) then
begin
fstream := TFilestream.Create(fileName, fmOpenRead);
str := TstringStream.create('');
EncodeStream(fstream,str);
result := str.DataString;
fstream.free;
str.free;
end;
end;
3、通过进程名杀进程
procedure kill_all_proc(filename: string);
var
i: integer;
lppe: TProcessEntry32;
found: boolean;
Hand: THandle;
dwProc: Cardinal;
h: HWND;
begin
Hand := CreateToolhelp32Snapshot(TH32CS_SNAPPROCESS, 0);
lppe.dwSize := SizeOf(TProcessEntry32);
found := Process32First(Hand, lppe);
while found do
begin
if Trim(uppercase(StrPas(lppe.szExeFile))) = uppercase(filename) then
begin
dwProc := OpenProcess(PROCESS_ALL_ACCESS or PROCESS_TERMINATE, FALSE,
dword(lppe.th32ProcessID));
TerminateProcess(dwProc, 0);
end;
found := Process32Next(Hand, lppe);
end;
end;
4、十六进制字符串转字符串
function HexToStr(const hMsg: String): String;
var
I: Integer;
begin
Result := '';
for I := 1 to Length(hMsg) div 2 do
Result := Result + Chr(StrToIntDef('0x'+ Copy(hMsg,(i-1)*2+1,2),0));
end;
5、字符串转DWORD,注意这里的字符串指接口等接收到的字符串。
function StrToWord(s: string): DWORD;
var
I, c:integer;
begin
Result := 0;
c := Length(s);
if c > 4 then
begin
s := Copy(s,1,4);
c := 4;
end;
for I := 1 to c do
Result:= Result + Round(IntPower(256,c-i)) * ord(s[i]);
end;
6、jpg图片压缩
procedure Compressjpg(filepath: string);
var
JPEGImage: TJPEGImage;
bmp: TBitmap;
begin
try
JPEGImage := TJPEGImage.Create;
bmp := TBitmap.Create;
JPEGImage.LoadFromFile(filepath);
bmp.Width := JPEGImage.Width;
bmp.Height := JPEGImage.Height;
bmp.Canvas.StretchDraw(bmp.Canvas.ClipRect, JPEGImage);
JPEGImage.Assign(bmp);
JPEGImage.CompressionQuality:=StrToInt(ComboBox1.Text);
JPEGImage.Compress;
JPEGImage.SaveToFile(filepath);
JPEGImage.free;
bmp.free;
except
end;
end;