简体   繁体   中英

C# filehelpers CSV DateTime exception on different system

So I got my program to do what I want, but it doesn't work on my work PC (Win 7).

  • When using compiled version from my home PC (Win 10) on work PC I'm getting error "sequence has no elements" with all the methods of my program when trying to read the file with filehelpers engine.
  • When I use the same sln from home that is compiled from work pc on my home pc, it works.
  • When I compile the same sln the program on my work PC I'm getting different error:
FileHelpers.ConvertException: „Error Converting '03/19/2019 3:40 PM' to type: 'DateTime'.  Using the format: 'MM/dd/yyyy h:mm tt'”

which is super strange, because the program works on my home pc.

the data template is:

[DelimitedRecord(","), IgnoreFirst(1)]
public class CSVDataFields
{
    [FieldQuoted('"')] [FieldConverter(ConverterKind.Date, "MM/dd/yyyy h:mm tt")]
    public DateTime Date;

    [FieldQuoted('"')]
    public float Value;
}

and the data that the program reads is:

"Date","Value"
"03/19/2019 3:40 PM","23.1"

I "rewrote" (create new project and copy paste methods one by one) the program on my work pc, but that didn't change anything.

I have no clue what could cause this problem, only differences are system language and operating system.

The DateTimeConvertor class which is found in the Converters/ConvertHelpers.cs has several constructors that can be initiated with just a format or with a culture as well. If you do include a culture, then the culture is obtained via

if (culture != null)
    mCulture = CultureInfo.GetCultureInfo(culture);

When attempting the conversion in the StringToField() method, the DateTime.TryParseExact() function is used.

DateTime.TryParseExact(from.Trim(), mFormat, mCulture, DateTimeStyles.None, out val)

If the mCulture variable was not set above, then the current culture is utilised as @niisK said earlier.

The Microsoft documentation on this at https://docs.microsoft.com/en-us/dotnet/api/system.datetime.tryparseexact?view=netframework-4.8 state:

The particular date and time symbols and strings (such as the names of the days of the week in a particular language) used in s are defined by the provider parameter, as is the precise format of s if format is a standard format specifier string. The provider parameter can be any of the following:

  • A CultureInfo object that represents the culture used to interpret s. The DateTimeFormatInfo object returned by its DateTimeFormat property defines the symbols and formatting in s.

  • A DateTimeFormatInfo object that defines the format of date and time data.

  • A custom IFormatProvider implementation whose GetFormat method returns either the CultureInfo object or the DateTimeFormatInfo object that provides formatting information.

If provider is null, the CultureInfo object that corresponds to the current culture is used.

So, the errors that have you been experiencing may have been due to a conflict between the format you were trying to use and the culture that is being used by default. Setting the culture as you did will likely 'fix' the problem but you should check what culture is being used next time as it is likely wrong in the control panel/system settings.

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.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM