如下图的附件中,添加了文件之后,在文件列表里显示了带有路径的文件名,我想问,怎么得到不带路径的文件名,请问有什么方法? 谢谢!能有代码是最好的了。

解决方案 »

  1.   

    本帖最后由 bcrun 于 2011-01-25 08:15:38 编辑
      

  2.   


    Public Function GetFileName(flname As String) As String
        
        'Get the filename without the path or extension.
        'Input Values:
        '   flname - path and filename of file.
        'Return Value:
        '   GetFileName - name of file without the extension.
        
        Dim posn As Integer, i As Integer
        Dim fName As String
        
        posn = 0
        'find the position of the last "\" character in filename
        For i = 1 To Len(flname)
            If (Mid(flname, i, 1) = "\") Then posn = i
        Next i    'get filename without path
        fName = Right(flname, Len(flname) - posn)    'get filename without extension
        posn = InStr(fName, ".")
            If posn <> 0 Then
                fName = Left(fName, posn - 1)
            End If
        GetFileName = fName
    End Function这个是提取文件名的函数,不知道是否是你要的
      

  3.   

    Option Explicit '强制宣告定义变量
    Dim AppDisk$, aa$, jj%  '定义变量
    Private Sub Form_Load()
       '将窗体居中显示 (屏幕宽度减去窗体的宽度)除以2 , (屏幕高度减去窗体的高度)除以2
       Me.Move (Screen.Width - Me.Width) \ 2, (Screen.Height - Me.Height) \ 2
       '将本地路径赋值给变量 AppDisk
       AppDisk = IIf(Right(App.Path, 1) = "\", App.Path, App.Path & "\")
       aa = AppDisk & "test.mp3"
    End SubPrivate Sub Command1_Click()
       MsgBox "文件名称是:" & GetFileNm(aa) '调用涵数返回
       MsgBox "文件括展名是:" & GetExtNm(aa) '调用涵数返回
    End SubPublic Function GetFileNm(Fnm$) As String
       jj = InStrRev(Fnm, "\") 'InStrRev是Instr涵数的反向,从后面找起
       If jj > 0 Then GetFileNm = Mid(Fnm, jj + 1)
    End FunctionPublic Function GetExtNm(Fnm$) As String
       jj = InStrRev(Fnm, ".")
       If jj > 0 Then GetExtNm = Mid(Fnm, jj + 1)
    End Function
      

  4.   

    如下图的附件中,添加了文件之后看不到图, 如果你是用了 CommonDialog控件 则CommonDialog1.ShowOpen
    MsgBox CommonDialog1.FileTitle 就是不带路径的文件名
      

  5.   

    CommonDialog控件CommonDialog1.ShowOpen
    MsgBox CommonDialog1.FileName 就是带路径的文件名
    MsgBox CommonDialog1.FileTitle 就是不带路径的文件名
    MsgBox Replace(CommonDialog1.FileName,CommonDialog1.FileTitle,"") 就是不带文件名的路径
      

  6.   

    不知道图怎么添加的。
    没有用 CommonDialog控件原代码如下:Private Sub cmdOpen_Click()
      On Error GoTo errHandle
      
      Dim i As Integer
      Dim y As Integer
      Dim Z As Integer
      Dim fileNames() As String                                   '用于打开文件时的数组
      
     ' With comDlg
     '    .CancelError = True
     '     .MaxFileSize = 32767
     '     .flags = cdlOFNHideReadOnly Or cdlOFNAllowMultiselect Or cdlOFNExplorer  ' Or cadlOFNNoDereferenceLinks
     '     .DialogTitle = "选择文件"
     '     .Filter = "图形文件(*.dwg)|*.dwg|所有文件(*.*)|*.*"
     '    .fileName = ""
     '     .ShowOpen
     ' End With
    ' 打开对话框
      
       Dim ofn As OPENFILENAME
       Dim strFileName As String
       
       
       ofn.lStructSize = Len(ofn)                                    'Set   the   structure  size
       ofn.hwndOwner = Me.hWnd
       ofn.hInstance = App.hInstance
       ofn.lpstrFilter = "图形文件(*.dwg)" & Chr$(0) & "*.dwg" + Chr$(0) + "所有文件(*.*)" + Chr$(0) + "*.*" + Chr$(0)      ' 文件扩展名
       ofn.lpstrFile = Space$(32767 - 1)                             'Create   a   buffer
       ofn.nMaxFile = 32767                                          'Set   the   maximum   number   of   chars
       ofn.lpstrFileTitle = Space$(254)                              'Create   a   buffer
       ofn.nMaxFileTitle = 255                                       'Set   the   maximum   number   of   chars
       ofn.lpstrInitialDir = "C:\"                                   ' 文件初始路径
       ofn.lpstrTitle = "选择文件"                                   ' 指定对话框的标题
       ofn.flags = &H80000 + &H200 + &H4 + &H100000   Dim rt As Long
       rt = GetOpenFileName(ofn)
       
       If rt >= 1 Then
      '   strFileName = ofn.lpstrFile
      '   strFileName = Left(strFileName, InStr(strFileName, Chr(0)) - 1)
       
       'MsgBox strFileName    strFileName = Trim(ofn.lpstrFile)
        Dim arr
        Dim j     As Long
        strFileName = Left(strFileName, Len(strFileName) - 2)             '除去结尾符号
        arr = Split(strFileName, Chr(0))                                  '不同的系统分割符可能不同,我这是在2000下
            
            Dim path     As String
            Dim file1     As String
            
            j = InStrRev(arr(0), "\ ")
            lstFile.Clear
            If j > 0 Then
                    path = Left(arr(0), j - 1)
                    If UBound(arr) >= 1 Then
                            For j = 1 To UBound(arr)
                                  'If arr(1) = " " Then
                                  '          List1.AddItem ofn.lpstrFile
                                  '          Exit Sub
                                  'End If
                                  lstFile.AddItem path + "\ " + arr(j)
                            Next
                    Else
                            lstFile.AddItem strFileName
                    End If
            End If
        End If   
         
      'comDlg.fileName = comDlg.fileName & Chr(0) '这些文件名是用空字符chr(0)分隔符,而不是空格分隔符分开
      strFileName = strFileName & Chr(0)
      
      Z = 1
      For i = 1 To Len(strFileName)
          'InStr函数,返回Variant(Long),指定一个字符串在另一个字符串中最先出现的位置
          '语法InStr(起点位置, string1, string2)
          i = InStr(Z, strFileName, Chr(0))
          If i = 0 Then Exit For
          ReDim Preserve fileNames(y)
          'Mid函数,返回Variant(String),其中包含字符串中指定数量的字符
          '语法Mid(String, start[, length])
          fileNames(y) = Mid(strFileName, Z, i - Z)
          Z = i + 1
          y = y + 1
      Next i
      
      '向列表框中添加对象
      Dim count As Integer
      count = lstFile.ListCount
      If y = 1 Then
        If Not HasItem(fileNames(y - 1)) Then
          lstFile.AddItem fileNames(y - 1), count
        End If
      Else
        For i = 1 To y - 1
          If StrComp(Right$(fileNames(0), 1), "\") = 0 Then
            fileNames(i) = fileNames(0) & fileNames(i)
          Else
            fileNames(i) = fileNames(0) & "\" & fileNames(i)
          End If
          
          If Not HasItem(fileNames(i)) Then
            lstFile.AddItem fileNames(i), i - 1 + count
          End If
        
        Next i
      End If
      
    errHandle:
     
    SumOfList.Text = lstFile.ListCount        '计算列表框中的列表项
    End SubPublic Function HasItem(ByVal strDwgName As String) As Boolean
      HasItem = False
      
      Dim i As Integer
      For i = 0 To lstFile.ListCount - 1
        If StrComp(lstFile.List(i), strDwgName, vbTextCompare) = 0 Then
          HasItem = True
          Exit Function
        End If
      Next i
    End Function
      

  7.   

    你这是自定义 利用comdlg32.dll 的CommonDialog,将选中的文件添加进ListBox里面.你要抄也要抄完整些,API与TYpe结构体你都缺了......不带路径就是 MsgBox ofn.lpstrFileTitle