0

for some reason it seems File.ReadAllLines has an uncanny issue converting all dates from yyyy/mm/dd to yy/mm/dd.

Problem is i am working with historic data that can pull back all the way past 1901, thus causing an issue with shorthand date notation.

public static string[] readFileToStringArray(string filePath = null, string fileName = null)
{
    string[] lines = null;
    if (File.Exists(filePath + fileName))
    {
        //lines = File.ReadAllLines(filePath + fileName);
        lines = File.ReadAllLines(filePath + fileName, Encoding.UTF8);
        string unescape = String.Empty;
        List<string> thisCSV = new List<string>();
        foreach (string line in lines)
        {
            unescape = line.Replace("\"", "");
            thisCSV.Add(unescape );
        }
        lines = thisCSV.ToArray();
    }
    return lines;
}

Weird thing is it does not do this to all files, I have several running this script. I then checked the physical file and everything is in order there.

It is a valid CSV,everything is encapsulated as text.

Source:

Result:

Any idea how to negate this, as remedying the whole dataset in memory/stream is out of the question? already checked MSDN but no help there.

The only other code involved at this point is a helper method, but irrelevant as the only thing it does is create a list of filenames.

public static List<string> GetFilesinDirectory(string filesPath)
{
    List<string> files = new List<string>();
    string physicalFilesPath = (string)HttpContext.Current.Server.MapPath(filesPath);
    System.IO.DirectoryInfo dir = new System.IO.DirectoryInfo(physicalFilesPath);
    foreach (var file in dir.GetFiles())
    {
        string thisfile = physicalFilesPath + "\\" + (string)file.Name;
        files.Add(thisfile);
    }
    return files;
}

Thanks

LokizFenrir
  • 340
  • 3
  • 9
  • readalllines does nothing special of its own then reading the lines, eyes closed, and giving you the data as is. Check http://referencesource.microsoft.com/#mscorlib/system/io/file.cs,8d10107b7a92c5c2. – user734028 Jul 15 '15 at 13:22
  • @user734028 thank you for reference, exactly my thoughts that is why this is so bizarre, _as you can see from my screenshots the date value gets mangled. The only other custom code running is a bulk file get method._ – LokizFenrir Jul 15 '15 at 14:23
  • exactly my point, it should not happen (ever) and hence my question, at the moment this is a very simple app, with a very simple csv (4937 lines is barely a scratch). – LokizFenrir Jul 16 '15 at 06:04

2 Answers2

0

ReadAllLines wouldn't ever format the date like described, and I'm unable to replicate the issue.

Remove the superfluous ToString() from

unescape = line.ToString().Replace("\"", "");

As it's a string already, and parameters here could format the line incorrectly.

Oh and make sure you're viewing the physical file in a text editor not excel for comparison, else it will look different.

Can you post some example dates that aren't working correctly?

Pabregez
  • 11
  • 2
  • Thanks. I removed the .ToString() got no idea why i had that there. Valid point, but I use notepad++ for most of these things, not excel. The physical file as said originally is correct "yyyy/mm/dd" however .ReadAllLines() reads it as "yy/mm/dd". – LokizFenrir Jul 15 '15 at 13:00
0

Not as much an answer, but the solution nonetheless for my issue it seems.

  1. Closed all applications
  2. Ran a ccleaner cleanup
  3. Reinstalled Visual Studio
  4. Reinstalled dotnet framework 4.5.2
  5. Registered the framework

And to be overly cautious rewrote my code in a vanilla fresh new project.

Magically everything works as it should now.


~i think i know now how it must feel for a doctor hitting a bug with antibiotics (you're not exactly sure what did the trick nor caused the problem but it's solved)~

LokizFenrir
  • 340
  • 3
  • 9