最新国产好看的视频,伊人天堂AV在线,国产Aaaaaa视频,蜜臀视频在线观看一区,人妻av色图,密臀久久久精品影片,青青视频免费观看毛片,久草在线观看视,国产三级精品色情在线

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

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

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

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

1. 首先需要獲得校準(zhǔn)時(shí)間,兩種方式:

通過可上網(wǎng)的電腦進(jìn)行外部獲取當(dāng)前時(shí)間。

通過NTP實(shí)現(xiàn)

 //NTP消息大小摘要是16字節(jié) (RFC 2030)
 byte[] ntpData = new byte[48];
 //設(shè)置跳躍指示器、版本號(hào)和模式值
 // LI = 0 (no warning), VN = 3 (IPv4 only), Mode = 3 (Client Mode)
 ntpData[0] = 0x1B; 
 IPAddress ip = iPAddress;
 // NTP服務(wù)給UDP分配的端口號(hào)是123
 IPEndPoint ipEndPoint = new IPEndPoint(ip, 123);
 // 使用UTP進(jìn)行通訊
 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();

程序手動(dòng)輸入。

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

 //傳輸時(shí)間戳字段偏移量,以64位時(shí)間戳格式,應(yīng)答離開客戶端服務(wù)器的時(shí)間
 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時(shí)間
 DateTime webTime = (new DateTime(1900, 1, 1, 0, 0, 0, DateTimeKind.Utc)).AddMilliseconds(milliseconds);
 //本地時(shí)間
 DateTime dt = webTime.ToLocalTime();

3. 獲取當(dāng)前是否是管理員

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í)間進(jìn)行本地設(shè)置,并返回成功與否
bool isSuccess = SetLocalDateTime(dt);

5. 提升權(quán)限

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

引入引用程序清單文件(app.manifest),步驟:添加新建項(xiàng)->選擇‘應(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

默認(rèn)權(quán)限:

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

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

highestAvailable 表示提升當(dāng)前用戶最高權(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)時(shí)間
    /// </summary>
    /// <param name="dt">需要設(shè)置的時(shí)間</param>
    /// <returns>返回系統(tǒng)時(shí)間設(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è)置跳躍指示器、版本號(hào)和模式值
        // LI = 0 (no warning), VN = 3 (IPv4 only), Mode = 3 (Client Mode)
        ntpData[0] = 0x1B; 
        IPAddress ip = iPAddress;
        // NTP服務(wù)給UDP分配的端口號(hào)是123
        IPEndPoint ipEndPoint = new IPEndPoint(ip, 123);
        // 使用UTP進(jìn)行通訊
        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;
        //傳輸時(shí)間戳字段偏移量,以64位時(shí)間戳格式,應(yīng)答離開客戶端服務(wù)器的時(shí)間
        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時(shí)間
        DateTime webTime = (new DateTime(1900, 1, 1, 0, 0, 0, DateTimeKind.Utc)).AddMilliseconds(milliseconds);
        //本地時(shí)間
        DateTime dt = webTime.ToLocalTime();
        bool isSuccess = SetLocalDateTime(dt);
        syncDateTime = dt;

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

    }
  }
}

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

