In visual studio, how can I write a macro that will insert the date into a comment?

廉价感情. 提交于 2019-12-23 20:15:13

问题


My question is as exact as it is. I am wondering if it would be possible to insert the date or the date and time into a comment without having to manually write it out. But what I would really like to know is, is there a way to do this and will I have to implement something with visual studio to do this for me and if so, how can I do this? Any help will be greatly appreciated! :)
Also, I am coding in C-Sharp and am using Visual Studio 2010.


回答1:


In Visual Studio 2010 you write macros in Visual Basic and add them to the Macro Explorer. Then call the macros while working in your C# code editor. I bind my favorite macros to key combinations, so I can quickly run them when needed.

If you are new to macros and the Visual Studio Macro editor check out this link. MSDN docs for Macros

Here is the code to add a comment and date to your C# code.

 Public Sub AddCommentWithDate()
    Dim doc As Document = DTE.ActiveDocument
    ' only modify the doc, if it is a text document in VS
    Dim textDoc As TextDocument = _
       CType(doc.Object("TextDocument"), TextDocument)

    ' verify that the code editor is C#
    If doc.ProjectItem.ContainingProject.Kind = _
       VSLangProj.PrjKind.prjKindCSharpProject Then
        textDoc.StartPoint.CreateEditPoint()
        textDoc.Selection.Insert("// A comment " & Date.Now)
    End If
End Sub

Here's the results in a C# file.

// A comment 3/18/2013 2:13:38 AM



回答2:


You could either write a macro in Visual Studio or use some external program (such as AutoHotKey) to type the text for you.




回答3:


When someone posts a comment and you process the form data on your serverside, maybe you would add a hard return (
or chr(13)) and then add the date BEFORE persisting the comments to a database or xml file.

so you would end up with something like....

Comments == Comments + Strings.Chr(13) + System.DateTime.Today

or

Comments == Comments + ("<br />") + System.DateTime.Today


来源:https://stackoverflow.com/questions/15467271/in-visual-studio-how-can-i-write-a-macro-that-will-insert-the-date-into-a-comme

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!