StreamWriter 在写入文本文件中途停止写入

StreamWriter stops writing in the meddle of writing to a text file halfway

我有一个列表,其中包含来自 SQL 服务器 table 的 14 个值,我正在循环将值传递给带有 WriteLine 的 StreamWriter 对象,以将值以所需格式写入文本文件。我的问题是,在调试时我可以看到所有值都添加到我的打印变量中;然而,当循环完成时,它只打印 9 个值,第 9 个值在文本文件中被剪切。请参考我的截图。

Using resource As New DevelopmentEntities
            Dim sw As StreamWriter
            Dim list As New List(Of ContactU)
            list = resource.ContactUs.ToList()
            sw = My.Computer.FileSystem.OpenTextFileWriter(outputpath & "Web_ContactUs_" & countValue & ".txt", True)
            For Each item In list
                list = list.OrderBy(Function(x) item.Submitters_First_Name_First).ToList()
                countValue = countValue + 1
                If Not item.Entry_Id.ToString Is "" Then
                    Dim valueItem = list
                    Dim ssnValue = "UANPF" & item.Last_4_of_SSN.TrimStart.TrimEnd
                    Dim raw_date As Date = item.Entry_Date.TrimEnd
                    Dim entry_date As Date = raw_date.ToString("MM/dd/yyyy")
                    Dim concatinatedFilepath = textfilepath & item.Submitters_First_Name_First.Replace(" ", "_").TrimStart.TrimEnd _
                        & "_" & item.Submitters_Last_Name_Last.Replace(" ", "_").TrimStart.TrimEnd _
                        & "_" & item.Last_4_of_SSN.TrimStart _
                        & "_" & countValue
                    Dim print = filetype &
                        "|" & concatinatedFilepath &
                        ".txt" &
                        "|" & ssnValue &
                        "|" & incoming &
                        "|" & addresschange &
                        "|" & entry_date &
                        "|" & ITP
                    sw.WriteLine(print)
                Else
                    'Do nothing will clear invalid data
                End If
            Next

        End Using

试试这个:

Using resource As New DevelopmentEntities
        Dim sw As StreamWriter
        Dim list As New List(Of ContactU)
        list = resource.ContactUs.ToList()
        sw = My.Computer.FileSystem.OpenTextFileWriter(outputpath & "Web_ContactUs_" & countValue & ".txt", True)
        For Each item In list
            list = list.OrderBy(Function(x) item.Submitters_First_Name_First).ToList()
            countValue = countValue + 1
            If Not item.Entry_Id.ToString Is "" Then
                Dim valueItem = list
                Dim ssnValue = "UANPF" & item.Last_4_of_SSN.TrimStart.TrimEnd
                Dim raw_date As Date = item.Entry_Date.TrimEnd
                Dim entry_date As Date = raw_date.ToString("MM/dd/yyyy")
                Dim concatinatedFilepath = textfilepath & item.Submitters_First_Name_First.Replace(" ", "_").TrimStart.TrimEnd _
                    & "_" & item.Submitters_Last_Name_Last.Replace(" ", "_").TrimStart.TrimEnd _
                    & "_" & item.Last_4_of_SSN.TrimStart _
                    & "_" & countValue
                Dim print = filetype &
                    "|" & concatinatedFilepath &
                    ".txt" &
                    "|" & ssnValue &
                    "|" & incoming &
                    "|" & addresschange &
                    "|" & entry_date &
                    "|" & ITP
                sw.WriteLine(print)
            Else
                'Do nothing will clear invalid data
            End If
        Next
        sw.Close()
    End Using