Solved

File.GetCreationTime incorrectly returns 12/31/1600 date

Posted on 2011-03-02
9
2,388 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
Why You Should Analyze Threat Actor TTPs

After years of analyzing threat actor behavior, it’s become clear that at any given time there are specific tactics, techniques, and procedures (TTPs) that are particularly prevalent. By analyzing and understanding these TTPs, you can dramatically enhance your security program.

 
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

What Should I Do With This Threat Intelligence?

Are you wondering if you actually need threat intelligence? The answer is yes. We explain the basics for creating useful threat intelligence.

Join & Write a Comment

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…
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…
Get a first impression of how PRTG looks and learn how it works.   This video is a short introduction to PRTG, as an initial overview or as a quick start for new PRTG users.
Here's a very brief overview of the methods PRTG Network Monitor (https://www.paessler.com/prtg) offers for monitoring bandwidth, to help you decide which methods you´d like to investigate in more detail.  The methods are covered in more detail in o…

747 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

11 Experts available now in Live!

Get 1:1 Help Now