Solved

File.GetCreationTime incorrectly returns 12/31/1600 date

Posted on 2011-03-02
9
2,840 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
Get 15 Days FREE Full-Featured Trial

Benefit from a mission critical IT monitoring with Monitis Premium or get it FREE for your entry level monitoring needs.
-Over 200,000 users
-More than 300,000 websites monitored
-Used in 197 countries
-Recommended by 98% of users

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

Get 15 Days FREE Full-Featured Trial

Benefit from a mission critical IT monitoring with Monitis Premium or get it FREE for your entry level monitoring needs.
-Over 200,000 users
-More than 300,000 websites monitored
-Used in 197 countries
-Recommended by 98% of users

Question has a verified solution.

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

Entity Framework is a powerful tool to help you interact with the DataBase but still doesn't help much when we have a Stored Procedure that returns more than one resultset. The solution takes some of out-of-the-box thinking; read on!
Real-time is more about the business, not the technology. In day-to-day life, to make real-time decisions like buying or investing, business needs the latest information(e.g. Gold Rate/Stock Rate). Unlike traditional days, you need not wait for a fe…
In this video, viewers are given an introduction to using the Windows 10 Snipping Tool, how to quickly locate it when it's needed and also how make it always available with a single click of a mouse button, by pinning it to the Desktop Task Bar. Int…
Michael from AdRem Software outlines event notifications and Automatic Corrective Actions in network monitoring. Automatic Corrective Actions are scripts, which can automatically run upon discovery of a certain undesirable condition in your network.…

622 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