Create and write to a text file in vb.net

I am creating a small vb.net application and I am trying to write a list of results from a list to a text file. I looked online and found the code to open the save file dialog and write a text file. When I click "Save" in the save file dialog box, I get an "IOException" message with the message "The process cannot access the file" C: \ thethe.txt "because it is being used by another process." The text file is created in the right place, but empty. The application terminates in this line "Dim fs As New FileStream (saveFileDialog1.FileName, FileMode.OpenOrCreate, FileAccess.Write)" Thanks in advance for any help.

Private Sub btnSave_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnSave.Click
    Dim myStream As Stream
    Dim saveFileDialog1 As New SaveFileDialog()

    saveFileDialog1.Filter = "txt files (*.txt)|*.txt|All files (*.*)|*.*"
    saveFileDialog1.FilterIndex = 2
    saveFileDialog1.RestoreDirectory = True

    If saveFileDialog1.ShowDialog() = DialogResult.OK Then
        myStream = saveFileDialog1.OpenFile()
        If (myStream IsNot Nothing) Then
            Dim fs As New FileStream(saveFileDialog1.FileName, FileMode.OpenOrCreate, FileAccess.Write)
            Dim m_streamWriter As New StreamWriter(fs)
            m_streamWriter.Flush()
            'Write to the file using StreamWriter class
            m_streamWriter.BaseStream.Seek(0, SeekOrigin.Begin)
            'write each row of the ListView out to a tab-delimited line in a file
            For i As Integer = 0 To Me.ListView1.Items.Count - 1
                m_streamWriter.WriteLine(((ListView1.Items(i).Text & vbTab) + ListView1.Items(i).SubItems(0).ToString() & vbTab) + ListView1.Items(i).SubItems(1).ToString())
            Next
            myStream.Close()
        End If
    End If

End Sub
+3
3

StreamWriter:

    Private Sub btnSave_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnSave.Click

        Dim saveFileDialog1 As New SaveFileDialog()

        saveFileDialog1.Filter = "txt files (*.txt)|*.txt|All files (*.*)|*.*"
        saveFileDialog1.FilterIndex = 2
        saveFileDialog1.RestoreDirectory = True

        If saveFileDialog1.ShowDialog() = DialogResult.OK Then
            Using sw As New IO.StreamWriter(saveFileDialog1.FileName, False)
                'write each row of the ListView out to a tab-delimited line in a file 
                For i As Integer = 0 To Me.ListView1.Items.Count - 1
                    sw.WriteLine(((ListView1.Items(i).Text & vbTab) + ListView1.Items(i).SubItems(0).ToString() & vbTab) + ListView1.Items(i).SubItems(1).ToString())
                Next
            End Using
        End If

    End Sub
+5

, SaveFileDialog.OpenFile - :

Dim fs As New FileStream(...)

, ? ( OpenFile.)

(Btw, a Using , .)

+5

You have a working process that has not provided the resource properly. Maybe your debugger?

[EDIT] Sorry, I am not reading the sample code correctly ...

+1
source

Source: https://habr.com/ru/post/1737168/


All Articles