SaveFileDialog 产生无法识别的字符
SaveFileDialog producing unrecognized character
我的程序应该创建一个文本文件,但它在文本文件的开头添加了不可见的无法识别的字符。扫描时我只能看到不可见的无法识别的字符。我能够通过使用写字板而不是记事本打开,键入字符,然后退格然后保存来让字符消失。这修复了文件,但我需要更正导致它的代码。我弄乱了文件类型并添加了一个随机字符吗?
这是对话框和文件的代码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
这里是构建文本的代码:
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) – Chase Rocker 18 mins ago
我的程序应该创建一个文本文件,但它在文本文件的开头添加了不可见的无法识别的字符。扫描时我只能看到不可见的无法识别的字符。我能够通过使用写字板而不是记事本打开,键入字符,然后退格然后保存来让字符消失。这修复了文件,但我需要更正导致它的代码。我弄乱了文件类型并添加了一个随机字符吗?
这是对话框和文件的代码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
这里是构建文本的代码:
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) – Chase Rocker 18 mins ago