如何复制Visual studio当前行号

How to copy the Visual studio current line number

如何使用 C# copy/get Visual Studio 活动文档中的当前行号

首先,您需要为您的 C# 项目添加引用 "envDTE" 和 "envDTE80"。

然后使用下面的代码(在我的例子中,我把它放在点击按钮事件中)将行号(和文件名)复制到剪贴板。

    private void btnGetLineVS_Click(object sender, EventArgs e)
    {
        EnvDTE80.DTE2 dte2;
        dte2 = (EnvDTE80.DTE2)System.Runtime.InteropServices.Marshal.GetActiveObject("VisualStudio.DTE");
        dte2.MainWindow.Activate();
        int line = ((EnvDTE.TextSelection)dte2.ActiveDocument.Selection).ActivePoint.Line;

        //Show it to the user the way you like
        StringBuilder builder = new StringBuilder();
        builder.Append(dte2.ActiveDocument.FullName);//The file name
        builder.Append('\t');
        builder.Append(line);//The current line
        if (builder.Length > 0)
        {
            Clipboard.SetText(builder.ToString());
            MessageBox.Show("Copied to clipboard");
        }
        else
            MessageBox.Show("Nothing!");
    }

感谢Reder的answer让我知道了这种事情的存在,我一直想这样做,我们必须使用VSIX Visual Studio代码项目。