Solved

File.GetCreationTime incorrectly returns 12/31/1600 date

Posted on 2011-03-02
9
2,476 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
  • 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
 
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
Complete VMware vSphere® ESX(i) & Hyper-V Backup

Capture your entire system, including the host, with patented disk imaging integrated with VMware VADP / Microsoft VSS and RCT. RTOs is as low as 15 seconds with Acronis Active Restore™. You can enjoy unlimited P2V/V2V migrations from any source (even from a different hypervisor)

 
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 500 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

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.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
LINQ - C# to VB convertion 12 48
Word Directory is not in the drop down list 4 25
In WPF / C# binding a local database in code behind 1 24
Replace Dates in query 14 18
by Mark Wills Attending one of Rob Farley's seminars the other day, I heard the phrase "The Accidental DBA" and fell in love with it. It got me thinking about the plight of the newcomer to SQL Server...  So if you are the accidental DBA, or, simp…
Introduction Hi all and welcome to my first article on Experts Exchange. A while ago, someone asked me if i could do some tutorials on object oriented programming. I decided to do them on C#. Now you may ask me, why's that? Well, one of the re…
This Micro Tutorial demonstrates using Microsoft Excel pivot tables, how to reverse engineer competitors' marketing strategies through backlinks.
Many functions in Excel can make decisions. The most simple of these is the IF function: it returns a value depending on whether a condition you describe is true or false. Once you get the hang of using the IF function, you will find it easier to us…

895 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

Need Help in Real-Time?

Connect with top rated Experts

12 Experts available now in Live!

Get 1:1 Help Now