Go Premium for a chance to win a PS4. Enter to Win

x
?
Solved

File.GetCreationTime incorrectly returns 12/31/1600 date

Posted on 2011-03-02
9
Medium Priority
?
3,119 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
What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

 
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 Tool: SSL Checker

Scans your site and returns information about your SSL implementation and certificate. Helpful for debugging and validating your SSL configuration.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

Exception Handling is in the core of any application that is able to dignify its name. In this article, I'll guide you through the process of writing a DRY (Don't Repeat Yourself) Exception Handling mechanism, using Aspect Oriented Programming.
This article aims to explain the working of CircularLogArchiver. This tool was designed to solve the buildup of log file in cases where systems do not support circular logging or where circular logging is not enabled
This Micro Tutorial will teach you how to add a cinematic look to any film or video out there. There are very few simple steps that you will follow to do so. This will be demonstrated using Adobe Premiere Pro CS6.
Screencast - Getting to Know the Pipeline

916 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