SaveFileDialog生成无法识别的字符

时间:2017-06-06 18:30:31

标签: vb.net filesystems savefiledialog

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

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

                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

1 个答案:

答案 0 :(得分:0)

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