Private Sub button1_Click(ByVal sender As Object, ByVal e As System.EventArgs)
Dim myStream As Stream = Nothing
Dim openFileDialog1 As New OpenFileDialog()
openFileDialog1.InitialDirectory = "c:\"
openFileDialog1.Filter = "txt files (*.txt)|*.txt|All files (*.*)|*.*"
openFileDialog1.FilterIndex = 2
openFileDialog1.RestoreDirectory = True
If openFileDialog1.ShowDialog() = System.Windows.Forms.DialogResult.OK Then
Try
myStream = openFileDialog1.OpenFile()
If (myStream IsNot Nothing) Then
' Insert code to read the stream here.
End If
Catch Ex As Exception
MessageBox.Show("Cannot read file from disk. Original error: " & Ex.Message)
Finally
' Check this again, since we need to make sure we didn't throw an exception on open.
If (myStream IsNot Nothing) Then
myStream.Close()
End If
End Try
End If
End Sub
Code (VB.NET)
Protected Sub button1_Click(sender As Object, e As System.EventArgs)
Dim myStream As Stream
Dim openFileDialog1 As New OpenFileDialog()
openFileDialog1.InitialDirectory = "c:\"
openFileDialog1.Filter = "txt files (*.txt)|*.txt|All files (*.*)|*.*"
openFileDialog1.FilterIndex = 2
openFileDialog1.RestoreDirectory = True
If openFileDialog1.ShowDialog() = DialogResult.OK Then
myStream = openFileDialog1.OpenFile()
If Not (myStream Is Nothing) Then
' Insert code to read the stream here.
myStream.Close()
End If
End If
End Sub
Private Sub BtnCopy_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles BtnCopy.Click
Dim ofd As New OpenFileDialog
ofd.Filter = "song (*.mp3)|*.mp3"
If ofd.ShowDialog() = Windows.Forms.DialogResult.OK Then
FileCopy(DialogResult, "C:/new folder")
End If
End Sub
ส่วนการเก็บลง Database ให้ดูตัวอย่างของ ASP.NET ครับ
Dim od As New OpenFileDialog()
od.Filter = "XLS files|*.xls"
od.Multiselect = True
If od.ShowDialog() = System.Windows.Forms.DialogResult.OK Then
Dim tempFolder As String = System.IO.Path.GetTempPath()
For Each fileName As String In od.FileNames
System.IO.File.Copy(fileName, tempFolder & "\" & System.IO.Path.GetFileName(fileName))
Next
End If