如何在 C# 中设置日期/时间

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/278936/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-03 21:19:04  来源:igfitidea点击:

How to set the date/time in C#

c#datetime

提问by Robert

How do I set the date/time of the computer in C#?

如何在 C# 中设置计算机的日期/时间?

采纳答案by Powerlord

You can set the date using Microsoft.VisualBasic.Todayand Microsoft.VisualBasic.TimeOfDay, although they're subject to security restrictions.

您可以使用Microsoft.VisualBasic.TodayMicrosoft.VisualBasic.TimeOfDay设置日期,尽管它们受到安全限制。

Yes, it's strange accessing the Microsoft,VisualBasic namespace, but there doesn't appear to be a C# equivalent.

是的,访问 Microsoft,VisualBasic 命名空间很奇怪,但似乎没有 C# 等效项。

回答by Greg Hewgill

You will need to call the SetSystemTimeWin32 API function through P/Invoke.

您将需要通过 P/Invoke 调用SetSystemTimeWin32 API 函数。

回答by Luke

You'll have to use a P/Invoke to the Windows API.

您必须对 Windows API 使用 P/Invoke。

Here's some example codein C#

这是C# 中的一些示例代码

回答by Jon Dewees

Here'sa bit of code to show how to use the Pinvoke.

这里有一些代码来展示如何使用 Pinvoke。