判断目录是否打开。如果打开则关闭。在线等待啊。。。。。 (50分)

S

sydan

Unregistered / Unconfirmed
GUEST, unregistred user!
判断某个目录是否被:
SelectDirectory('C:/XX',[sdAllowCreate,sdPerformCreate,sdPrompt],0)
选定并打开,如是已被打开则关闭这个目录。
老大,应该如何做啊?
 
是不是要得到已经打开的目录名?
用这种方式打开吧.

procedure TForm1.Button2Click(Sender: TObject);
var
dir: string;
begin
selectdirectory('caption','c:/',dir);
caption := dir;
end;
 
不是这样的。我想知道我所要删除的目录是否被打开,如果是被打开则关闭,再删除
否则则直接删除。怎么做啊,快把我都给急死了
 
删除目录,不需要知道此目录是否被打开,只需要知道此目录内所有的文件(包括子目录)
有没有被使用.
 
如果只是判断自己程序打开.用一个全局变量保存已经打开的目录.
 
if SelectDirectory('C:/XX',[sdAllowCreate,sdPerformCreate,sdPrompt],0) then 已经打开
else 没有打开
 
TO:All
謝謝大家!但問題是:
如果我是用SelectDirectory(VPath,[sdAllowCreate,sdPerformCreate,sdPrompt],0)
打開的這個目錄.那麼,當我用RmDir(VPath)來刪除這個目錄時則出現I/O錯誤.
所以我現在就需要知道這個目錄是否被打開,如果是的話則關閉並刪除這個目錄.否則
刪除這個目錄.
Sydan
 
IShellWindows
 
一大堆API,不知对你有帮助吗?
Api函数列表
——与文件相关

Api函数名
函数说明
适用范围

W3.x
W95
NT

mmioWrite
写文件




WriteFile
写文件




ExtractAssociatedIcon
从文件或相关EXE中获取图标句柄




ExtractIcon
从可执行文件中返回图标句柄




LZRead
从压缩文件中读入数据




GetPrivateProfileString
从私有文件中获取字符串




GetPrivateProfileInt
从私有文件中获取整数




UnlockFile
开锁文件




UnlockFileEx

开锁文件




LZOpenFile
打开文件




mmioOpen
打开多媒体文件




SetFileApisToOEM
设置文件API为OEM字符集




SetFileSecurity
设置文件或目录安全属性




FindFirstChangeNotification
设置文件或目录修改等待




SetFileTime
设置文件的64位时间




mmioSetInfo
设置文件信息




SetTextColor
设置文件前颜色




SetFilePointer
设置文件指针位置




SetFileAttributes
设置文件属性




SetFileApisToOEM
设置文件API为OEM字符集




SetFileSecurity
设置文件或目录安全属性




FindFirstChangeNotification
设置文件或目录修改等待





SetFileTime
设置文件的64位时间




mmioSetInfo
设置文件信息




SetTextColor
设置文件前颜色




SetFilePointer
设置文件指针位置




SetFileAttributes
设置文件属性




DeleteFile
删除文件




mmioSeek
改变当前文件位置




MoveFile
更名文件




MoveFileEx
更名文件




GetFileTime
返回文件64位时间




GetFileTitle
返回文件名




GetVolumeInformation
返回文件系统信息





GetFileVersionInfo
返回文件的版本信息




GetFullPathName
返回文件的路径名




GetFileInformationByHandle
返回文件信息




GetFileType
返回文件类型




GetFileAttributes
返回文件属性




GetShortPathName
返回文件短路径




mmioRead
读入文件




ReadFile
读文件




WriteFileEx
写文件





 

 
 
1、拷贝目录

---- 为了能拷贝目录下带有子目录的情况,先定义一个辅助的拷贝函数,它是递归执行的,直到把目录下的所有文件和子目录都拷贝完。

---- 1.1拷贝目录的递归辅助函数:DoCopyDir

function DoCopyDir(sDirName:String;
sToDirName:String):Boolean;
var
hFindFile:Cardinal;
t,tfile:String;
sCurDir:String[255];
FindFileData:WIN32_FIND_DATA;
begin
//先保存当前目录
sCurDir:=GetCurrentDir;
ChDir(sDirName);
hFindFile:=FindFirstFile('*.*',FindFileData);
if hFindFile< >INVALID_HANDLE_VALUE then
begin
if not DirectoryExists(sToDirName) then
ForceDirectories(sToDirName);
repeat
tfile:=FindFileData.cFileName;
if (tfile='.') or (tfile='..') then
Continue;
if FindFileData.dwFileAttributes=
FILE_ATTRIBUTE_DIRECTORY then
begin
t:=sToDirName+'/'+tfile;
if not DirectoryExists(t) then
ForceDirectories(t);
if sDirName[Length(sDirName)]< >'/' then
DoCopyDir(sDirName+'/'+tfile,t)
else
DoCopyDir(sDirName+tfile,sToDirName+tfile);
end
else
begin
t:=sToDirName+'/'+tFile;
CopyFile(PChar(tfile),PChar(t),True);
end;
until FindNextFile(hFindFile,FindFileData)=false;
FindClose(hFindFile);
end
else
begin
ChDir(sCurDir);
result:=false;
exit;
end;
//回到原来的目录下
ChDir(sCurDir);
result:=true;
end;

