SaveFileDialog producing unrecognized character

徘徊边缘 提交于 2019-12-26 03:33:05

问题


My program is supposed to create a text file, but it is adding invisible unrecognized char(s) to the beginning of the text file. I can only see the invisible unrecognized char(s) when scanned. I was able to get the char(s) to go away by opening with word pad instead of notepad, typing a char, and backspacing then saving. That fixed the file, but I need to correct the code causing it. Am I messing up the file type and its adding a random char?

Here is code for dialog and file name/type:

                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

Here is code for building the text:

     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:


Thank you @Chase Rocker ... Here is his answer: I'm guessing it's a Byte Order Mark (BOM). The WriteAllText method adds a BOM if no encoding is specified. After your True param, try adding this encoding param: New System.Text.UTF8Encoding(False) – Chase Rocker 18 mins ago



来源:https://stackoverflow.com/questions/44397355/savefiledialog-producing-unrecognized-character

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!