DateTimeOffset.FromFileTime(Int64) DateTimeOffset.FromFileTime(Int64) DateTimeOffset.FromFileTime(Int64) DateTimeOffset.FromFileTime(Int64) Method

定義

指定された Windows ファイル時刻を同等の現地時刻に変換します。Converts the specified Windows file time to an equivalent local time.

public:
 static DateTimeOffset FromFileTime(long fileTime);
public static DateTimeOffset FromFileTime (long fileTime);
static member FromFileTime : int64 -> DateTimeOffset
Public Shared Function FromFileTime (fileTime As Long) As DateTimeOffset

パラメーター

fileTime
Int64 Int64 Int64 Int64

タイマー刻み単位で表した Windows ファイルの時刻。A Windows file time, expressed in ticks.

戻り値

fileTime の日時を表すオブジェクト。オフセットは現地時刻のオフセットに設定されます。An object that represents the date and time of fileTime with the offset set to the local time offset.

例外

filetime が 0 未満です。filetime is less than zero.

または-or- filetimeDateTimeOffset.MaxValue.Ticks より大きくなっています。filetime is greater than DateTimeOffset.MaxValue.Ticks.

次の例では、Windows API を使用して、ワードパッド実行可能ファイルの Windows ファイルの時刻を取得します。The following example uses the Windows API to retrieve the Windows file times for the WordPad executable.

using System;
using System.IO;
using System.Runtime.InteropServices;

public struct FileTime
{
   public uint dwLowDateTime;
   public uint dwHighDateTime;

   public static implicit operator long(FileTime fileTime)
   {
      long returnedLong;
      // Convert 4 high-order bytes to a byte array
      byte[] highBytes = BitConverter.GetBytes(fileTime.dwHighDateTime);
      // Resize the array to 8 bytes (for a Long)
      Array.Resize(ref highBytes, 8); 

      // Assign high-order bytes to first 4 bytes of Long
      returnedLong = BitConverter.ToInt64(highBytes, 0); 
      // Shift high-order bytes into position
      returnedLong = returnedLong << 32;
      // Or with low-order bytes
      returnedLong = returnedLong | fileTime.dwLowDateTime;
      // Return long 
      return returnedLong;
   }
}

public class FileTimes
{
   private const int OPEN_EXISTING = 3;
   private const int INVALID_HANDLE_VALUE = -1;
      
   [DllImport("Kernel32.dll", CharSet = CharSet.Unicode)]
   private static extern int CreateFile(string lpFileName, 
                                       int dwDesiredAccess, 
                                       int dwShareMode, 
                                       int lpSecurityAttributes, 
                                       int dwCreationDisposition, 
                                       int dwFlagsAndAttributes, 
                                       int hTemplateFile);

   [DllImport("Kernel32.dll")]
   private static extern bool GetFileTime(int hFile, 
                                          out FileTime lpCreationTime, 
                                          out FileTime lpLastAccessTime, 
                                          out FileTime lpLastWriteTime);

   [DllImport("Kernel32.dll")]
   private static extern bool CloseHandle(int hFile); 

