vb.net IO.StreamWriter仅适用于调试模式

时间:2013-06-03 09:51:32

标签: vb.net

它在调试模式下完美运行,但它给了我'进程无法访问该文件,因为当我运行构建的进程并按下Save按钮时,它正被另一个进程使用'未处理的异常错误(它加载但不保存之后)那)。 请给我任何建议。

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

2 个答案:

答案 0 :(得分:2)

不确定这可能是答案,但我会在此发帖,因为我可以更好地格式化代码 如果这改变了什么,请告诉我

将您的阅读功能更改为

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

或者只是

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

答案 1 :(得分:0)

通常这意味着权限问题。尝试以管理员身份运行Visual Studio以消除这种可能性。