How do you get the current solution directory from a VSPackage? - c#

Following is how you would get the current solution directory from an add-in:
_applicationObject = (DTE2)application; // retrieved from OnConnection method
string solutionDir = System.IO.Path.GetDirectoryName(_applicationObject.Solution.FullName);
How would you do this via a VSPackage?
I'm migrating a visual studio add-in to a VSPackage as I'm intending to add some user controls that require a deeper integration with the IDE.
I found some good references on the relative merits of add-ins vs integration packages such as:
http://nayyeri.net/visual-studio-addin-vs-integration-package-part-1
And some good tutorials on msdn on VSPackages such as:
http://msdn.microsoft.com/en-us/library/cc138589.aspx
I haven't found a good reference yet (on msdn or otherwise) on how the higher level interfaces in add-ins (such as DTE) map to lower level interfaces in VSPackages.
Any good references out there to help with general mapping from add-in interfaces to VSPackage interfaces?

I found the answer to the specific question. The VisualStudio.DTE object can be retrieved via the GetService() method as follows:
// Get an instance of the currently running Visual Studio IDE
DTE dte = (DTE)GetService(typeof(DTE));
string solutionDir = System.IO.Path.GetDirectoryName(dte.Solution.FullName);

You can get a DTE object from one of these functions:
public static DTE GetCurrentDTE(IServiceProvider provider)
{
/*ENVDTE. */DTE vs = (DTE)provider.GetService(typeof(DTE));
if (vs == null) throw new InvalidOperationException("DTE not found.");
return vs;
}
public static DTE GetCurrentDTE()
{
return GetCurrentDTE(/* Microsoft.VisualStudio.Shell. */ServiceProvider.GlobalProvider);
}
After that, you can get active Solution from DTE.Solution and Solution path from DTE.Solution.Path property.

If using the IVsSolution interface, you can use GetSolutionInfo to obtain the path of the solution, the solution filename, and the solution user options (SUO) filename:
this.solution.GetSolutionInfo(
out string solutionDirectory,
out string solutionFile,
out string userOptsFile);

Related

How to get "Include Paths" property of Microsoft Macro Assembler in Visual Studio by a plugin?

I'm workng to get include path resolved by some VS plugin (asm-dude in fact). Include path in microsoft macro assembler looks like this:
includepath
Include file resolve part in asm-dude lies in: https://github.com/HJLebbink/asm-dude/blob/vxix2022-B/VS/CSHARP/asm-dude-vsix/Tools/LabelGraph.cs#L602
Anyway, at the beginning I think I just need to get the value of IncludePath property, and then other things can be done in a minute. But after reading some docs I realized I'm in a mess. It seems that VS prevents me to get names of all properties, but I can only get the value by the name.
Codes I write are like:
DTE dte = Package.GetGlobalService(typeof(SDTE)) as DTE;
Projects projects = dte.Solution.Projects;
if (projects.Count != 0)
{
VCProject project = (VCProject)projects.Item(1).Object;
VCConfiguration cfg = project.ActiveConfiguration;
if (cfg != null)
{
string includePathStr = cfg.GetEvaluatedPropertyValue("IncludePaths");
}
}
but in vein, it gets include path of msvc, not MASM
I cast Project to VCProject because it's a VC project. Although I can iterate properties of a non-VCProject's configuration, but it doesn't seem to work on VCProject, because it doesn't have a (at least not public) member named properties. All these docs tell me that I can only get its value by name, but the problem is I don't know its name. Or I'm completely wrong? I must admit that I'm new to VS plugins.
refs I used so far:
https://learn.microsoft.com/en-us/previous-versions/dn655034(v=vs.140)?redirectedfrom=MSDN
https://learn.microsoft.com/en-us/dotnet/api/microsoft.visualstudio.vcprojectengine.vcconfiguration?view=visualstudiosdk-2022

Getting the StartArguments from a VSIX plugin for .NET Core projects

EDIT - Looks like the initial problem only applies to .NET Core projects. So the question shifts to "What is the correct way to get the full range of project properties from .NET Core projects?"
I'm writing a Visual Studio 2019 plugin that uses some of the project configuration settings. It seems straight forward enough to get the Project object (C# here, but also C++ & others) and then spelunking the Configuration's for Property objects.
But it appears that accessing most of the properties will throw System.NotImplementedException.
Primary Question: Is there another way to access these settings - like startup arguments and other debugging configuration?
Secondary Question: Are there any good resources on this stuff? The online MS docs are a bit terse for my taste.
void ProcessCSharpProject(VSProject csProj)
{
foreach (var config in csProj.Project.ConfigurationManager.Cast<Configuration>())
{
Property debugInfoProp = config.Properties.Item("DebugInfo");
var debugInfo = debugInfoProp.Value as String; // works
Property startArgsProp = config.Properties.Item("StartArguments");
var startArgs = startArgsProp.Value as String; // NotImplemented
// Another way to access the same thing:
var configProps = config.Object as CSharpProjectConfigurationProperties6;
var startArgs2 = configProps.StartArguments; // Also NotImplemented
}
}
Thanks!

Visual Studio Extension: Get Package from adornment

I'm developing a Visual Studio Extension, made up of:
A menu and series of commands
A tools window
One or more textview adornments
A custom implementation of AysncPackage
Now, while the Tools Window and the commands are either wired up by, or have a handle on, the AsyncPackage for my extension, what I cannot figure out is HOW I get a handle to the self-same AsyncPackage from one or more of my text adornments.
For example, my Tools Window extends ToolWindowPane, which has a hook to the Package via the Package's ProvideToolWindow attribute. My commands are constructed inside the Package itself, so passing a handle to the AsyncPackage is simple enough.
What I cannot work out is HOW you get a reference to this AsyncPackage inside any of my TextAdornments.
Any help?
This was a tricky one! You must get the IVsShell to retrieve a package based on a GUID you associate with your Package, and then cast it to your interface (or the base interface of IPackage)
private IMyPackageInterface _myPackage;
//let's get our hands on that package
var vsShell = (IVsShell) ServiceProvider.GlobalProvider.GetService(typeof(IVsShell));
if (vsShell == null)
{
throw new NullReferenceException();
}
if (vsShell.IsPackageLoaded(PackageGuid, out var myPossiblePackage)
== Microsoft.VisualStudio.VSConstants.S_OK) {
_myPackage = (IMyPackageInterface)myPossiblePackage;

How do I retrieve text from the Visual Studio editor for use with Roslyn SyntaxTree?

I am attempting to write a Visual Studio extension that will analyze the C# code displayed in the editor and possibly update the code based on what I find. This would be on demand (via a menu item), and not using an analyzer and code fix.
There are a number of examples and samples on the Internet, but they all start either with the source code hard-coded in the samples, or create a new document, or look at each file in the VS solution that is open. How do I access the source code from the active editor window?
In a comment to my original question, #SJP gave a link to #Frank Bakker's answer to the question at Calling Roslyn from VSIX Command. This does work as outlined.
#JoshVarty provided a hint of the direction to go in his answer above. I combined that with code provided by #user1912383 for how to get an IWpfTextView answering the question Find an IVsTextView or IWpfTextView for a given ProjectItem, in 2010 RC extension. Here is the code I came up with:
var componentModel = (IComponentModel)Package.GetGlobalService(typeof(SComponentModel));
var textManager = (IVsTextManager)Package.GetGlobalService(typeof(SVsTextManager));
IVsTextView activeView = null;
ErrorHandler.ThrowOnFailure(textManager.GetActiveView(1, null, out activeView));
var editorAdapter = componentModel.GetService<IVsEditorAdaptersFactoryService>();
var textView = editorAdapter.GetWpfTextView(activeView);
var document = (textView.TextBuffer.ContentType.TypeName.Equals("CSharp"))
? textView : null;
In a comment after #user1912383's code mentioned above, #kman mentioned that this does not work for document types such as .sql files. It does, however, work for .cs files which is what I will be using it with.
First, you need to install the Microsoft.CodeAnalysis.EditorFeatures.Text package.
Then you need to add the appropriate using statement:
using Microsoft.CodeAnalysis.Text;
Now you can map between Visual Studio concepts (ITextSnapshot, ITextBuffer etc.) and Roslyn concepts (Document, SourceText etc.) with the extension methods found here: https://github.com/dotnet/roslyn/blob/master/src/EditorFeatures/Text/Extensions.cs
For example:
ITextSnapshot snapshot = ... //Get this from Visual Studio
var documents = snapshot.GetRelatedDocuments(); //There may be more than one

C# Add-in: How do you access runtime instances of objects while debugging?

I am developing an add in for C# that would only be used during debugging. Once instantiated, my add in needs to find all instances of a specific class or interface to display a graph about the data found.
How exactly can I find or access these objects in my extension? I have access to the DTE2 application object in my extension, but I'm not sure how to search the actual code being debugged by VS. I'm thinking I might somehow be able to use Reflection, but I'm not sure where to look.
Thanks.
I've implemented a plugin that searches through dlls in a given directory and finds classes that implement a particular interface. Below is the class I used to do this:
public class PlugInFactory<T>
{
public T CreatePlugin(string path)
{
foreach (string file in Directory.GetFiles(path, "*.dll"))
{
foreach (Type assemblyType in Assembly.LoadFrom(file).GetTypes())
{
Type interfaceType = assemblyType.GetInterface(typeof(T).FullName);
if (interfaceType != null)
{
return (T)Activator.CreateInstance(assemblyType);
}
}
}
return default(T);
}
}
All you have to do is initialize this class with something like this:
PluginLoader loader = new PlugInFactory<InterfaceToSearchFor>();
InterfaceToSearchFor instanceOfInterface = loader.CreatePlugin(AppDomain.CurrentDomain.BaseDirectory);
This type of operation isn't really possible from a Visual Studio plugin. The object alive when debugging live in the debugee process while your add-in is running in the Visual Studio process. It's not possible to access arbitrary objects across process boundaries in .Net.

Categories