问题描述
我运行cmd.exe来移动具有管理员权限的文件:
I run cmd.exe to move a file with Administrator rights:
ThisParams := '/C move ' + '"' + ThisSourceFile + '"' + ' ' + '"' + ATargetFile + '"';
Winapi.ShellAPI.ShellExecute(0, 'runas', 'cmd.exe', PChar(ThisParams), '', Winapi.Windows.SW_HIDE);
不幸的是, ShellExecute
总是会成功,不管 move 操作是否成功(例如,如果目标文件存在且为只读文件,或者目标磁盘已写保护,那么move动作都会失败)。
Unfortunately, ShellExecute
always gives back success, regardless of whether the move action was successful or not (the move action would fail for example if the target file exists and it is read-only or if the target disk is write-protected).
那么在上述情况下的 move 操作失败时,如何获得通知?
So how can I get notified if the move action in the above case fails?
推荐答案
也许您可以使用管道和加载生成的文件
ex。移动A.txt B.TXT> result.txt
Perhaps you can use pipes and the load generated fileex. move A.txt B.TXT >result.txt
procedure TForm1.Button1Click(Sender: TObject);
var
stToDo: string;
sl1 : TSTringList ;
MyResult : boolean ;
begin
stToDo := '/C move "C:\Users\awr\Desktop\D2\a.txt" "C:\Users\awr\Desktop\D2\b.txt" >C:\Users\awr\Desktop\D2\Result.txt';
ShellExecute(Application.Handle, 'runas', 'cmd.exe' ,PChar(stToDo),'', SW_HIDE);
sl1 := TSTringList.create ;
sl1.LoadFromFile('C:\Users\awr\Desktop\D2\Result.txt');
MyResult := trim(sl1.text) <> '' ;
sl1.Free ;
如果移动失败, result.txt为空
If the move fail "result.txt" is empty
这篇关于如果cmd.exe无法成功移动文件,是否会收到通知?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!