Using the open file and save file dialogs in vb.net

后端 未结 3 787
伪装坚强ぢ
伪装坚强ぢ 2021-01-28 16:03

Once a user has selected a file with the open file dialog, how can I handle this action? For example, if the user has selected a .txt file and has opened it, how can it get the

相关标签:
3条回答
  • 2021-01-28 16:28

    You want to handle the FileOk event:

    Private Sub OpenFileDialog1_FileOk(ByVal sender As System.Object, ByVal e As System.ComponentModel.CancelEventArgs) Handles OpenFileDialog1.FileOk
        Dim path As String = OpenFileDialog1.FileName
    
        If fileIsBitmap Then
            ' say the file is a bitmap image '
            Dim bmp As New Bitmap(path)
    
            ' rotate the image 90 degrees '
            bmp.RotateFlip(RotateFlipType.Rotate90FlipNone)
    
            ' save the image '
            bmp.Save(path)
    
        ElseIf fileIsTextFile Then
            ' or say the file is a text file '
            Dim fs As New IO.FileStream(path, IO.FileMode.Append)
            Dim sr As New IO.StreamWriter(fs)
    
            ' write a new line at the end of the file '
            sr.WriteLine("This is a new line.")
    
            ' close the FileStream (this saves the file) '
            sr.Close()
            fs.Close()
        End If
    End Sub
    
    0 讨论(0)
  • 2021-01-28 16:36

    Here is some good example: https://web.archive.org/web/1/http://blogs.techrepublic%2ecom%2ecom/programming-and-development/?p=481.

    This is a trivial question that could be answered by Google in a matter of seconds.

    0 讨论(0)
  • 2021-01-28 16:48
    Dim dlg_open As New OpenFileDialog()
    If (dlg_open.Show() <> DialogResult.OK) Then Return
    
    'if a textfile, then
    Dim content As String() = IO.File.ReadAllLines(dlg_open.FileName)
    
    'if an image, then
    Dim img As New Bitmap(dlg_open.FileName)
    

    You should put Try...Catch blocks around all operations dealing with IO, you will not be able to prevent all exceptions.

    0 讨论(0)
提交回复
热议问题