Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

File.GetCreationTime incorrectly returns 12/31/1600 date

Posted on 2011-03-02
9
Medium Priority
?
2,976 Views
Last Modified: 2012-05-11
I am using the system.io.File object to get File creation time for the image files located on another server but this method incorrectly returns the date as 12/31/1600 for all the files I am looping through. Please see the code below. Is there anything I am missing/doing wrong here:
string fullImportPath = ConfigurationManager.AppSettings["saveImportPath"];
                                                                string fullSavePath = ConfigurationManager.AppSettings["savePath"];
                                                                // C# - copy with overwrite
                                  if ((FileNameExt!="zip") && (FileNameExt!="eps"))
                                  {
                                      try
                                      {
                                          sfileDate = File.GetCreationTime(fullImportPath + FileNameCleaned).ToString();
                                          File.Copy(fileName, fullSavePath + FileNameCleaned, true);

                                          //Add an entry to the HWDetails table in the database


                                          int iAccID = int.Parse(dtvalid.Rows[0].ItemArray[0].ToString());
                                          string sFileOriginalName = FileNameCleaned;
                                          string sFilePath = fullSavePath + FileNameCleaned;

                                          RetrieveDataGateway rdg = new RetrieveDataGateway();

                                          //// Call the related stored proc to insert record into the database
                                          rdg.InsertHWDetailsPhotoInfo("InsertHWDetailsPhotoInfo", iAccID, sFileOriginalName,
                                              sFilePath, sfileDate);
                                      }
                                      catch (Exception ex)
                                      {
                                          string x = ex.Message;
                                          continue;
                                      }                                     
                                    }

Open in new window

0
Comment
Question by:skaleem1
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 3
  • 3
  • 3
9 Comments
 
LVL 7

Expert Comment

by:Gene_Cyp
ID: 35018131
Have a look at this mate:

http://www.csharp-examples.net/file-creation-modification-time/

It explains what you want to get. If you are still having problems, let me know.
0
 
LVL 23

Expert Comment

by:wdosanjos
ID: 35018474
Check if "fullImportPath + FileNameCleaned" returns the correct full file name. Here is what the File.GetCurrentTime documentation says:
If the file described in the path parameter does not exist, this method returns 12:00 midnight, January 1, 1601 A.D. (C.E.) Coordinated Universal Time (UTC), adjusted to local time.

http://msdn.microsoft.com/en-us/library/system.io.file.getcreationtime.aspx
0
 
LVL 1

Author Comment

by:skaleem1
ID: 35018489
I tried to change it to the following  but it the date is still returned as "12/31/1600 7:00:00 PM". Please see the code in the code section:
string fullImportPath = ConfigurationManager.AppSettings["saveImportPath"];
                                                               string fullSavePath = ConfigurationManager.AppSettings["savePath"];
                                                                // C# - copy with overwrite
                                  if ((FileNameExt!="zip") && (FileNameExt!="eps"))
                                  {
                                      try
                                      {
                                          //sfileDate = File.GetCreationTime(fullImportPath + FileNameCleaned).ToString();
                                          FileInfo fileInfo = new FileInfo(fullImportPath + FileNameCleaned);


                                          // local times
                                          sfileDate = fileInfo.CreationTime.ToString();

                                          
                                          File.Copy(fileName, fullSavePath + FileNameCleaned, true);

                                          //Add an entry to the HWDetails table in the database


                                          int iAccID = int.Parse(dtvalid.Rows[0].ItemArray[0].ToString());
                                          string sFileOriginalName = FileNameCleaned;
                                          string sFilePath = fullSavePath + FileNameCleaned;

                                          RetrieveDataGateway rdg = new RetrieveDataGateway();

                                          //// Call the related stored proc to insert record into the database
                                          rdg.InsertHWDetailsPhotoInfo("InsertHWDetailsPhotoInfo", iAccID, sFileOriginalName,
                                              sFilePath, sfileDate);
                                      }
                                      catch (Exception ex)
                                      {
                                          string x = ex.Message;
                                          continue;
                                      }                                     
                                    }

Open in new window

0
Microsoft Certification Exam 74-409

