John Hughes
John Hughes

Reputation: 377

StreamReader fails on Spanish Windows 8

I use the code below to import a CSV file, was using the VB reader but had the same problem. Basically on my system and others I have tested it works but on Windows 8 ESP (Spanish) it fails. I was able to work around the problem by opening the imported file in Visual Studio and saving it with encoding (US-ASCII - Codepage 20127) and Line endings CR/LF. My guess is it is the CR/LF setting but am not sure. I don't have Spanish Windows to test on so kind of awkward to determine the exact solution. I can't keep manually changing the input file.

Edit: I do know in the Spanish row.length always returns <= 5 so no data is imported. I don't know if it is reading the whole file as one line (CR not work) or it is not splitting the lines ";" not working.

Any idea why Spanish Windows is not reading the file correctly and or how to work around it?

var frm = new OpenFileDialog();
var culture = System.Globalization.CultureInfo.CreateSpecificCulture("en-US");
var styles = System.Globalization.DateTimeStyles.None;
var delimiters = new char[] { ';' };
var trims = new char[] { '"' };
if (frm.ShowDialog() ?? false)
{
using (System.IO.StreamReader reader = System.IO.File.OpenText(frm.FileName))
{
    string input;
    while((input = reader.ReadLine()) != null)
    {
        var rows = input.Split(delimiters);
        for(int i = 0; i < rows.Length; i++)
        {
            rows[i] = rows[i].TrimStart(trims).TrimEnd(trims);
        }
        DateTime expiration;
        if (rows.Length > 5 && DateTime.TryParse(rows[4], culture, styles, out expiration))
        {
            new Pin()
                {
                    ID = Guid.NewGuid(),
                    PinCode = rows[0],
                    Name = rows[1],
                    Area = rows[5],
                    DeviceId = "",
                    Expiration = expiration,
                    IsSold = false
                });
        }
    }
}
}

sample data:

"Code";"Profile name";"Profile type";"Duration";"Expiration date";"WiFi Area";
"99999";"10 peso 1 hour";"One-Time";"60  Mins";"2015-06-19 23:59:59";"company"

Upvotes: 0

Views: 80

Answers (1)

riahc3
riahc3

Reputation: 878

This sticks out like a eye sore:

var culture = System.Globalization.CultureInfo.CreateSpecificCulture("en-US");

Why do you do this? Why not get the CultureInfo of the current system the code is running on?

Upvotes: 1

Related Questions