2017-07-24 79 views
0

如何复制/使用C#如何复制Visual Studio的当前行号

+0

请参阅https://stackoverflow.com/questions/32502847/is-there-any-extension-for-vs-copying-code-position –

+0

@Sergey Vlasov:哦,对不起,我不知道这个问题被回答 - 我已经搜查,但无法找到。 – 123iamking

回答

1

首先获取Visual Studio中的活动文档中当前的行号,你需要添加引用“envDTE”和“envDTE80”为您的C#项目。

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

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!"); 
    } 

感谢瑞德的answer,我知道这种事情存在,我一直认为要做到这一点,我们必须使用VSIX Visual Studio代码项目。

相关问题