当前位置: 首页>>代码示例>>C#>>正文


C# DateTimeOffset.FromFileTime方法代码示例

本文整理汇总了C#中System.DateTimeOffset.FromFileTime方法的典型用法代码示例。如果您正苦于以下问题:C# DateTimeOffset.FromFileTime方法的具体用法?C# DateTimeOffset.FromFileTime怎么用?C# DateTimeOffset.FromFileTime使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在System.DateTimeOffset的用法示例。


在下文中一共展示了DateTimeOffset.FromFileTime方法的1个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C#代码示例。

示例1: long

//引入命名空间
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()); 
   }
}
开发者ID:.NET开发者,项目名称:System,代码行数:107,代码来源:DateTimeOffset.FromFileTime

输出:

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


注:本文中的System.DateTimeOffset.FromFileTime方法示例由纯净天空整理自Github/MSDocs等开源代码及文档管理平台,相关代码片段筛选自各路编程大神贡献的开源项目,源码版权归原作者所有,传播和使用请参考对应项目的License;未经允许,请勿转载。