Vb.net SaveFileDialog生成无法识别的字符

Vb.net SaveFileDialog生成无法识别的字符,vb.net,filesystems,savefiledialog,Vb.net,Filesystems,Savefiledialog,我的程序应该创建一个文本文件,但它正在将不可见的无法识别的字符添加到文本文件的开头。我只能在扫描时看到不可见的未识别字符。我可以通过使用word pad而不是记事本打开文件,输入字符,然后退格保存来消除字符。这修复了文件,但我需要更正导致它的代码。我是否弄乱了文件类型,并添加了一个随机字符 以下是对话框和文件名/类型的代码: Try Dim strLocalFilePath As String Dim today A

我的程序应该创建一个文本文件,但它正在将不可见的无法识别的字符添加到文本文件的开头。我只能在扫描时看到不可见的未识别字符。我可以通过使用word pad而不是记事本打开文件,输入字符,然后退格保存来消除字符。这修复了文件,但我需要更正导致它的代码。我是否弄乱了文件类型,并添加了一个随机字符

以下是对话框和文件名/类型的代码:

                Try
            Dim strLocalFilePath As String
            Dim today As String = String.Format("{0:yyyy-MM-dd}", DateTime.Now)

            Dim dlgSaveFile As New SaveFileDialog
            dlgSaveFile.InitialDirectory = CustomSettings.UserSettings.Settings.EligibilityExport_LocalFilePath
            dlgSaveFile.Filter = "Text Files (*.txt)|*.txt|All Files (*.*)|*.*"
            dlgSaveFile.FilterIndex = 1
            dlgSaveFile.OverwritePrompt = True
            dlgSaveFile.FileName = "WWW_EligibilityExport-" & today

            'Dim saveToFolder = System.IO.Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.Desktop), "SISCO")
            'If Not (System.IO.Directory.Exists(saveToFolder)) Then
            'System.IO.Directory.CreateDirectory(saveToFolder)
            'End If

            If dlgSaveFile.ShowDialog = System.Windows.Forms.DialogResult.OK Then
                strLocalFilePath = dlgSaveFile.FileName
                CustomSettings.UserSettings.Settings.EligibilityExport_LocalFilePath = My.Computer.FileSystem.GetFileInfo(strLocalFilePath).Directory.FullName
                CustomSettings.UserSettings.Settings.Save()
            Else
                Exit Sub
            End If

            If intSelectedCount > 0 Then
                Me.bsEligibilityExportExportCurrentEmployeeDataSelect.EndEdit()
                If ExportFile(strLocalFilePath) Then
                    MessageBox.Show("Export file successfully created!", "Success", MessageBoxButtons.OK, MessageBoxIcon.Information)
                    Me.RefreshData()
                Else
                    MessageBox.Show("Unable to create export file.", "Error...", MessageBoxButtons.OK)
                End If
            Else
                MessageBox.Show("No rows selected for export.", "Error...", MessageBoxButtons.OK)
            End If
以下是构建文本的代码:

     Dim blnReturn As Boolean = False
        Dim objHeader = CType(CType(Me.dgvExportSummary.CurrentRow.DataBoundItem, DataRowView).Row, dsEmployee.EligibilityExport_ExportCurrentEmployeeData_SelectRow)
        Try
            Dim siscoService As Sisco834Service = New Sisco834Service()

            bsEligibilityExportExportCurrentEmployeeDataSelect.EndEdit()
            Dim isaID As Integer = 100000000
            Dim rand As New Random()
            Dim header As SiscoHeader = New SiscoHeader(Now, isaID)
            header.BeginningStatement.ReferenceIdentification = rand.Next(11110, 99999).ToString()
            Dim footer As SiscoFooter = New SiscoFooter(header)
            Dim document As SiscoDocument = New SiscoDocument()
            document.Header = header
            document.Footer = footer
            For Each dgvr As DataGridViewRow In Me.dgvExportSummary.Rows
                Dim objRowExportSummary = CType(CType(dgvr.DataBoundItem, DataRowView).Row, dsEmployee.EligibilityExport_ExportCurrentEmployeeData_SelectRow)
                If objRowExportSummary.Selected Then
                    Dim eligibilityExport As EligibilityExportCurrent = New EligibilityExportCurrent()
                    With eligibilityExport
                        .Address1 = If(objRowExportSummary.IsAddress1Null, String.Empty, objRowExportSummary.Address1)
                        .City = If(objRowExportSummary.IsCityNull, String.Empty, objRowExportSummary.City)
                        .DepartmentName = If(objRowExportSummary.IsDepartmentNameNull, String.Empty, objRowExportSummary.DepartmentName)
                        .DivisionName = If(objRowExportSummary.IsDivisionNameNull, String.Empty, objRowExportSummary.DivisionName)
                        .DivisionNumber = If(objRowExportSummary.IsDivisionNumberNull, Nothing, objRowExportSummary.DivisionNumber)
                        .EmployeeID = objRowExportSummary.EmployeeID
                        .ExportRowData = If(objRowExportSummary.IsExportRowDataNull, String.Empty, objRowExportSummary.ExportRowData)
                        .FirstName = objRowExportSummary.FirstName
                        .Header = If(objRowExportSummary.IsHeaderNull, String.Empty, objRowExportSummary.Header)
                        .HireDate = If(objRowExportSummary.IsHireDateNull, Date.MinValue, objRowExportSummary.HireDate)
                        .LastName = objRowExportSummary.LastName
                        .MiddleName = If(objRowExportSummary.IsMiddleNameNull, String.Empty, objRowExportSummary.MiddleName)
                        .Selected = If(objRowExportSummary.IsSelectedNull, True, objRowExportSummary.Selected)
                        .State = If(objRowExportSummary.IsStateNull, String.Empty, objRowExportSummary.State)
                        .WorkcenterName = If(objRowExportSummary.IsWorkcenterNameNull, String.Empty, objRowExportSummary.WorkcenterName)
                        .ZipCode = If(objRowExportSummary.IsZipCodeNull, String.Empty, objRowExportSummary.ZipCode)
                    End With
                    document.Subscribers.Add(siscoService.ConvertEligibilityExportCurrentToSiscoSubscriber(eligibilityExport, footer))
                End If
            Next
            Dim str As String = siscoService.CreateSiscoDocumentString(document)
            My.Computer.FileSystem.WriteAllText(strLocalFilePath, str + Environment.NewLine + Environment.NewLine, True)
            blnReturn = True

谢谢你@Chase Rocker。。。他的答案是:我猜这是一个字节顺序标记(BOM)。如果未指定编码,WriteAllText方法将添加BOM表。在您的True参数之后,尝试添加此编码参数:New System.Text.UTF8Encoding(False)–18分钟前

我猜这是一个字节顺序标记(BOM)。如果未指定编码,则
writealText
方法会添加BOM。在
True
param之后,尝试添加此编码参数:
New System.Text.UTF8Encoding(False)
,这正是问题所在。泰:)@ClinDom看起来已经解决了。我会在更多的匿名傻瓜投票之前,将此标记为已回答或已结束。我在投票,因为这对我来说是一个独特的问题。