📄 form1.cs
字号:
/// which contains two embedded SystemTime structures.</remarks>
/// <param name="bytes">Byte Array containing SystemTime data.</param>
/// <param name="offset">Offset (in bytes) to SystemTime data.</param>
public SystemTime( byte[] bytes, int offset )
{
// Dump the byte array into our array.
Buffer.BlockCopy( bytes, offset, flatStruct, 0, flatStruct.Length );
}
#endregion
#region ctor(ushort, ushort, ushort, ushort, ushort, ushort)
/// <summary>
/// Initializes a new SystemTime object with the specified parameters. /// Initializes a new SYSTEMTIME object with the specified parameters.
/// <param name="year">Specifies the current year.</param>
/// <param name="month">Specifies the current month; January = 1, February = 2, and so on</param>
/// <param name="day">Specifies the current day of the month.</param>
/// <param name="hour">Specifies the current hour.</param>
/// <param name="minute">Specifies the current minute.</param>
/// <param name="second">Specifies the current second.</param>
/// </summary>
public SystemTime(short year, short month, short day, short hour, short minute, short second)
{
Year = year;
Month = month;
DayOfWeek = 0;
Day = day;
Hour = hour;
Minute = minute;
Second = second;
Milliseconds = 0;
}
#endregion
#region ToByteArray
/// <summary>
/// Method to extract marshal-compatible 'structure' from the class.
/// </summary>
/// <returns>Byte Array containing the SystemTime data.</returns>
public byte[] ToByteArray()
{
return flatStruct;
}
#endregion
/// <summary>
/// Create a new SystemTime instance from an existing DateTime instance.
/// </summary>
/// <param name="dt">DateTime to create SystemTime from.</param>
public SystemTime(System.DateTime dt) : this((short)dt.Year, (short)dt.Month, (short)dt.Day, (short)dt.Hour, (short)dt.Minute, (short)dt.Second)
{
}
/// <summary>
/// Create a new empty SystemTime instance.
/// </summary>
public SystemTime() : this(new byte[16])
{
}
/// <summary>
/// Converts a SystemTime structure to a DateTime object.
/// </summary>
/// <param name="st">A SystemTime structure.</param>
/// <returns>Equivalent date in the form of a <see cref="DateTime"/></returns>
public static implicit operator System.DateTime(SystemTime st)
{
return st.ToDateTime();
}
/// <summary>
/// Converts a 64bit FileTime value to a SystemTime structure.
/// </summary>
/// <param name="FileTime">FileTime.</param>
/// <returns>A SystemTime structure.</returns>
public static implicit operator SystemTime(long FileTime)
{
byte[] bytes = new byte[16];
switch(System.Environment.OSVersion.Platform)
{
case PlatformID.WinCE:
FileTimeToSystemTimeCE(ref FileTime, bytes);
break;
default:
FileTimeToSystemTimePC(ref FileTime, bytes);
break;
}
SystemTime st = new SystemTime(bytes);
return st;
}
/// <summary>
/// Converts a SystemTime structure to the equivalent FileTime 64bit integer.
/// </summary>
/// <param name="st"></param>
/// <returns></returns>
public static implicit operator long(SystemTime st)
{
byte[] bytes = new byte[16];
bytes = st.ToByteArray();
long ft = new long();
switch(System.Environment.OSVersion.Platform)
{
case PlatformID.WinCE:
SystemTimeToFileTimeCE(bytes, ref ft);
break;
default:
SystemTimeToFileTimePC(bytes, ref ft);
break;
}
return ft;
}
#region From FileTime
/// <summary>
/// Returns a SystemTime equivalent to the specified operating system file timestamp.
/// </summary>
/// <param name="fileTime">A Windows file time.</param>
/// <returns>A SystemTime value representing the date and time of fileTime.</returns>
public static SystemTime FromFileTime(long fileTime)
{
SystemTime st = new SystemTime();
switch(System.Environment.OSVersion.Platform)
{
case PlatformID.WinCE:
FileTimeToSystemTimeCE(ref fileTime, st.flatStruct);
break;
default:
FileTimeToSystemTimePC(ref fileTime, st.flatStruct);
break;
}
return st;
}
#endregion
#region ToFileTime
/// <summary>
/// Converts the value of this instance to the format of a local operating system file time.
/// </summary>
/// <returns>The value of this SystemTime in the format of a local operating system file time.</returns>
public long ToFileTime()
{
long ft = new long();
switch(System.Environment.OSVersion.Platform)
{
case PlatformID.WinCE:
SystemTimeToFileTimeCE(this.flatStruct, ref ft);
break;
default:
SystemTimeToFileTimePC(this.flatStruct, ref ft);
break;
}
return ft;
}
#endregion
#region PC P/Invokes
[DllImport("kernel32.dll", EntryPoint="FileTimeToSystemTime", SetLastError=true)]
private static extern int FileTimeToSystemTimePC(
ref long lpFileTime,
byte[] lpSystemTime );
[DllImport("kernel32.dll", EntryPoint="SystemTimeToFileTime", SetLastError=true)]
private static extern int SystemTimeToFileTimePC(
byte[] lpSystemTime,
ref long lpFileTime);
#endregion
#region CE Invokes
[DllImport("coredll.dll", EntryPoint="FileTimeToSystemTime", SetLastError=true)]
private static extern int FileTimeToSystemTimeCE(
ref long lpFileTime,
byte[] lpSystemTime );
[DllImport("coredll.dll", EntryPoint="SystemTimeToFileTime", SetLastError=true)]
private static extern int SystemTimeToFileTimeCE(
byte[] lpSystemTime,
ref long lpFileTime);
#endregion
/// <summary>
/// Converts a SystemTime structure to the equivalent binary data.
/// </summary>
/// <param name="st"></param>
/// <returns></returns>
public static implicit operator byte[]( SystemTime st )
{
return st.ToByteArray();
}
#region From DateTime
/// <summary>
/// Creates a new instance of SystemTime from an existing System.DateTime object
/// </summary>
/// <param name="dt">DateTime object to copy.</param>
/// <returns>SystemTime class matching the DateTime object.</returns>
public static SystemTime FromDateTime(System.DateTime dt)
{
return new SystemTime(Convert.ToInt16(dt.Year), Convert.ToInt16(dt.Month), Convert.ToInt16(dt.Day), Convert.ToInt16(dt.Hour), Convert.ToInt16(dt.Minute), Convert.ToInt16(dt.Second));
}
#endregion
#region To DateTime
/// <summary>
/// Returns a <see cref="T:System.DateTime"/> object with the same Date and time as this instance.
/// </summary>
/// <returns>A <see cref="T:System.DateTime"/> copy of the SystemTime object.</returns>
public System.DateTime ToDateTime()
{
try
{
return new System.DateTime(Year, Month, Day, Hour, Minute, Second, Milliseconds);
}
catch
{
//catch invalid date
return DateTime.MinValue;
}
}
#endregion
#region Year
/// <summary>
/// Gets the year component of the date represented by this instance.
/// </summary>
public short Year
{
get
{
return BitConverter.ToInt16( flatStruct, wYearOffset );
}
set
{
byte[] bytes = BitConverter.GetBytes( value );
Buffer.BlockCopy( bytes, 0, flatStruct, wYearOffset, 2 );
}
}
#endregion
#region Month
/// <summary>
/// Gets the month component of the date represented by this instance.
/// </summary>
public short Month
{
get
{
return BitConverter.ToInt16( flatStruct, wMonthOffset );
}
set
{
byte[] bytes = BitConverter.GetBytes( value );
Buffer.BlockCopy( bytes, 0, flatStruct, wMonthOffset, 2 );
}
}
#endregion
#region DayOfWeek
/// <summary>
/// The Day of the week. Sunday = 0, Monday = 1, and so on.
/// </summary>
/// <remarks>Because the numbering scheme matches the System.DayOfWeek enumeration,
/// it is possible to cast this field to DayOfWeek.</remarks>
public short DayOfWeek
{
get
{
return BitConverter.ToInt16( flatStruct, wDayOfWeekOffset );
}
set
{
byte[] bytes = BitConverter.GetBytes( value );
Buffer.BlockCopy( bytes, 0, flatStruct, wDayOfWeekOffset, 2 );
}
}
#endregion
#region Day
/// <summary>
/// Gets the day of the month represented by this instance.
/// </summary>
public short Day
{
get
{
return BitConverter.ToInt16( flatStruct, wDayOffset );
}
set
{
byte[] bytes = BitConverter.GetBytes( value );
Buffer.BlockCopy( bytes, 0, flatStruct, wDayOffset, 2 );
}
}
#endregion
#region Hour
/// <summary>
/// Gets the hour component of the date represented by this instance.
/// </summary>
public short Hour
{
get
{
return BitConverter.ToInt16( flatStruct, wHourOffset );
}
set
{
byte[] bytes = BitConverter.GetBytes( value );
Buffer.BlockCopy( bytes, 0, flatStruct, wHourOffset, 2 );
}
}
#endregion
#region Minute
/// <summary>
/// Gets the minute component of the date represented by this instance.
/// </summary>
public short Minute
{
get
{
return BitConverter.ToInt16( flatStruct, wMinuteOffset );
}
set
{
byte[] bytes = BitConverter.GetBytes( value );
Buffer.BlockCopy( bytes, 0, flatStruct, wMinuteOffset, 2 );
}
}
#endregion
#region Second
/// <summary>
/// Gets the seconds component of the date represented by this instance.
/// </summary>
public short Second
{
get
{
return BitConverter.ToInt16( flatStruct, wSecondOffset );
}
set
{
byte[] bytes = BitConverter.GetBytes( value );
Buffer.BlockCopy( bytes, 0, flatStruct, wSecondOffset, 2 );
}
}
#endregion
#region Milliseconds
/// <summary>
/// Gets the milliseconds component of the date represented by this instance.
/// </summary>
public short Milliseconds
{
get
{
return BitConverter.ToInt16( flatStruct, wMillisecondsOffset );
}
set
{
byte[] bytes = BitConverter.GetBytes( value );
Buffer.BlockCopy( bytes, 0, flatStruct, wMillisecondsOffset, 2 );
}
}
#endregion
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -