Skip to content

Instantly share code, notes, and snippets.

@timsneath
Created March 11, 2017 23:46
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save timsneath/d7ad31795dab8da6dafe27d5623a3b2f to your computer and use it in GitHub Desktop.
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
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