我想让选择CheckBox1Click后 在硬盘查找1.EXE 找到后执行,找不到ShowMessage 提示找不到

解决方案 »

  1.   

    CheckBox1Click后CheckBox1的状态可能是"选中"和"取消选中"
    思路:
    var
      isExist: boolean;
    if CheckBox1.Checked = true then
      isExist := FindFile();//FindFile为自定义函数
    if isExist = true then
    //存在,ShellExecute,执行
    else
    //不存在,showmessage........FindFile函数编写可以查看下面回复
    ----------------------------------
    查找任意类型的文件例子:自定义查找函数
    procedure tform1.FIND(
        //路径;
        path:string;
        //文件类型;
        atype:tstrings;
        //是否包含子目录;
        includ:bool);
    var SearchRec: TSearchRec;
        OLDDIR:STRING;
        i: integer;
    begin
    //检测路径是否正确
    if DirectoryExists(path)  then
    begin
      //进入该目录,查找其中的子目录和文件
      oldDir := GetCurrentDir;
      ChDir(path);
      //检测目录中所有类型文件
      FindFirst('*.*', faAnyFile, SearchRec);
     repeat
     //如果是目录并且不是.和..,还有查找包含子目录则递归调用find
      if(SearchRec.Attr and faDirectory > 0)and includ then
        begin
          if(SearchRec.Name[1]<>'.') then
          FIND(SearchRec.Name,atype,includ);
        end
     //如果是文件则检测后缀
     else
       {------------如果属于查找类型则加入listbox------------}
          with atype do
          for i:=0 to count-1 do
          begin
            if ExtractFileExt(SearchRec.Name)=strings[i] then
            ListBox1.Items.Add(SearchRec.Name);
         end;
        {-----------------------------------------------------}
         //继续查找,直到最后
     until (FindNext(SearchRec)<>0) ;
     //跳出上一层
     SetCurrentDir(oldDir);
    end
     else //路径错误就返回!;
     showmessage('something is 错误!');
    end ;
    将以下改为:
    //procedure TForm1.Button1Click(Sender: TObject);
    //var
    //  i:string;
    //begin
    //SelectDirectory('浏览目录','',i);
    //end;procedure TForm1.Button1Click(Sender: TObject);
    var
      i:string;
      aa:tstrings;
      rr:bool;//这个为是否查找子目录的标志
    begin
    aa:=tstringlist.Create;
    //这里你可以任意添加你想查找文件的类型
    aa.add('.rm');
    aa.add('.ram');
    aa.add('.mp3');
    //...
    rr := True;
    if SelectDirectory('浏览目录','',i) then
    begin
           if i[Length(i)]<>'\' then
           i := i + '\';
           Find(i,aa,rr);
    end;
    end;