[英]Setting internet time server on windows embedded standard through batch or similar
[英]Setting Date and Time on Windows Embedded Standard 7 OS
我正在使用C#.net在Windows Embedded Standard 7操作系統上設置日期和時間。
我嘗試使用以下鏈接中的代碼更改系統日期。
http://www.pinvoke.net/default.aspx/kernel32.setsystemtime
但我也看到一個人應該獲得改變相同的特權。 但我收到一個錯誤。 這是代碼
Privileges.EnablePrivilege(SecurityEntity.SE_SYSTEMTIME_NAME);
if (SetSystemTime(ref st) == 0)
{
return 0;
}
return 1;
為了獲得私有化,我使用了以下鏈接中的代碼。
http://www.pinvoke.net/default.aspx/advapi32/AdjustTokenPrivileges.html C#示例代碼2(完整錯誤處理):
我有疑問:是否可以使用PInvoke更改日期和時間。 如果有可能,我應該在操作系統上進行哪些更改/設置。
還有什么其他方法可以改變日期和時間。
謝謝
編輯:
[DllImport("kernel32.dll")]
extern static uint SetSystemTime(ref SYSTEMTIME lpSystemTime);
public int SetSystemDateTime(DateTime NewSystemDateTime)
{
SYSTEMTIME st = new SYSTEMTIME();
st.wYear = (ushort)NewSystemDateTime.Year;
st.wMonth = (ushort)NewSystemDateTime.Month;
st.wDay = (ushort)NewSystemDateTime.Day;
st.wHour = (ushort)NewSystemDateTime.Hour;
st.wMinute = (ushort)NewSystemDateTime.Minute;
st.wMilliseconds = (ushort)NewSystemDateTime.Millisecond;
Privileges.EnablePrivilege(SecurityEntity.SE_SYSTEMTIME_NAME);
if (SetSystemTime(ref st) == 0)
{
return 0;
}
return 1;
}
好的,我做了一些研究,我嘗試了一些東西。 您需要管理員權限才能更改系統日期和時間,一個解決方案是添加
<requestedExecutionLevel level="requireAdministrator" uiAccess="false" />
到應用程序清單文件,這樣應用程序將始終詢問用戶的權限。
在代碼項目上,有一篇文章可能能夠在不添加清單文件的情況下幫助ypu。
http://www.codeproject.com/Articles/125810/A-complete-Impersonation-Demo-in-C-NET
我終於找到了解決方案。 以下鏈接中的代碼對我來說很好: http : //www.pinvoke.net/default.aspx/kernel32/SetLocalTime.html
另外,我在stackoverflow中的一個帖子中找到的代碼,幫助了我。
using System.Security.Principal;
bool IsUserAdministrator()
{
bool isAdmin;
try
{
WindowsIdentity user = WindowsIdentity.GetCurrent();
WindowsPrincipal principal = new WindowsPrincipal(user);
isAdmin = principal.IsInRole(WindowsBuiltInRole.Administrator);
}
catch (UnauthorizedAccessException ex)
{
isAdmin = false;
}
catch (Exception ex)
{
isAdmin = false;
}
return isAdmin;
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.