如何排除文件名字符串中的扩展名

时间:2018-01-17 12:43:19

标签: xml vb.net

以下是我在ListBox中显示XML文件的代码,但我希望它们不带扩展名:

Dim Directory = Application.StartupPath + "\orders\"
Dim Files() As System.IO.FileInfo
Dim DirInfo As New System.IO.DirectoryInfo(Directory)
Dim StrClean As String

Sub ShowFiles()
    Files = DirInfo.GetFiles("*.xml", IO.SearchOption.TopDirectoryOnly)
    For Each File In Files
        lstFiles.Items.Add(File)
    Next
End Sub

如何展示没有.xml扩展名的文件?

2 个答案:

答案 0 :(得分:1)

如评论中所述,请使用System.IO.Path.GetFileNameWithoutExtension

Sub ShowFiles()
    Files = DirInfo.GetFiles("*.xml", IO.SearchOption.TopDirectoryOnly)
    For Each File In Files
        lstFiles.Items.Add(Path.GetFileNameWithoutExtension(File.Name))
    Next
End Sub

此外,最好使用Path.Combine将路径与foldernames连接起来。
您不必担心前导/尾随反斜杠:

Dim Directory As String = Path.Combine(Application.StartupPath, "orders")

答案 1 :(得分:0)

您可能希望查看我上周为我转换为VB.net的c#编写的以下代码

Imports System
Imports System.Collections.Generic
Imports System.Linq
Imports System.Text
Imports System.Xml
Imports System.Xml.Linq
Imports System.IO

Module Module1
    Dim FILENAME As String = "c:\temp\test.xml"
    Dim FOLDER As String = "c:\temp"
    Dim writer As XmlWriter
    Sub Main()
        Dim settings As New XmlWriterSettings()
        settings.Indent = True

        writer = XmlWriter.Create(FILENAME, settings)
        writer.WriteStartDocument(True)

        Dim info As New DirectoryInfo(FOLDER)
        WriteTree(info)

        writer.WriteEndDocument()
        writer.Flush()
        writer.Close()
        Console.WriteLine("Enter Return")
        Console.ReadLine()
    End Sub

    Function WriteTree(info As DirectoryInfo) As Long

        Dim size As Long = 0
        writer.WriteStartElement("Folder")
        Try

            writer.WriteAttributeString("name", info.Name)
            writer.WriteAttributeString("numberSubFolders", info.GetDirectories().Count().ToString())
            writer.WriteAttributeString("numberFiles", info.GetFiles().Count().ToString())
            writer.WriteAttributeString("date", info.LastWriteTime.ToString())


            For Each childInfo As DirectoryInfo In info.GetDirectories()

                size += WriteTree(childInfo)
            Next childInfo

        Catch ex As Exception

            Dim errorMsg As String = String.Format("Exception Folder : {0}, Error : {1}", info.FullName, ex.Message)
            Console.WriteLine(errorMsg)
            writer.WriteElementString("Error", errorMsg)
        End Try

        Dim fileInfo As FileInfo()
        Try

            fileInfo = info.GetFiles()

        Catch ex As Exception

            Dim errorMsg As String = String.Format("Exception FileInfo : {0}, Error : {1}", info.FullName, ex.Message)
            Console.WriteLine(errorMsg)
            writer.WriteElementString("Error", errorMsg)
        End Try

        If Not fileInfo Is Nothing Then

            For Each finfo As FileInfo In fileInfo
                Try

                    writer.WriteStartElement("File")
                    writer.WriteAttributeString("name", RemoveExtension(finfo.Name))
                    writer.WriteAttributeString("size", finfo.Length.ToString())
                    writer.WriteAttributeString("date", info.LastWriteTime.ToString())
                        writer.WriteEndElement()
                    size += finfo.Length

                Catch ex As Exception

                    Dim errorMsg As String = String.Format("Exception File : {0}, Error : {1}", finfo.FullName, ex.Message)
                    Console.WriteLine(errorMsg)
                    writer.WriteElementString("Error", errorMsg)

                End Try
            Next finfo
        End If

        writer.WriteElementString("size", size.ToString())
        writer.WriteEndElement()
        Return size

    End Function

    Function RemoveExtension(fname As String) As String
        Return fname.Substring(0, fname.LastIndexOf("."))
    End Function

End Module