Created
March 11, 2017 23:46
-
-
Save timsneath/d7ad31795dab8da6dafe27d5623a3b2f to your computer and use it in GitHub Desktop.
Gets the filename for the file currently visible in the Visual Studio code editor
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
private string GetCurrentFilenameFromEditor() | |
{ | |
var textManager = this.ServiceProvider.GetService(typeof(SVsTextManager)) as IVsTextManager; | |
int mustHaveFocus = 1; | |
textManager.GetActiveView(mustHaveFocus, null, out IVsTextView textView); | |
var userData = textView as IVsUserData; | |
if (userData == null) | |
{ | |
// no text view is currently open | |
return String.Empty; | |
} | |
else | |
{ | |
Guid guidViewHost = DefGuidList.guidIWpfTextViewHost; | |
userData.GetData(ref guidViewHost, out object holder); | |
IWpfTextViewHost viewHost = (IWpfTextViewHost)holder; | |
viewHost.TextView.TextDataModel.DocumentBuffer.Properties.TryGetProperty(typeof(ITextDocument), out ITextDocument doc); | |
return doc.FilePath; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment