C# 在 Visual Studio Snippet 中插入当前日期时间

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/1029189/
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-06 05:55:02  来源:igfitidea点击:

Insert current datetime in Visual Studio Snippet

c#visual-studio-2008code-snippets

提问by Scott Ivey

Does anyone know of a way that I can insert the current date & time in a visual studio 2008 snippet? What I want is something like this in the body of my .snippet file...

有谁知道我可以在 Visual Studio 2008 片段中插入当前日期和时间的方法吗?我想要的是我的 .snippet 文件正文中的类似内容...

  <Code Language="csharp">
    <![CDATA[
  // $DateTime$
  // more code here for my snippet...
  </Code>

采纳答案by Andrew Hare

There are no DateTime functions available for snippets but here is a macro that will insert the current DateTime:

片段没有可用的 DateTime 函数,但这里有一个宏可以插入当前的 DateTime:

Sub PrintDateTime()
    If (Not IsNothing(DTE.ActiveDocument)) Then
        Dim selection As TextSelection = DTE.ActiveDocument.Selection
        selection.Insert(DateTime.Now.ToString())
    End If
End Sub

You can open your macro explorer with Alt+ F8and create a new module and paste the code above inside the module that is generated.

您可以使用Alt+打开宏资源管理器F8并创建一个新模块并将上面的代码粘贴到生成的模块中。

Then create a new keyboard shortcut and bind it to your macro.

然后创建一个新的键盘快捷键并将其绑定到您的宏。