如何删除完整路径名称 [英] How Do I Remove The Full Path Name

查看:73
本文介绍了如何删除完整路径名称的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

此代码中的所有内容都有效,但它列出了完整的路径名,我想删除它。所有我想这样做,只生成所选目录中没有完整路径名称的文件列表。



这里是我需要帮助的代码。



everything in this code works, however it lists the full pathname and i would like to remove that. All i would like for this to do, is produce only a list of files in the selected directory without the fullpath name.

here is the code i need help with.

Private Sub btnDirectory_Click(sender As Object, e As EventArgs) Handles btnDirectory.Click
    'This open up a dialog for the user to choose a folder.
    Dim objFolderDlg As System.Windows.Forms.FolderBrowserDialog
    objFolderDlg = New System.Windows.Forms.FolderBrowserDialog
    objFolderDlg.SelectedPath = "C:\Test"
    If objFolderDlg.ShowDialog() = DialogResult.OK Then
        'This is the location where the text file will be saved.
        Dim path As String = "C:\Users\" & Environment.UserName & "\list\Directory_List.txt"
        'This gets the list of files in the selected directory.
        Dim dirs As String() = Directory.GetFiles(objFolderDlg.SelectedPath)
        'Dim dirs As String() = System.IO.Directory.GetFileSystemEntries(objFolderDlg.SelectedPath)
        Dim fileName As String
        For Each fileName In dirs

        Next
        'Code to write the information to a text file.
        File.WriteAllLines(path, dirs)
        'Code to show the user the folder that they selected.
        'MessageBox.Show(objFolderDlg.SelectedPath)

    End If
End Sub



推荐答案

轻松完成System.Io.Path [ ^ ] class。
Easily done with the methods in the System.Io.Path[^] class.


这应该给你没有完整路径的文件名。
This should give you the filename without the full path.
For Each fileName As String In dirs
Dim nopath As String = FileIO.FileSystem.GetName(filename)
'code to write each  nopath  to your textfile
Next


Public Sub btnFileList_Click(sender As Object, e As EventArgs) Handles btnFileList.Click
    ' This opens up a Dialog for the user to choose a folder to list the contents in.
    Dim objFolderDlg As System.Windows.Forms.FolderBrowserDialog
    objFolderDlg = New System.Windows.Forms.FolderBrowserDialog
    objFolderDlg.SelectedPath = "U:\"
    If objFolderDlg.ShowDialog() = DialogResult.OK Then
        ' This will get the User Profile information.
        Dim user As String = Environment.GetEnvironmentVariable("userprofile")
        '
        ' This is the location where the text file will be saved.
        Dim chkpath As String = user & "\list"
        Dim path As String
        If Directory.Exists(chkpath) Then
            path = user & "\list\File_List.txt"
        Else
            path = "u:\" & Environment.UserName & "\list\File_list.txt"
            'Environment.GetFolderPath(Environment.SpecialFolder.UserProfile)
        End If
        '
        'Dim path As String = user & "\list\File_List.txt"
        Dim di As New DirectoryInfo(objFolderDlg.SelectedPath)
        ' Get a reference to each file in that directory.
        Dim fiArr As FileInfo() = di.GetFiles()
        ' Display the names of the files.

        Dim file As FileInfo
        For Each file In fiArr
            ' Code to write the information to a text file.
            My.Computer.FileSystem.WriteAllText(path, file.Name & Environment.NewLine, True)

        Next

        ' This adds a new line at the end of file.
        My.Computer.FileSystem.WriteAllText(path, vbCrLf & "-----------------------------------------------------------" & vbCrLf & vbCrLf, True)

        ' Code to show the user the folder that they selected.
        MessageBox.Show(objFolderDlg.SelectedPath)

        ' This code opens the file that was created.
        Dim fyle As String = path

        If System.IO.File.Exists(fyle) Then
            IO.File.Open(fyle, FileMode.Open, FileAccess.ReadWrite, FileShare.ReadWrite)
            'IO.File.Open(fyle, FileMode.Open)
        End If

        Process.Start(fyle)
        'System.Diagnostics.Process.Start(fyle)
    End If
End Sub


这篇关于如何删除完整路径名称的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