I'm working on an application that will read in delimited files and display the first few rows on a datagridview, it will then use certain elements from these rows to do a SQL query (postcode lookup). I had it all working fine until we got some data where one of the column values contained a £, when it displays in the datagrid it looks like a diamond with a ? in it but even worse when it writes the file back out (with the SQL result added) it comes out as �
I thought I probably needed to specify the encoding but I was using the following which has no encoding setting that I can find:
Sub ParseFile(inputfile)
Dim dataFile = My.Computer.FileSystem.OpenTextFieldParser(inputFile)
Using dataFile
dataFile.TextFieldType = FileIO.FieldType.Delimited
If DelimiterComboBox.SelectedItem = ".csv" Then
dataFile.SetDelimiters(",")
ElseIf DelimiterComboBox.SelectedItem = ".txt" Then
dataFile.SetDelimiters(vbTab)
End If
Dim currentRow As String()
currentRow = dataFile.ReadFields()
I tried changing that to use a TextFieldReader which would allow the Encoding to be specified but I can't pass the filename in as a variable then as I get conversion errors. I then tried defining a variable as a stream of the filename but then I get: Unable to cast object of type 'System.String' to type 'System.IO.Stream'.
I'm thinking that I need to construct a stream from the filename somehow and feed that into the "dataFile" object so I can then control (or even detect) the encoding.
I should point out that this app needs to work with various delimited files (.csv, tab and pipe being the most common).
Try
Using dataFile As TextFieldParser = My.Computer.FileSystem.OpenTextFieldParser(New System.IO.StreamReader(inputFile, encoding))
...
End Using
See http://msdn.microsoft.com/en-us/library/system.text.encoding(v=vs.90).aspx for more information on things you can specify for the encoding
parameter.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.