   public static void Main()
   {
      // Open file %windir%\write.exe
      string winDir = Environment.SystemDirectory; 
      if (! (winDir.EndsWith(Path.DirectorySeparatorChar.ToString())))
         winDir += Path.DirectorySeparatorChar; 
      winDir += "write.exe";

      // Get file time using Windows API
      //
      // Open file
      int hFile = CreateFile(winDir, 0, 0, 0, OPEN_EXISTING, 0, 0);
      if (hFile == INVALID_HANDLE_VALUE)
      {
         Console.WriteLine("Unable to access {0}.", winDir);
      }   
      else
      {
         FileTime creationTime, accessTime, writeTime;
         if (GetFileTime(hFile, out creationTime, out accessTime, out writeTime)) 
         {
            CloseHandle(hFile);
            long fileCreationTime = (long) creationTime;
            long fileAccessTime = accessTime;
            long fileWriteTime = (long) writeTime;

            Console.WriteLine("File {0} Retrieved Using the Windows API:", winDir);
            Console.WriteLine("   Created:     {0:d}", DateTimeOffset.FromFileTime(fileCreationTime).ToString());
            Console.WriteLine("   Last Access: {0:d}", DateTimeOffset.FromFileTime(fileAccessTime).ToString());
            Console.WriteLine("   Last Write:  {0:d}", DateTimeOffset.FromFileTime(fileWriteTime).ToString());
            Console.WriteLine();
         }   
      }
      
      // Get date and time, convert to file time, then convert back
      FileInfo fileInfo = new FileInfo(winDir);
      DateTimeOffset infoCreationTime, infoAccessTime, infoWriteTime;
      long ftCreationTime, ftAccessTime, ftWriteTime;
      
      // Get dates and times of file creation, last access, and last write
      infoCreationTime = fileInfo.CreationTime;
      infoAccessTime = fileInfo.LastAccessTime;
      infoWriteTime = fileInfo.LastWriteTime;
      // Convert values to file times
      ftCreationTime = infoCreationTime.ToFileTime();
      ftAccessTime = infoAccessTime.ToFileTime();
      ftWriteTime = infoWriteTime.ToFileTime();
      
      // Convert file times back to DateTimeOffset values
      Console.WriteLine("File {0} Retrieved Using a FileInfo Object:", winDir);
      Console.WriteLine("   Created:     {0:d}", DateTimeOffset.FromFileTime(ftCreationTime).ToString());
      Console.WriteLine("   Last Access: {0:d}", DateTimeOffset.FromFileTime(ftAccessTime).ToString());
      Console.WriteLine("   Last Write:  {0:d}", DateTimeOffset.FromFileTime(ftWriteTime).ToString()); 
   }
}
// The example produces the following output:
//    File C:\WINDOWS\system32\write.exe Retrieved Using the Windows API:
//       Created:     10/13/2005 5:26:59 PM -07:00
//       Last Access: 3/20/2007 2:07:00 AM -07:00
//       Last Write:  8/4/2004 5:00:00 AM -07:00
//    
//    File C:\WINDOWS\system32\write.exe Retrieved Using a FileInfo Object:
//       Created:     10/13/2005 5:26:59 PM -07:00
//       Last Access: 3/20/2007 2:07:00 AM -07:00
//       Last Write:  8/4/2004 5:00:00 AM -07:00
Imports System.IO

Public Structure FileTime
   Public dwLowDateTime As UInteger
   Public dwHighDateTime As UInteger

   Public Shared Widening Operator CType(fileTime As FileTime) As Long
      Dim returnedLong As Long
      ' Convert 4 high-order bytes to a byte array
      Dim highBytes() As Byte = BitConverter.GetBytes(fileTime.dwHighDateTime)
      ' Resize the array to 8 bytes (for a Long)
      ReDim Preserve highBytes(7)

      ' Assign high-order bytes to first 4 bytes of Long
      returnedLong = BitConverter.ToInt64(highBytes, 0) 
      ' Shift high-order bytes into position
      returnedLong = returnedLong << 32
      ' Or with low-order bytes
      returnedLong = returnedLong Or fileTime.dwLowDateTime
      ' Return Long 
      return returnedLong
   End Operator
End Structure

Module modMain
   Private Const OPEN_EXISTING As Integer = 3
   
   Private Const INVALID_HANDLE_VALUE As Integer = -1
      
   Private Declare Function CreateFile Lib "kernel32" Alias "CreateFileA" ( _
                            ByVal lpFileName As String, _
                            ByVal dwDesiredAccess As Integer, _
                            ByVal dwShareMode As Integer, _
                            ByVal lpSecurityAttributes As Integer, _
                            ByVal dwCreationDisposition As Integer, _
                            ByVal dwFlagsAndAttributes As Integer, _
                            ByVal hTemplateFile As Integer) _
           As Integer

   Private Declare Function GetFileTime Lib "Kernel32"( _
                            hFile As Integer, _
                            ByRef lpCreationTime As FileTime, _
                            ByRef lpLastAccessTime As FileTime, _
                            ByRef lpLastWriteTime As FileTime) _
            As Boolean 

   Private Declare Function CloseHandle Lib "Kernel32" ( _
                            hFile As Integer) _
           As Boolean
                        
   Public Sub Main()
      ' Open file %windir%\write.exe
      Dim winDir As String = Environment.SystemDirectory 
      If Not winDir.EndsWith(Path.DirectorySeparatorChar) Then _
         winDir += Path.DirectorySeparatorChar 
      windir += "write.exe"

      ' Get file time using Windows API
      '
      ' Open file
      Dim hFile As Integer = CreateFile(winDir, 0, 0, 0, _
                                       OPEN_EXISTING, 0, 0)
      If hFile = INVALID_HANDLE_VALUE Then
         Console.WriteLine("Unable to access {0}.", winDir)
      Else
         Dim creationTime, accessTime, writeTime As FileTime
         If GetFileTime(hFile, creationTime, accessTime, writeTime) Then
            CloseHandle(hFile)
            Dim fileCreationTime As Long = CType(creationTime, Long)
            Dim fileAccessTime As Long = CType(accessTime, Long)
            Dim fileWriteTime As Long = CType(writeTime, Long)

            Console.WriteLine("File {0} Retrieved Using the Windows API:", winDir)
            Console.WriteLine("   Created:     {0:d}", DateTimeOffset.FromFileTime(fileCreationTime).ToString())
            Console.WriteLine("   Last Access: {0:d}", DateTimeOffset.FromFileTime(fileAccessTime).ToString())
            Console.WriteLine("   Last Write:  {0:d}", DateTimeOffset.FromFileTime(fileWriteTime).ToString())
            Console.WriteLine() 
         End If   
      End If
      
      ' Get date and time, convert to file time, then convert back
      Dim fileInfo As New FileInfo(winDir)
      Dim infoCreationTime, infoAccessTime, infoWriteTime As DateTimeOffset
      Dim ftCreationTime, ftAccessTime, ftWriteTime As Long
      
      ' Get dates and times of file creation, last access, and last write
      infoCreationTime = fileInfo.CreationTime
      infoAccessTime = fileInfo.LastAccessTime
      infoWriteTime = fileInfo.LastWriteTime
      ' Convert values to file times
      ftCreationTime = infoCreationTime.ToFileTime()
      ftAccessTime = infoAccessTime.ToFileTime()
      ftWriteTime = infoWriteTime.ToFileTime()
      
      ' Convert file times back to DateTimeOffset values
      Console.WriteLine("File {0} Retrieved Using a FileInfo Object:", winDir)
      Console.WriteLine("   Created:     {0:d}", DateTimeOffset.FromFileTime(ftCreationTime).ToString())
      Console.WriteLine("   Last Access: {0:d}", DateTimeOffset.FromFileTime(ftAccessTime).ToString())
      Console.WriteLine("   Last Write:  {0:d}", DateTimeOffset.FromFileTime(ftWriteTime).ToString()) 
   End Sub
