2017-07-24 86 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代碼項目。

相關問題