欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

如何使用C#修改本地Windows系統(tǒng)時間

 更新時間:2021年01月25日 08:56:16   作者:MarkYUN  
這篇文章主要介紹了如何使用C#修改本地Windows系統(tǒng)時間,幫助大家更好的理解和使用c#,感興趣的朋友可以了解下

C#提升管理員權(quán)限修改本地Windows系統(tǒng)時間

​在桌面應(yīng)用程序開發(fā)過程中,需要對C盤下進行文件操作或者系統(tǒng)參數(shù)進行設(shè)置,例如在沒有外網(wǎng)的情況下局域網(wǎng)內(nèi)部自己的機制進行時間同步校準,這是沒有管理員權(quán)限便無法進行設(shè)置。

1. 首先需要獲得校準時間,兩種方式:

通過可上網(wǎng)的電腦進行外部獲取當前時間。

通過NTP實現(xiàn)

 //NTP消息大小摘要是16字節(jié) (RFC 2030)
 byte[] ntpData = new byte[48];
 //設(shè)置跳躍指示器、版本號和模式值
 // LI = 0 (no warning), VN = 3 (IPv4 only), Mode = 3 (Client Mode)
 ntpData[0] = 0x1B; 
 IPAddress ip = iPAddress;
 // NTP服務(wù)給UDP分配的端口號是123
 IPEndPoint ipEndPoint = new IPEndPoint(ip, 123);
 // 使用UTP進行通訊
 Socket socket = new Socket(AddressFamily.InterNetwork, SocketType.Dgram, ProtocolType.Udp);
 socket.Connect(ipEndPoint);
 socket.ReceiveTimeout = 3000;
 socket.Send(ntpData);
 socket.Receive(ntpData);
 socket?.Close();
 socket?.Dispose();

程序手動輸入。

2. 轉(zhuǎn)換為本地時間

 //傳輸時間戳字段偏移量,以64位時間戳格式,應(yīng)答離開客戶端服務(wù)器的時間
 const byte serverReplyTime = 40;
 // 獲得秒的部分
 ulong intPart = BitConverter.ToUInt32(ntpData, serverReplyTime);
 //獲取秒的部分
 ulong fractPart = BitConverter.ToUInt32(ntpData, serverReplyTime + 4);
 //由big-endian 到 little-endian的轉(zhuǎn)換
 intPart = swapEndian(intPart);
 fractPart = swapEndian(fractPart);
 ulong milliseconds = (intPart * 1000) + ((fractPart * 1000) / 0x100000000UL);
 // UTC時間
 DateTime webTime = (new DateTime(1900, 1, 1, 0, 0, 0, DateTimeKind.Utc)).AddMilliseconds(milliseconds);
 //本地時間
 DateTime dt = webTime.ToLocalTime();

3. 獲取當前是否是管理員

public static bool IsAdministrator()
    {
      WindowsIdentity identity = WindowsIdentity.GetCurrent();
      WindowsPrincipal principal = new WindowsPrincipal(identity);
      return principal.IsInRole(WindowsBuiltInRole.Administrator);
    }

4. 引入dll

[DllImport("kernel32.dll")]
private static extern bool SetLocalTime(ref Systemtime time);

//轉(zhuǎn)化后的時間進行本地設(shè)置,并返回成功與否
bool isSuccess = SetLocalDateTime(dt);

5. 提升權(quán)限

如果程序不是管理員身份運行則不可以設(shè)置時間

引入引用程序清單文件(app.manifest),步驟:添加新建項->選擇‘應(yīng)用程序清單文件(僅限windows)'

引入后再文件中出現(xiàn)app.manifest文件

Value Description Comment
asInvoker The application runs with the same access token as the parent process. Recommended for standard user applications. Do refractoring with internal elevation points, as per the guidance provided earlier in this document.
highestAvailable The application runs with the highest privileges the current user can obtain. Recommended for mixed-mode applications. Plan to refractor the application in a future release.
requireAdministrator The application runs only for administrators and requires that the application be launched with the full access token of an administrator. Recommended for administrator only applications. Internal elevation points

默認權(quán)限:

 <requestedExecutionLevel level="asInvoker " uiAccess="false" />

asInvoker 表示當前用戶本應(yīng)該具有的權(quán)限

highestAvailable 表示提升當前用戶最高權(quán)限

requireAdministrator 表示提升為管理員權(quán)限

修改權(quán)限:

 <requestedExecutionLevel level="requireAdministrator" uiAccess="false" />

6. 重新生成程序

源碼

using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Net.Sockets;
using System.Runtime.InteropServices;
using System.Text;
using System.Threading.Tasks;

namespace WindowsFormsApp1
{

  public class DateTimeSynchronization
  {
    [StructLayout(LayoutKind.Sequential)]
    private struct Systemtime
    {
      public short year;
      public short month;
      public short dayOfWeek;
      public short day;
      public short hour;
      public short minute;
      public short second;
      public short milliseconds;
    }

    [DllImport("kernel32.dll")]
    private static extern bool SetLocalTime(ref Systemtime time);

    private static uint swapEndian(ulong x)
    {
      return (uint)(((x & 0x000000ff) << 24) +
      ((x & 0x0000ff00) << 8) +
      ((x & 0x00ff0000) >> 8) +
      ((x & 0xff000000) >> 24));
    }

    /// <summary>
    /// 設(shè)置系統(tǒng)時間
    /// </summary>
    /// <param name="dt">需要設(shè)置的時間</param>
    /// <returns>返回系統(tǒng)時間設(shè)置狀態(tài),true為成功,false為失敗</returns>
    private static bool SetLocalDateTime(DateTime dt)
    {
      Systemtime st;
      st.year = (short)dt.Year;
      st.month = (short)dt.Month;
      st.dayOfWeek = (short)dt.DayOfWeek;
      st.day = (short)dt.Day;
      st.hour = (short)dt.Hour;
      st.minute = (short)dt.Minute;
      st.second = (short)dt.Second;
      st.milliseconds = (short)dt.Millisecond;
      bool rt = SetLocalTime(ref st);
      return rt;
    }
    private static IPAddress iPAddress = null;
    public static bool Synchronization(string host, out DateTime syncDateTime, out string message)
    {
      syncDateTime = DateTime.Now;
      try
      {
        message = "";
        if (iPAddress == null)
        {
          var iphostinfo = Dns.GetHostEntry(host);
          var ntpServer = iphostinfo.AddressList[0];
          iPAddress = ntpServer;
        }
        DateTime dtStart = DateTime.Now;
        //NTP消息大小摘要是16字節(jié) (RFC 2030)
        byte[] ntpData = new byte[48];
        //設(shè)置跳躍指示器、版本號和模式值
        // LI = 0 (no warning), VN = 3 (IPv4 only), Mode = 3 (Client Mode)
        ntpData[0] = 0x1B; 
        IPAddress ip = iPAddress;
        // NTP服務(wù)給UDP分配的端口號是123
        IPEndPoint ipEndPoint = new IPEndPoint(ip, 123);
        // 使用UTP進行通訊
        Socket socket = new Socket(AddressFamily.InterNetwork, SocketType.Dgram, ProtocolType.Udp);
        socket.Connect(ipEndPoint);
        socket.ReceiveTimeout = 3000;
        socket.Send(ntpData);
        socket.Receive(ntpData);
        socket?.Close();
        socket?.Dispose();
        DateTime dtEnd = DateTime.Now;
        //傳輸時間戳字段偏移量,以64位時間戳格式,應(yīng)答離開客戶端服務(wù)器的時間
        const byte serverReplyTime = 40;
        // 獲得秒的部分
        ulong intPart = BitConverter.ToUInt32(ntpData, serverReplyTime);
        //獲取秒的部分
        ulong fractPart = BitConverter.ToUInt32(ntpData, serverReplyTime + 4);
        //由big-endian 到 little-endian的轉(zhuǎn)換
        intPart = swapEndian(intPart);
        fractPart = swapEndian(fractPart);
        ulong milliseconds = (intPart * 1000) + ((fractPart * 1000) / 0x100000000UL);
        // UTC時間
        DateTime webTime = (new DateTime(1900, 1, 1, 0, 0, 0, DateTimeKind.Utc)).AddMilliseconds(milliseconds);
        //本地時間
        DateTime dt = webTime.ToLocalTime();
        bool isSuccess = SetLocalDateTime(dt);
        syncDateTime = dt;

      }
      catch (Exception ex)
      {
        message = ex.Message;
        return false;
      }
      return true;

    }
  }
}

