您的位置:宽带测速网 > 编程知识 > vb怎么实现单个文件上传到文件夹

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

2025-06-17 15:03来源:互联网 [ ]

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

Imports System.IOPublic Class Form1Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.ClickDim openFileDialog1 As New OpenFileDialog()openFileDialog1.Filter = "All files (*.*)|*.*"openFileDialog1.Multiselect = FalseIf openFileDialog1.ShowDialog() = DialogResult.OK ThenDim selectedFile As String = openFileDialog1.FileNameDim 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 IfEnd SubEnd Class

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

VB