الحصول على التاريخ-الوقت الماضي ويندوز إيقاف تشغيل الحدث باستخدام .صافي

StackOverflow https://stackoverflow.com/questions/1631933

  •  06-07-2019
  •  | 
  •  

سؤال

هل هناك طريقة لمعرفة متى كان النظام الماضي الإغلاق ؟

أنا أعلم أن هناك طريقة لمعرفة آخر وقت التحميل باستخدام LastBootUpTime الملكية في Win32_OperatingSystem باستخدام مساحة الاسم WMI.

هل هناك أي شيء مماثل لمعرفة إيقاف تشغيل آخر الوقت ؟

شكرا

هل كانت مفيدة؟

المحلول

(كل شيء هنا هو 100 ٪ من باب المجاملة JDunkerley في وقت سابق الإجابة)

الحل هو أعلاه ، ولكن النهج من byte مجموعة DateTime لا يمكن أن يتحقق مع عدد أقل من البيانات باستخدام BitConverter.التالية ستة أسطر من التعليمات البرمجية تفعل نفس وإعطاء الصحيح DateTime من التسجيل:

public static DateTime GetLastSystemShutdown()
{
    string sKey = @"System\CurrentControlSet\Control\Windows";
    Microsoft.Win32.RegistryKey key = Microsoft.Win32.Registry.LocalMachine.OpenSubKey(sKey);

    string sValueName = "ShutdownTime";
    byte[] val = (byte[]) key.GetValue(sValueName);
    long valueAsLong = BitConverter.ToInt64(val, 0);
    return DateTime.FromFileTime(valueAsLong);
}

نصائح أخرى

على افتراض ويندوز إيقاف تشغيل بسلاسة.لأنه يخزن في التسجيل:

HKEY_LOCAL_MACHINE\SYSTEM\CurrentControlSet\Control\Windows\ShutdownTime

ويتم تخزينها إلى صفيف من البايت ولكن هو FILETIME.

في حين قد يكون هناك طريقة أفضل, لقد استخدمت هذا من قبل و أعتقد أنه يعمل:

    public static DateTime GetLastSystemShutdown()
    {
        string sKey = @"System\CurrentControlSet\Control\Windows";
        Microsoft.Win32.RegistryKey key = Microsoft.Win32.Registry.LocalMachine.OpenSubKey(sKey);

        string sValueName = "ShutdownTime";
        object val = key.GetValue(sValueName);

        DateTime output = DateTime.MinValue;
        if (val is byte[] && ((byte[])val).Length == 8)
        {
            byte[] bytes = (byte[])val;

            System.Runtime.InteropServices.ComTypes.FILETIME ft = new System.Runtime.InteropServices.ComTypes.FILETIME();
            int valLow = bytes[0] + 256 * (bytes[1] + 256 * (bytes[2] + 256 * bytes[3]));
            int valTwo = bytes[4] + 256 * (bytes[5] + 256 * (bytes[6] + 256 * bytes[7]));
            ft.dwLowDateTime = valLow;
            ft.dwHighDateTime = valTwo;

            DateTime UTC = DateTime.FromFileTimeUtc((((long) ft.dwHighDateTime) << 32) + ft.dwLowDateTime);
            TimeZoneInfo lcl = TimeZoneInfo.Local;
            TimeZoneInfo utc = TimeZoneInfo.Utc;
            output = TimeZoneInfo.ConvertTime(UTC, utc, lcl);
        }
        return output;
    }

آخر تشغيل الوقت يمكن العثور عليها باستخدام قطعة من التعليمات البرمجية

static void Main(string[] args)
    {          
        TimeSpan t = TimeSpan.FromMilliseconds(System.Environment.TickCount);
        Console.WriteLine( DateTime.Now.Subtract(t));          
    }
مرخصة بموجب: CC-BY-SA مع الإسناد
لا تنتمي إلى StackOverflow
scroll top