Vb.net 如何在图片框中显示文件夹中的随机照片并打印出照片名称而无需扩展名

Vb.net 如何在图片框中显示文件夹中的随机照片并打印出照片名称而无需扩展名,vb.net,vb.net-2010,Vb.net,Vb.net 2010,我有一个名为“MyPhotos”的文件夹,我想在每次单击“Myphoto”中的命令按钮时显示一张随机照片,并在表单中打印出显示照片的名称,不带扩展名。我正在尝试以下代码,但仍然无法获取照片名称 Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As _ System.EventArgs) Handles Button1.Click Dim DirectoryPath As String = Applicati

我有一个名为“MyPhotos”的文件夹,我想在每次单击“Myphoto”中的命令按钮时显示一张随机照片,并在表单中打印出显示照片的名称,不带扩展名。我正在尝试以下代码,但仍然无法获取照片名称

Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As _ System.EventArgs) Handles Button1.Click
    Dim DirectoryPath As String = Application.ExecutablePath
    Directorypath = DirectoryPath.Substring(0, Directorypath.LastIndexOf("\bin")) & "\MyPhotos"
    Dim bm As New Bitmap(GetRandomImageFilePath(Directorypath))
    picImage.Image = bm
End Sub

Public Function GetRandomImageFilePath(ByVal folderPath As String) As String
    Dim files() As String = Directory.GetFiles(folderPath, "*.jpg")
    Dim random As Random = New Random()
    Return files(random.Next(0, files.Length))
End Function

有什么帮助吗?

您已经拥有GetRandomImageFilePath返回的完整路径。 如果只需要文件名,请在调用子文件中执行此操作

Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As _ System.EventArgs) Handles Button1.Click
    Dim DirectoryPath As String = Application.ExecutablePath
    Directorypath = DirectoryPath.Substring(0, Directorypath.LastIndexOf("\bin")) & "\MyPhotos"
    Dim imagePath as String = GetRandomImageFilePath(Directorypath)
    Dim fileInfo as New FileInfo(imagePath)
    Dim imageName as String = fileInfo.Name
    Dim bm As New Bitmap(imagePath)
    picImage.Image = bm
End Sub

我将此添加到您的按钮方法中。我用的只是文件信息来获取细节

    Dim finfo As FileInfo = New FileInfo(GetRandomImageFilePath(DirectoryPath))

    Dim filename As String = finfo.Name.Replace(finfo.Extension, "")
    Dim bm As New Bitmap(finfo.FullName)
    picImage.Image = bm

非常感谢。我刚刚从'imageName'
Dim imageName中删除了扩展名,即String=System.IO.Path.GetFileNameWithoutExtension(imagePath)End Sub