Stream对象又称流式对象
Stream对象又称流式对象
posted @ 2011-09-22 09:27 delphi初学者 阅读(76) 评论(0) 编辑
WinAPI: GetDiskFreeSpaceEx - 获取磁盘容量信息
WinAPI: GetDiskFreeSpaceEx - 获取磁盘容量信息
//声明:
GetDiskFreeSpaceEx(
lpDirectoryName: PChar; {磁盘根路径}
var lpFreeBytesAvailableToCaller: TLargeInteger; {可用空间}
var lpTotalNumberOfBytes: TLargeInteger; {总空间}
TotalFree: PLargeInteger {剩余空间}
): BOOL;
---------------------------------------------------------------------------
-----
//举例:
procedure TForm1.FormCreate(Sender: TObject);
var
d1,d2,d3: Int64;
begin
GetDiskFreeSpaceEx('C:',d1,d2,@d3);
Memo1.Clear;
with Memo1.Lines do
begin
Add(Format('可用空间: %f GB',[d1/1024/1024/1024]));
Add(Format('总空间: %f GB',[d2/1024/1024/1024]));
Add(Format('剩余空间: %f GB',[d3/1024/1024/1024]));
end;
end;
本文转自:http://www.cnblogs.com/del/archive/2008/02/11/1066914.html
posted @ 2011-09-13 09:31 delphi初学者 阅读(107) 评论(0) 编辑
GetVolumeInformation
GetVolumeInformation函数来获取硬盘的序列号
函数声明:
BOOL GetVolumeInformation(
LPCTSTR lpRootPathName, // 与获取信息卷的根路径
LPTSTR lpVolumeNameBuffer, // 用于装在卷名的一个字符串
DWORD nVolumeNameSize, // 字符串的长度
LPDWORD lpVolumeSerialNumber, // 用于装载磁盘卷序列号的变量
LPDWORD lpMaximumComponentLength, // 指定一个变量,用于装载文件名每一部分的长度
LPDWORD lpFileSystemFlags, // 用于装载一个或多个二进制位标志的长度
LPTSTR lpFileSystemNameBuffer, // 指定一个缓冲区
DWORD nFileSystemNameSize // lpFileSystemNameBuffer,的长度
);
unit Unit1;
interface
uses
Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
Dialogs, StdCtrls;
type
TForm1 = class(TForm)
Label1: TLabel;
Edit1: TEdit;
Button1: TButton;
Button2: TButton;
Button3: TButton;
Edit2: TEdit;
Edit3: TEdit;
Edit4: TEdit;
Button4: TButton;
OpenDialog1: TOpenDialog;
ComboBox1: TComboBox;
Edit5: TEdit;
Label2: TLabel;
Button5: TButton;
procedure Button4Click(Sender: TObject);
procedure Button1Click(Sender: TObject);
procedure Button2Click(Sender: TObject);
procedure Button3Click(Sender: TObject);
procedure ComboBox1Change(Sender: TObject);
procedure Button5Click(Sender: TObject);
private
{ Private declarations }
public
{ Public declarations }
end;
var
Form1: TForm1;
implementation
function gefiletime(sFilename:string;timetype:integer):tdatetime;
var
ffd:twin32finddata;
dft:dword;
lft,time:tfiletime;
h:thandle;
begin
h:=windows.FindFirstFile(pchar(sfilename),ffd);
case timetype of
0:time:=ffd.ftCreationTime;
1:time:=ffd.ftLastWriteTime;
2:time:=ffd.ftLastAccessTime;
end;
if(h<>invalid_handle_value) then
begin
windows.FindClose(h);
filetimetolocalfiletime(time,lft);
filetimetodosdatetime(lft,longrec(dft).hi,longrec(dft).lo);
result:=filedatetodatetime(dft);
end
else
result:=0;
end;
function getdiskvoiserialld(drivename:string):dword;
var
dwtemp1,dwtemp2:dword;
nresult:pdword;
begin
new(nresult);
getvolumeinformation(pchar(drivename+'\'),nil,0,nresult,dwtemp1,dwtemp2,nil,0);
result:=nresult^;
dispose(nresult);
end;
{$R *.dfm}
procedure TForm1.Button4Click(Sender: TObject);
begin
if opendialog1.Execute then
edit1.Text:=opendialog1.FileName;
end;
procedure TForm1.Button1Click(Sender: TObject);
begin
edit2.Text:=datetostr(gefiletime(edit1.text,0));
end;
procedure TForm1.Button2Click(Sender: TObject);
begin
edit3.Text:=datetostr(gefiletime(edit1.text,1));
end;
procedure TForm1.Button3Click(Sender: TObject);
begin
edit4.Text:=datetostr(gefiletime(edit1.text,2));
end;
procedure TForm1.ComboBox1Change(Sender: TObject);
begin
edit5.Text:=inttostr(getdiskvoiserialld(combobox1.Items[combobox1.ItemIndex]));
end;
procedure TForm1.Button5Click(Sender: TObject);
var
nu : DWORD;
Vf : DWORD;
Num : DWORD; //用来存储得到的硬盘序列号
vl : array[0..MAX_PATH] of Char; //字符串的长度
begin
GetVolumeInformation(PChar(Trim(Edit1.Text)),nil,SizeOf(vl),@Num,nu,Vf,nil,0);
Edit2.Text := IntToStr(Num);
end;
end.
posted @ 2011-09-13 09:15 delphi初学者 阅读(242) 评论(0) 编辑
FindFirstFile
VC声明
HANDLE FindFirstFile(
LPCTSTR lpFileName, // file name
LPWIN32_FIND_DATA lpFindFileData // data buffer
);
lpFileName String,欲搜索的文件名。可包含通配符,并可包含一个路径或相对路
径名
lpFindFileData WIN32_FIND_DATA,这个结构用于装载与找到的文件有关的信息。该结
构可用于后续的搜索
调用失败 返回为INVALID_HANDLE_VALUE(即-1)
typedef struct _WIN32_FIND_DATA { // wfd
DWORD dwFileAttributes;
FILETIME ftCreationTime;
FILETIME ftLastAccessTime;
FILETIME ftLastWriteTime;
DWORD nFileSizeHigh;
DWORD nFileSizeLow;
DWORD dwReserved0;
DWORD dwReserved1;
TCHAR cFileName[ MAX_PATH ];
TCHAR cAlternateFileName[ 14 ];
} WIN32_FIND_DATA;
WIN32_FIND_DATA是文件查找结构体(我这样给它命名),主要作用是进行文件查找。
你可以用函数FindFirst()和FindNext()来遍历一个目录
posted @ 2011-09-12 11:06 delphi初学者 阅读(277) 评论(0) 编辑
设置文件日期信息
unit MainUnit;
interface
uses
Windows, Messages, SysUtils, Classes, Graphics, Controls, Forms, Dialogs,
StdCtrls;
type
TMainForm = class(TForm)
Label1: TLabel;
Edit1: TEdit;
Button1: TButton;
Label2: TLabel;
Edit2: TEdit;
Button2: TButton;
Label3: TLabel;
Edit3: TEdit;
Label4: TLabel;
Edit4: TEdit;
OpenDialog1: TOpenDialog;
procedure Button1Click(Sender: TObject);
procedure Button2Click(Sender: TObject);
private
{ Private declarations }
public
{ Public declarations }
end;
var
MainForm: TMainForm;
implementation
{$R *.DFM}
function GetFilesTime(sFilename: String; Timetype: Integer): TDateTime;
var
ffd: TWin32FindData;
dft: DWord;
lft, Time: TFileTime;
sHandle: THandle;
begin
sHandle:= Windows.FindFirstFile(PChar(sFileName), ffd);
if (sHandle <>INVALID_HANDLE_VALUE) then
begin
case Timetype of
0: Time:= ffd.ftCreationTime;
1: Time:= ffd.ftLastWriteTime;
2: Time:= ffd.ftLastAccessTime;
end;
Windows.FindClose(sHandle);
FileTimeToLocalFileTime(Time, lft);
FileTimeToDosDateTime(lft, LongRec(dft).HI, LongRec(dft).Lo);
Result:= FileDateToDateTime(dft);
end else Result:= 0;
end;
procedure TMainForm.Button1Click(Sender: TObject);
begin
if OpenDialog1.Execute then
Edit1.Text:= OpenDialog1.FileName;
end;
procedure TMainForm.Button2Click(Sender: TObject);
begin
if Edit1.Text<> '' then
begin
Edit2.Text:= DateToStr(GetFilesTime(Edit1.Text, 0));
Edit3.Text:= DateToStr(GetFilesTime(Edit1.Text, 1));
Edit4.Text:= DateToStr(GetFilesTime(Edit1.Text, 2));
end
else ShowMessage('请指定文件!');
end;
end.
posted @ 2011-09-12 11:05 delphi初学者 阅读(37) 评论(0) 编辑
文件函数
unit Unit1;
interface
uses
Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
Dialogs, StdCtrls;
type
TForm1 = class(TForm)
Label1: TLabel;
Edit1: TEdit;
Button1: TButton;
Button2: TButton;
Edit2: TEdit;
Button3: TButton;
Edit3: TEdit;
Button4: TButton;
Edit4: TEdit;
Button5: TButton;
Edit5: TEdit;
Button6: TButton;
Edit6: TEdit;
Button7: TButton;
Edit7: TEdit;
Button8: TButton;
Button9: TButton;
Button10: TButton;
Edit8: TEdit;
Edit9: TEdit;
Edit10: TEdit;
OpenDialog1: TOpenDialog;
procedure Button1Click(Sender: TObject);
procedure Button2Click(Sender: TObject);
procedure Button3Click(Sender: TObject);
procedure Button4Click(Sender: TObject);
procedure Button5Click(Sender: TObject);
procedure Button6Click(Sender: TObject);
procedure Button7Click(Sender: TObject);
procedure Button8Click(Sender: TObject);
procedure Button9Click(Sender: TObject);
procedure Button10Click(Sender: TObject);
private
{ Private declarations }
public
{ Public declarations }
end;
var
Form1: TForm1;
implementation
{$R *.dfm}
procedure TForm1.Button1Click(Sender: TObject);
begin
if opendialog1.Execute then
begin
edit1.Text:=opendialog1.FileName;
end;
end;
procedure TForm1.Button2Click(Sender: TObject);
begin
//将当前路径名与指定文件名合成一个绝对的文件名
edit2.Text:=expandfilename(edit1.text)
end;
procedure TForm1.Button3Click(Sender: TObject);
begin
//获取一个以UNC格式的包括风络驱动器名的绝对文件名
edit3.Text:=expandUNCfileName(edit1.text);
end;
procedure TForm1.Button4Click(Sender: TObject);
begin
//从绝对文件名中获取目录名
edit4.Text:=ExtractFileDir(edit1.text);
end;
procedure TForm1.Button5Click(Sender: TObject);
begin
//从绝对文件名中获取驱动器名
edit5.Text:=extractfiledrive(edit1.text);
end;
procedure TForm1.Button6Click(Sender: TObject);
begin
//从绝对文件名中获取扩展名
edit6.Text:=extractfileext(edit1.text);
end;
procedure TForm1.Button7Click(Sender: TObject);
begin
//从绝对文件名中获取文件名
edit7.Text:=extractfilename(edit1.text);
end;
procedure TForm1.Button8Click(Sender: TObject);
begin
//从绝对文件名中获取路径名
edit8.Text:=extractfilepath(edit1.text);
end;
procedure TForm1.Button9Click(Sender: TObject);
begin
//从绝对文件名中获取相对于某一路径的相对路径名
edit9.Text:=extractRelativepath(edit1.text,edit4.text);
end;
procedure TForm1.Button10Click(Sender: TObject);
begin
// 将长文件名转化为短文件名
edit10.Text:=extractshortpathname(edit1.text);
end;
end.
posted @ 2011-09-12 09:18 delphi初学者 阅读(40) 评论(0) 编辑
读取文件属性 FileGetAttr
procedure TForm1.N17Click(Sender: TObject); //读取文件属性 FileGetAttr; var FileName: string; Attr: Integer; //属性值是一个整数 begin FileName := 'F:\test\Test.txt'; Attr := FileGetAttr(FileName); ShowMessage(IntToStr(Attr)); //属性可选值(有些用不着): //FILE_ATTRIBUTE_READONLY = 1; 只读 //FILE_ATTRIBUTE_HIDDEN = 2; 隐藏 //FILE_ATTRIBUTE_SYSTEM = 4; 系统 //FILE_ATTRIBUTE_DIRECTORY = 16; 文件夹 //FILE_ATTRIBUTE_ARCHIVE = 32; 存档 //FILE_ATTRIBUTE_DEVICE = 64 //FILE_ATTRIBUTE_NORMAL = 128; 一般 //FILE_ATTRIBUTE_TEMPORARY = 256 //FILE_ATTRIBUTE_SPARSE_FILE = 512 //FILE_ATTRIBUTE_REPARSE_POINT = 1204 //FILE_ATTRIBUTE_COMPRESSED = 2048; 压缩 //FILE_ATTRIBUTE_OFFLINE = 4096 //FILE_ATTRIBUTE_NOT_CONTENT_INDEXED = 8192; 不被索引 //FILE_ATTRIBUTE_ENCRYPTED = 16384 end;
下面进行函数分析:
function FileGetAttr(const FileName: string): Integer; begin Result := GetFileAttributes(PChar(FileName)); end; //可以看出此函数是由API函数GetFileAttributes封装而来,参数是要操作文件的文件名 //返回值是整型的属性值,如'32'表示为存档文件
posted @ 2011-09-12 05:49 delphi初学者 阅读(128) 评论(0) 编辑
WinAPI: SetCurrentDirectory、GetCurrentDirectory - 设置与获取当前目录
//声明: SetCurrentDirectory( lpPathName: PAnsiChar {路径名} ): BOOL; GetCurrentDirectory( nBufferLength: DWORD; {缓冲区大小} lpBuffer: PAnsiChar {缓冲区} ): DWORD; {返回目录实际长度} //举例: var buf: array[0..MAX_PATH] of Char; begin SetCurrentDirectory('c:\temp'); GetCurrentDirectory(SizeOf(buf), buf); ShowMessage(buf); {c:\temp} end; |
posted @ 2011-09-12 05:39 delphi初学者 阅读(85) 评论(0) 编辑
临界区对象TCriticalSection(Delphi) 与 TRtlCriticalSection 的区别
临界区对象TCriticalSection(Delphi) 与 TRtlCriticalSection 的区别
TRtlCriticalSection 是一个结构体,在windows单元中定义;
是InitializeCriticalSection,EnterCriticalSection,LeaveCriticalSection, DeleteCriticalSection 等这几个kernel32.dll中的临界区操作API的参数;
TCriticalSection是在SyncObjs单元中实现的类,它对上面的那些临界区操作API函数进行了了封装,简化并方便了在Delphi的使用;
如TCriticalSection.Enter;就是调用了EnterCriticalSection这个API函数。
多线程程序中,如果各个线程要访问同一个资源,如同一个变量
这时就要使用线程同步技术,才不会使线程之间产生冲突和干扰
线程同步有多种办法,使用临界区是其中最简单,也是效率最高的办法(CPU占用时间最少)
使用临界区代码如下:
先声明一个TRTLCriticalSection类型的全局变量
var
MyCs:TRTLCriticalSection;
在程序开始或建立线程之前,初始化
InitializeCriticalSection(MyCs);//初始化临界区
在程序结束或所有线程结束后,删除它
DeleteCriticalSection(MyCs);//删除临界区
再在线程中要同步的地方加入
EnterCriticalSection(MyCs); //进入临界区
try
//程序代码
finally
LeaveCriticalSection(MyCs); //离开临界区
end;