vb怎么实现单个文件上传到文件夹

vb
1165
2024/2/20 9:37:27
栏目: 编程语言
开发者测试专用服务器限时活动,0元免费领,库存有限,领完即止! 点击查看>>

在VB中实现单个文件上传到文件夹可以使用以下代码:

Imports System.IO

Public Class Form1

    Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
        Dim openFileDialog1 As New OpenFileDialog()

        openFileDialog1.Filter = "All files (*.*)|*.*"
        openFileDialog1.Multiselect = False

        If openFileDialog1.ShowDialog() = DialogResult.OK Then
            Dim selectedFile As String = openFileDialog1.FileName
            Dim folderPath As String = "C:\YourFolderPath\" ' 指定文件夹路径

            Dim fileName As String = Path.GetFileName(selectedFile)
            Dim destinationFile As String = Path.Combine(folderPath, fileName)

            File.Copy(selectedFile, destinationFile)

            MessageBox.Show("File uploaded successfully.")
        End If
    End Sub

End Class

在上面的代码中,首先创建一个OpenFileDialog对象来选择要上传的文件,然后获取所选文件的路径并指定文件夹路径。接着使用File.Copy方法将选择的文件复制到指定的文件夹中,并显示上传成功的消息框。

辰迅云「云服务器」,即开即用、新一代英特尔至强铂金CPU、三副本存储NVMe SSD云盘,价格低至29元/月。点击查看>>

推荐阅读: vb中picturebox控件怎么使用