以上就是如何使用C#修改本地Windows系統(tǒng)時間的詳細內(nèi)容,更多關(guān)于c#修改系統(tǒng)時間的資料請關(guān)注腳本之家其它相關(guān)文章!

相關(guān)文章

  • WPF中的ValidationRule實現(xiàn)參數(shù)綁定解決方案

    WPF中的ValidationRule實現(xiàn)參數(shù)綁定解決方案

    在WPF中,默認情況下,DataContext是通過可視化樹來傳遞的,父元素的DataContext會自動傳遞給其子元素,以便子元素可以訪問父元素的數(shù)據(jù)對象,這篇文章主要介紹了WPF中的ValidationRule實現(xiàn)參數(shù)綁定解決方案,需要的朋友可以參考下
    2023-08-08
  • C#自定義基于控制臺的Timer實例

    C#自定義基于控制臺的Timer實例

    這篇文章主要介紹了C#自定義基于控制臺的Timer實現(xiàn)方法,可以簡單模擬timer控件的相關(guān)功能,具有一定參考借鑒價值,需要的朋友可以參考下
    2015-08-08
  • C#、ASP.NET通用擴展工具類之TypeParse

    C#、ASP.NET通用擴展工具類之TypeParse

    這篇文章主要介紹了C#、ASP.NET通用擴展工具類之TypeParse,使用了此類,類型轉(zhuǎn)換方便多了,本文直接給出實現(xiàn)代碼和使用方法,需要的朋友可以參考下
    2015-06-06
  • C#判斷當前程序是否通過管理員運行的方法

    C#判斷當前程序是否通過管理員運行的方法

    這篇文章主要介紹了C#判斷當前程序是否通過管理員運行的方法,可通過非常簡單的系統(tǒng)函數(shù)調(diào)用實現(xiàn)對當前程序是否通過管理員運行進行判定,是非常實用的技巧,需要的朋友可以參考下
    2014-11-11
  • C#播放背景音樂的方法小結(jié)

    C#播放背景音樂的方法小結(jié)

    這篇文章主要介紹了C#播放背景音樂的方法,實例總結(jié)了C#播放背景音樂的相關(guān)技巧,非常具有實用價值,需要的朋友可以參考下
    2015-04-04
  • C#網(wǎng)絡(luò)適配器簡單操作

    C#網(wǎng)絡(luò)適配器簡單操作

    這篇文章主要介紹了C#網(wǎng)絡(luò)適配器簡單操作,提供多種相關(guān)的輔助方法類,感興趣的小伙伴們可以參考一下
    2016-10-10
  • C#在DataTable中根據(jù)條件刪除某一行的實現(xiàn)方法

    C#在DataTable中根據(jù)條件刪除某一行的實現(xiàn)方法

    我們通常的方法是把數(shù)據(jù)源放在DataTable里面,但是偶爾也會需要把不要的行移除,怎么實現(xiàn)呢,下面通過代碼給大家介紹c# atatable 刪除行的方法,需要的朋友一起看下吧
    2016-05-05
  • C#二維數(shù)組基本用法實例

    C#二維數(shù)組基本用法實例

    這篇文章主要介紹了C#二維數(shù)組基本用法,以實例形式分析了C#中二維數(shù)組的定義、初始化、遍歷及打印等用法,具有一定參考借鑒價值,需要的朋友可以參考下
    2015-10-10
  • C# 和 Python 的 hash_md5加密方法

    C# 和 Python 的 hash_md5加密方法

    這篇文章主要介紹了C# 和 Python 的 hash_md5加密方法,文章圍繞著C# 和 Python 的 hash_md5加密的相關(guān)資料展開文章的詳細呢偶然,需要的朋友可以參考一下,希望對你有所幫助
    2021-11-11
  • C#實現(xiàn)拷貝文件的9種方法小結(jié)

    C#實現(xiàn)拷貝文件的9種方法小結(jié)

    最近遇一個問題,一個程序調(diào)用另一個程序的文件,結(jié)果另一個程序的文件被占用,使用不了文件,這時候的解決方案就是把另一個程序的文件拷貝到當前程序就可以了,本文介紹用C#拷貝文件的多種方式,需要的朋友可以參考下
    2024-04-04

最新評論