1

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).

4

1 回答 1

0

尝试

Using dataFile As TextFieldParser = My.Computer.FileSystem.OpenTextFieldParser(New System.IO.StreamReader(inputFile, encoding))
...
End Using

有关可以为参数指定的内容的更多信息,请参阅http://msdn.microsoft.com/en-us/library/system.text.encoding(v=vs.90).aspx 。encoding

于 2013-01-22T18:06:59.633 回答