user1785594
user1785594

Reputation:

vb.net IO.StreamWriter only works in debug mode

It works perfectly in debug mode but it gives me 'The process cannot access the file because it is being used by another process' unhandled exception error when I run the built one and press the Save button(It loads but not saves after that). Any advice, please.

Imports System.IO
Public Class DL1

    Function DirExists(ByVal DirName As String) As Boolean
        On Error GoTo ErrorHandler
        DirExists = GetAttr(DirName) And vbDirectory
ErrorHandler:
    End Function
    Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load

    End Sub

    Private Sub LoadGlink_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles LoadGlink.Click

        GlinkList.Items.Clear()



        Dim fileReader As System.IO.StreamReader
        fileReader = _
        My.Computer.FileSystem.OpenTextFileReader("c:\Source\DL1\Glink.txt")

        Dim mystring() As String = fileReader.ReadToEnd.Split(vbNewLine)


        GlinkList.Items.AddRange(mystring)

        fileReader.Close()

        Me.Controls("Glinklist").Focus()
    End Sub

    Private Sub linktochrome_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles linktochrome.Click
        If GlinkList.SelectedItem IsNot Nothing Then
            ' selected item is sglink
            Dim sglink = GlinkList.SelectedItem.ToString


            Process.Start("C:\Users\1\AppData\Local\Google\Chrome\Application\chrome.exe", sglink)
        End If
        Me.Controls("Glinklist").Focus()

    End Sub

    Private Sub movetofol_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles movetofol.Click



        Dim strDir As String
        strDir = "C:\Users\1\Downloads\Glink\" & "\" & SFoltext.Text
        If DirExists(Trim(strDir)) = False Then
            MkDir(Trim(strDir))
        End If


        For Each f As String In Sresult1.Items
            Dim f_name As String = Path.GetFileName(f)
            My.Computer.FileSystem.MoveFile(f, strDir & "\" & f_name)
        Next
        Sresult1.Items.Clear()

        GlinkList.Items.Remove(GlinkList.SelectedItem)


    End Sub

    Private Sub ChecKDL_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles ChecKDL.Click

        Sresult1.Items.Clear()
        Dim fileList As System.Collections.ObjectModel.ReadOnlyCollection(Of String)
        fileList = My.Computer.FileSystem.GetFiles("C:\Users\1\Downloads\")


        For Each foundFile As String In fileList
            Sresult1.Items.Add(foundFile)
        Next

    End Sub

    Private Sub Bsave_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Bsave.Click
        Dim i As Integer


        'Saves Glinklist
        Dim path As String = System.IO.Path.Combine("c:\Source\DL1\", "Glink.txt")
        Using fs As New System.IO.FileStream(path, IO.FileMode.Create)

            Using w As IO.StreamWriter = New IO.StreamWriter(fs)
                For i = 0 To GlinkList.Items.Count - 1
                    w.WriteLine(GlinkList.Items.Item(i))
                Next
                w.Close()
            End Using
        End Using
    End Sub

    Private Sub RMselec_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles RMselec.Click
        GlinkList.Items.Remove(GlinkList.SelectedItem)
        Me.Controls("Glinklist").Focus()
    End Sub
End Class

Upvotes: 1

Views: 551

Answers (2)

Steve
Steve

Reputation: 216293

Not sure it this could be the answer, but I will post here because I can better format the code.
Let me know if this changes anything

Change your reading function to

Using fileReader = _
    My.Computer.FileSystem.OpenTextFileReader("c:\Source\DL1\Glink.txt")
    Dim mystring() = fileReader.ReadToEnd.Split(vbNewLine)
    GlinkList.Items.AddRange(mystring)
End Using

Or simply

    Dim mystring() = File.ReadAllLines()
    GlinkList.Items.AddRange(mystring)

Upvotes: 2

Vivek Bernard
Vivek Bernard

Reputation: 2073

Usually this means a permission issue. Try running Visual Studio as admin to eliminate that possibility.

Upvotes: 0

Related Questions