End Module
' The example produces the following output:
'    File C:\WINDOWS\system32\write.exe Retrieved Using the Windows API:
'       Created:     10/13/2005 5:26:59 PM -07:00
'       Last Access: 3/20/2007 2:07:00 AM -07:00
'       Last Write:  8/4/2004 5:00:00 AM -07:00
'    
'    File C:\WINDOWS\system32\write.exe Retrieved Using a FileInfo Object:
'       Created:     10/13/2005 5:26:59 PM -07:00
'       Last Access: 3/20/2007 2:07:00 AM -07:00
'       Last Write:  8/4/2004 5:00:00 AM -07:00

注釈

Windows ファイル時刻は、64年1月 1601 1 日の午前0時から12:00 に経過した100ナノ秒間隔の数を表すビット値です。A Windows file time is a 64-bit value that represents the number of 100-nanosecond intervals that have elapsed since 12:00 midnight, January 1, 1601 A.D. 西暦協定世界時 (UTC)。(C.E.) Coordinated Universal Time (UTC). Windows では、ファイルの作成、アクセス、またはファイルへの書き込みを行うときに、ファイルの時刻が記録されます。Windows uses a file time to record when an application creates, accesses, or writes to a file.

Windows ファイルの時刻には、 GetFileTime FILETIME構造体を返す関数を呼び出すことによって、windows API から直接アクセスできます。A Windows file time is directly accessible through the Windows API by calling the GetFileTime function, which returns a FILETIME structure. 1つの関数パラメーターは、ファイルの時刻情報を取得するファイルのハンドルです。The single function parameter is the handle of the file whose file time information is to be retrieved. ファイルハンドルは、 CreateFile関数を呼び出すことによって取得されます。The file handle is retrieved by calling the CreateFile function. 構造FILETIME体のdwHighDateTimeメンバーには、ファイル時間の4つの上位バイトが含まれdwLowDateTime 、そのメンバーには4つの下位バイトが含まれます。The FILETIME structure's dwHighDateTime member contains the four high-order bytes of the file time, and its dwLowDateTime member contains the four low-order bytes. 次の例は、Windows ファイルの時刻値を取得して値にDateTimeOffset変換する方法を示しています。The example that follows illustrates how to retrieve Windows file time values and convert them to DateTimeOffset values.

Windows ファイル時刻値は、 DateTime DateTime.ToFileTimeUtc DateTimeOffset DateTime.ToFileTime メソッドDateTimeOffset.ToFileTimeとメソッドを呼び出すことによって値から作成することも、メソッドを呼び出すことによって値から作成することもできます。Windows file time values can also be created from DateTime values by calling the DateTime.ToFileTime and DateTime.ToFileTimeUtc methods, and from DateTimeOffset values by calling the DateTimeOffset.ToFileTime method.

適用対象