2017-04-21 76 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); 
}