如何使用文件选择框来取得文件路径?

 所属目录:Java   |   类型:技术问答   |   时间:2007-05-21
 问题:

我想实现的是,按下一个按钮,然后弹出一个文件选择框(就是选择一个文件作用的),比如返回的一个值"c:\abcd\1234.exe",有什么办法可以从中取得"c:\abcd\"?

· 网友精彩回答:

发表者:ynsmcn

也可以直接选择需要文件的父文件夹  
   
  public   type   browseinfo  
            hwndowner   as   long  
            pidlroot   as   long  
            pszdisplayname   as   long  
            lpsztitle   as   long  
            ulflags   as   long  
            lpfncallback   as   long  
            lparam   as   long  
            iimage   as   long  
  end   type  
   
  public   const   bif_returnonlyfsdirs   =   1  
  public   const   max_path   =   260  
   
  public   declare   sub   cotaskmemfree   lib   "ole32.dll"   (byval   hmem   as   long)  
  public   declare   function   lstrcat   lib   "kernel32"   alias   "lstrcata"   (byval   lpstring1   as   string,   byval   lpstring2   as   string)   as   long  
  public   declare   function   shbrowseforfolder   lib   "shell32"   (lpbi   as   browseinfo)   as   long  
  public   declare   function   shgetpathfromidlist   lib   "shell32"   (byval   pidlist   as   long,   byval   lpbuffer   as   string)   as   long  
   
  public   function   browseforfolder(hwndowner   as   long,   sprompt   as   string)   as   string  
             
          declare   variables   to   be   used  
            dim   inull   as   integer  
            dim   lpidlist   as   long  
            dim   lresult   as   long  
            dim   spath   as   string  
            dim   udtbi   as   browseinfo  
   
          initialise   variables  
            with   udtbi  
                  .hwndowner   =   hwndowner  
                  .lpsztitle   =   lstrcat(sprompt,   "")  
                  .ulflags   =   bif_returnonlyfsdirs  
                  .pidlroot   =   0&      
            end   with  
   
          call   the   browse   for   folder   api  
            lpidlist   =   shbrowseforfolder(udtbi)  
             
          get   the   resulting   string   path  
            if   lpidlist   then  
                  spath   =   string$(max_path,   0)  
                  lresult   =   shgetpathfromidlist(lpidlist,   spath)  
                  call   cotaskmemfree(lpidlist)  
                  inull   =   instr(spath,   vbnullchar)  
                  if   inull   then   spath   =   left$(spath,   inull   -   1)  
            end   if  
   
          if   cancel   was   pressed,   spath   =   ""  
            browseforfolder   =   spath  
   
  end   function  
   
  可以参考:  
  http://dev.csdn.net/develop/article/27/27156.shtm

发表者:jxgzay

private   sub   command1_click()  
          dim   x()   as   string   定义动态数组  
          commondialog1.showopen  
          file_name   =   commondialog1.filename  
          x   =   split(file_name,   "\")   以"\"分隔  
          n   =   ubound(x)  
          redim   preserve   x(n   -   1)   数组下标减少1  
          file_path   =   join(x,   "\")   合并  
          msgbox   "文件的路径是:"   &   vbcrlf   &   file_path  
  end   sub  
 

发表者:myhgyp

msgbox   mid("c:\abcd\1234.exe",   1,   instrrev("c:\abcd\1234.exe",   "\"))  
  或  
  dim   str   as   string  
  str   =   "c:\abcd\1234.exe"  
  msgbox   mid(str,   1,   instrrev(str,   "\"))

.
处理 SSI 文件时出错
© 2006-2008 All Rights Reserved