在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
方法將選擇的文件復制到指定的文件夾中,并顯示上傳成功的消息框。