---- 1.2拷贝目录的函数:CopyDir

function CopyDir(sDirName:String;
sToDirName:string):Boolean;
begin
if Length(sDirName)< =0 then
exit;
//拷贝...
Result:=DoCopyDir(sDirName,sToDirName);
end;

---- 2、删除目录

---- 删除目录与拷贝目录很类似,但为了能删除位于根目录下的一个空目录,需要在辅助函数中设置一个标志变量,即:如果删除的是空目录,则置bEmptyDir为True,这一句已经用深色框表示了。

---- 2.1删除目录的递归辅助函数:DoRemoveDir

function DoRemoveDir(sDirName:String):Boolean;
var
hFindFile:Cardinal;
tfile:String;
sCurDir:String;
bEmptyDir:Boolean;
FindFileData:WIN32_FIND_DATA;
begin
//如果删除的是空目录,则置bEmptyDir为True
//初始时,bEmptyDir为True
bEmptyDir:=True;
//先保存当前目录
sCurDir:=GetCurrentDir;
SetLength(sCurDir,Length(sCurDir));
ChDir(sDirName);
hFindFile:=FindFirstFile('*.*',FindFileData);
if hFindFile< >INVALID_HANDLE_VALUE then
begin
repeat
tfile:=FindFileData.cFileName;
if (tfile='.') or (tfile='..') then
begin
bEmptyDir:=bEmptyDir and True;
Continue;
end;
//不是空目录,置bEmptyDir为False
bEmptyDir:=False;
if FindFileData.dwFileAttributes=
FILE_ATTRIBUTE_DIRECTORY then
begin
if sDirName[Length(sDirName)]< >'/' then
DoRemoveDir(sDirName+'/'+tfile)
else
DoRemoveDir(sDirName+tfile);
if not RemoveDirectory(PChar(tfile)) then
result:=false
else
result:=true;
end
else
begin
if not DeleteFile(PChar(tfile)) then
result:=false
else
result:=true;
end;
until FindNextFile(hFindFile,FindFileData)=false;
FindClose(hFindFile);
end
else
begin
ChDir(sCurDir);
result:=false;
exit;
end;
//如果是空目录,则删除该空目录
if bEmptyDir then
begin
//返回上一级目录
ChDir('..');
//删除空目录
RemoveDirectory(PChar(sDirName));
end;

//回到原来的目录下
ChDir(sCurDir);
result:=true;
end;

---- 2.2删除目录的函数:DeleteDir

function DeleteDir(sDirName:String):Boolean;
begin
if Length(sDirName)< =0 then
exit;
//删除...
Result:=DoRemoveDir(sDirName) and RemoveDir(sDirName);
end;

---- 3、移动目录

---- 有了拷贝目录和删除目录的函数,移动目录就变得很简单,只需顺序调用前两个函数即可:

function MoveDir(sDirName:String;
sToDirName:string):Boolean;
begin
if CopyDir(sDirName,sToDirName) then
if RemoveDir(sDirName) then
result:=True
else
result:=false;
end;

///////////////////////////////////////////////
procedure TForm1.Button2Click(Sender: TObject);
var
OpStruc: TSHFileOpStruct;
frombuf, tobuf: Array [0..128] of Char;
Begin
FillChar( frombuf, Sizeof(frombuf), 0 );
FillChar( tobuf, Sizeof(tobuf), 0 );
StrPCopy( frombuf, 'd:/brief/*.*' );
StrPCopy( tobuf, 'd:/temp/brief' );
With OpStruc DO Begin
Wnd:= Handle;
wFunc:= FO_COPY;
pFrom:= @frombuf;
pTo:=@tobuf;
fFlags:= FOF_NOCONFIRMATION or FOF_RENAMEONCOLLISION;
fAnyOperationsAborted:= False;
hNameMappings:= Nil;
lpszProgressTitle:= Nil;

end;
ShFileOperation( OpStruc );
end;
 
谢谢。虽然小弟并没有完成。但还是谢谢各位兄弟。帮忙!
因为小弟很久没来过了。所以一直拖到现在才分分给各位。真是非常抱歉。
 
顶部