2017-04-21 79 views
-2

我需要更改我的筆記本電腦與Windows 10使用C#的日期。當我使用Windows 7時,我已經在C#中創建了一個應用程序,並且它完美運行,但它不再有效,所以我不得不做一些調整。我目前正在以管理員身份運行該應用程序,並已自動關閉設置時間,但仍無效。這裏是我的代碼:更改窗口10日期與C#

void setDate(string dateInYourSystemFormat) { 
    var proc = new System.Diagnostics.ProcessStartInfo(); 
    proc.UseShellExecute = true; 
    proc.WorkingDirectory = @"C:\Windows\System32"; 
    proc.CreateNoWindow = true; 
    proc.FileName = @"C:\Windows\System32\cmd.exe"; 
    proc.Verb = "runas"; 
    proc.Arguments = "/C date " + dateInYourSystemFormat; 
    try { 
     System.Diagnostics.Process.Start(proc); 
    } 
    catch { 
     MessageBox.Show("Error to change time of your system"); 
     Application.ExitThread(); 
    } 
}  

請幫我在Windows 10中用C#代碼更改日期。
,我從計算器另一張貼此代碼,它說,這是在Windows 8的測試,但我似乎無法使其工作在Windows 10

+0

愚蠢的問題。什麼.net版本是你的應用程序,你有這個.net版本安裝在新的Windows 10筆記本電腦上?另外,你看到了什麼錯誤? – Ethilium

+0

我使用的是.Net Framework 4.5,沒有錯誤,只是執行而沒有任何反應。我已經有了這個.Net Framework,因爲它包含在Windows 10中,我已經嘗試過安裝它,並且我收到了這個消息。 – user1905507

+0

這可以在我的機器上使用此調用: setDate(「2017/4/22」); –

回答

0

警告:未經測試的代碼(不捨得我的傻瓜PC日期/時間)。

也許你可以嘗試正確的Windows API?

public 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("coredll.dll", SetLastError = true)] 
static extern bool SetSystemTime(ref SYSTEMTIME time); 

void setDate(DateTime aDateTime) { 
    var aSYSTEMTIME = new SYSTEMTIME(); 
    aSYSTEMTIME.year = (short)aDateTime.Year; 
    aSYSTEMTIME.month = (short)aDateTime.Month; 
    aSYSTEMTIME.day = (short)aDateTime.Day; 

    SetSystemTime(ref aSYSTEMTIME); 
}