Veeam® is happy to provide the Microsoft community with a study guide prepared by MVP and MCT, Orin Thomas. This guide will take you through each of the exam objectives, helping you to prepare for and pass the examination.

 
LVL 23

Expert Comment

by:wdosanjos
ID: 35018671
I suspect it should be as follows.  It gets the Creation Date/Time of the source file (based on your File.Copy stmt).

sfileDate = File.GetCreationTime(fileName).ToString();


What's the value of the following variables?

fullImportPath
FileNameCleaned
fileName
fullSavePath



0
 
LVL 1

Author Comment

by:skaleem1
ID: 35020436

fullImportPath ="\\fdg82ABC\Accessories_Photography\ABC-13025-004 (1GB SD Card)[HF].jpg"
FileNameCleaned= "ABC-13025-004 (1GB SD Card)[HF].jpg"
fileName="\\fdg82ABC\Accessories_Photography\ABC-13025-004 (1GB SD Card)[HF].jpg"
fullSavePath=F:\Photos\ABC-13025-004 (1GB SD Card)[HF].jpg


0
 
LVL 23

Accepted Solution

by:
wdosanjos earned 2000 total points
ID: 35020584
Definitely << fullImportPath + FileNameCleaned >> is not correct, because it results in an incorrect file name.

\\fdg82ABC\Accessories_Photography\ABC-13025-004 (1GB SD Card)[HF].jpgABC-13025-004 (1GB SD Card)[HF].jpg


The correct statement is either:
    sfileDate = File.GetCreationTime(fullImportPath).ToString();
or
    sfileDate = File.GetCreationTime(fileName).ToString();


Since I don't know the details of your application I can't be sure which is the correct answer (it might be both).
0
 
LVL 7

Expert Comment

by:Gene_Cyp
ID: 35027619
Skaleem:

Check what the ACTUAL values of the files in question are. That is, go to those files yourself and check their properties. Do they have a valid Creation Date?

0
 
LVL 7

Expert Comment

by:Gene_Cyp
ID: 35027691
That 1600 date signifies that either the files don't have a valid creation date or something fails along the way.

By the way go for raw DateTime at first instead of string:

DateTime creationTime = File.GetCreationTime(@"c:\file.txt");

And create four different DateTime variables for each of your attributes:

fullImportPath ="\\fdg82ABC\Accessories_Photography\ABC-13025-004 (1GB SD Card)[HF].jpg"
FileNameCleaned= "ABC-13025-004 (1GB SD Card)[HF].jpg"
fileName="\\fdg82ABC\Accessories_Photography\ABC-13025-004 (1GB SD Card)[HF].jpg"
fullSavePath=F:\Photos\ABC-13025-004 (1GB SD Card)[HF].jpg


As follows:

DateTime creationTime1 = File.GetCreationTime(fullImportPath);
DateTime creationTime2 = File.GetCreationTime(FileNameCleaned); // this one will definitely fail ;)
DateTime creationTime3 = File.GetCreationTime(fileName);
DateTime creationTime4 = File.GetCreationTime(fullSavePath);

This will shed some light as I don't know where you are accessing it from. And post the value of each creationTime variable here please.
0
 
LVL 1

Author Closing Comment

by:skaleem1
ID: 35037355
Thanks a lot. This one worked:

sfileDate = File.GetCreationTime(fileName).ToString();
0

Featured Post

Free Backup Tool for VMware and Hyper-V

Restore full virtual machine or individual guest files from 19 common file systems directly from the backup file. Schedule VM backups with PowerShell scripts. Set desired time, lean back and let the script to notify you via email upon completion.  

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

In this article I will describe the Backup & Restore method as one possible migration process and I will add the extra tasks needed for an upgrade when and where is applied so it will cover all.
Calculating holidays and working days is a function that is often needed yet it is not one found within the Framework. This article presents one approach to building a working-day calculator for use in .NET.
Video by: ITPro.TV
In this episode Don builds upon the troubleshooting techniques by demonstrating how to properly monitor a vSphere deployment to detect problems before they occur. He begins the show using tools found within the vSphere suite as ends the show demonst…
In this video you will find out how to export Office 365 mailboxes using the built in eDiscovery tool. Bear in mind that although this method might be useful in some cases, using PST files as Office 365 backup is troublesome in a long run (more on t…

705 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question