相關(guān)文章

  • C#實(shí)現(xiàn)的二維數(shù)組排序算法示例

    C#實(shí)現(xiàn)的二維數(shù)組排序算法示例

    這篇文章主要介紹了C#實(shí)現(xiàn)的二維數(shù)組排序算法,涉及C#針對(duì)二維數(shù)組的遍歷、判斷、排序等相關(guān)操作技巧,需要的朋友可以參考下
    2017-12-12
  • C#中實(shí)現(xiàn)一次執(zhí)行多條帶GO的sql語句實(shí)例

    C#中實(shí)現(xiàn)一次執(zhí)行多條帶GO的sql語句實(shí)例

    這篇文章主要介紹了C#中實(shí)現(xiàn)一次執(zhí)行多條帶GO的sql語句,以實(shí)例形式較為詳細(xì)的分析了C#執(zhí)行sql語句的相關(guān)技巧,具有一定參考借鑒價(jià)值,需要的朋友可以參考下
    2015-09-09
  • C#實(shí)現(xiàn)對(duì)數(shù)組進(jìn)行隨機(jī)排序類實(shí)例

    C#實(shí)現(xiàn)對(duì)數(shù)組進(jìn)行隨機(jī)排序類實(shí)例

    這篇文章主要介紹了C#實(shí)現(xiàn)對(duì)數(shù)組進(jìn)行隨機(jī)排序類,實(shí)例分析了C#數(shù)組與隨機(jī)數(shù)操作技巧,非常具有實(shí)用價(jià)值,需要的朋友可以參考下
    2015-03-03
  • Unity實(shí)現(xiàn)打磚塊游戲

    Unity實(shí)現(xiàn)打磚塊游戲

    這篇文章主要為大家詳細(xì)介紹了Unity實(shí)現(xiàn)打磚塊游戲,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下
    2022-05-05
  • C#實(shí)現(xiàn)查殺本地與遠(yuǎn)程進(jìn)程的方法

    C#實(shí)現(xiàn)查殺本地與遠(yuǎn)程進(jìn)程的方法

    這篇文章主要介紹了C#實(shí)現(xiàn)查殺本地與遠(yuǎn)程進(jìn)程的方法,可實(shí)現(xiàn)針對(duì)特定進(jìn)程的關(guān)閉操作,是C#進(jìn)程操作的一個(gè)典型應(yīng)用,需要的朋友可以參考下
    2014-12-12
  • C#實(shí)現(xiàn)壓縮HTML代碼的方法

    C#實(shí)現(xiàn)壓縮HTML代碼的方法

    這篇文章主要介紹了C#實(shí)現(xiàn)壓縮HTML代碼的方法,是非常實(shí)用的功能,需要的朋友可以參考下
    2014-09-09
  • C# 獲取指定QQ頭像繪制圓形頭像框GDI(Graphics)的方法

    C# 獲取指定QQ頭像繪制圓形頭像框GDI(Graphics)的方法

    某論壇的評(píng)論區(qū)模塊,發(fā)現(xiàn)這功能很不錯(cuò),琢磨了一晚上做了大致一樣的,用來當(dāng)做 注冊(cè)模塊 的頭像綁定功能,下面通過實(shí)例代碼給大家介紹下C# 獲取指定QQ頭像繪制圓形頭像框GDI(Graphics)的方法,感興趣的朋友一起看看吧
    2021-11-11
  • Unity C#打包AssetBundle與場景詳解

    Unity C#打包AssetBundle與場景詳解

    這篇文章主要給大家介紹了關(guān)于Unity C#打包AssetBundle與場景的相關(guān)資料,文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面來一起學(xué)習(xí)學(xué)習(xí)吧
    2019-02-02
  • 淺析C#異步中的Overlapped是如何尋址的

    淺析C#異步中的Overlapped是如何尋址的

    用ReadAsync做文件異步讀取時(shí),在Win32層面會(huì)傳lpOverlapped到內(nèi)核層,那在內(nèi)核層回頭時(shí),它是如何通過這個(gè)lpOverlapped尋找到?ReadAsync這個(gè)異步的Task的呢,下面我們就來簡單分析一下吧
    2025-01-01
  • .net文件上傳時(shí)實(shí)現(xiàn)通過文件頭確認(rèn)文件類型的方法

    .net文件上傳時(shí)實(shí)現(xiàn)通過文件頭確認(rèn)文件類型的方法

    這篇文章主要介紹了.net文件上傳時(shí)實(shí)現(xiàn)通過文件頭確認(rèn)文件類型的方法,很實(shí)用的功能,需要的朋友可以參考下
    2014-07-07

最新評(píng)論

天门市| 濮阳市| 新泰市| 琼中| 镇原县| 隆回县| 茂名市| 宁海县| 元江| 来宾市| 淳安县| 巍山| 宁化县| 家居| 新化县| 红桥区| 阆中市| 偏关县| 郴州市| 噶尔县| 慈溪市| 泸州市| 纳雍县| 柳林县| 政和县| 克什克腾旗| 济宁市| 栾城县| 肇源县| 绥芬河市| 蓬安县| 香格里拉县| 义乌市| 崇仁县| 营山县| 金堂县| 保山市| 得荣县| 台山市| 大新县| 林州市|