Where to get Windows.VisualStudio.Data? - c#

Hello I'm am beginning with visual studio and C#
I saw a very nice thread here: Display a ConnectionString dialog
Sadly the Link for the sample code is dead
Either way I need to use the object : DataConnectionDialog
this object is in the Windows.VisualStudio.Data namespace. Where can I Include that it's not there in the add reference window. Where can I download that.
I am using VS community 2013

You can add reference to Microsoft.Data.ConnectionUI.Dialog.dll which can be found under
C:\Program Files (x86)\Microsoft Visual Studio 12.0\Common7\IDE\Microsoft.Data.ConnectionUI.Dialog.dll
Microsoft.Data.ConnectionUI.DataConnectionDialog dlg = new Microsoft.Data.ConnectionUI.DataConnectionDialog();
Microsoft.Data.ConnectionUI.DataSource.AddStandardDataSources(dlg);
if (Microsoft.Data.ConnectionUI.DataConnectionDialog.Show(dlg) == DialogResult.OK)
{
}

Related

How to execute Visual Studio commands in C#?

I want to execute Visual Studio command to import/export settings file in C#.
Can I do this using DTE2. If yes how to do?
How to initialize DTE2 and do..Please give the complete code.
Can I make use of this?
ExecuteCommand("Tools.ImportandExportSettings", "/export:\"C:/temp/setttings.vssettings\"")
If yes - then how to initialize dte2 and call the method?
You can try the following code to execute Visual Studio command to import/export settings file in c#.
First, you need to install nuget package ->EnvDTE.
Second, you can use the following console app to get the settings file.
class Program
{
static void Main(string[] args)
{
var filename = "D:\\own.vssettings";
var dte = (EnvDTE.DTE)System.Runtime.InteropServices.Marshal.
GetActiveObject("VisualStudio.DTE.16.0"); // version neutral
dte.ExecuteCommand("Tools.ImportandExportSettings", "/export:" + filename);
}
}
We need to note that if your vs version is vs 2010, we should use VisualStudio.DTE.10.0,
If your vs version is vs2013, we should use VisualStudio.DTE.12.0, If your vs version is
vs2017 or later, we should use VisualStudio.DTE.16.0.
My vs version is vs2017, so I used VisualStudio.DTE.16.0.
Result:(The part of setting file)

Managed Package Framework for Visual Studio 2017

I'm following this tutorial on how to create a new Visual Studio Project type. In there, it says to "Import the source-code files for the Managed Package Framework". Google led me to this link that has a link to MPF 2013 package. In the first link they say to look for a file ProjectBase.files which does not exist in the second link download.
Questions:
Where is the correct MPF download for Visual Studio 2017.
In the future when we move on to Visual Studio 2019, will I need to download a new MPF for 2019?
I had the same problem, but it seems that I alreasy solved it. It seems that MPF is not needed anymore to do these steps and the tutorial is a bit outdated:
How to do it now:
Instead of loading the "Managed Package Framework code", skip this whole step in the tutorial and go to the next chaprer.
In the next chapter skip everything until step 3 and register
this.RegisterProjectFactory(new SimpleProjectFactory(this));
in the InitializeAsync Task of the SimpleProjectPackage.cs
At step 6 implement FlavoredProjectFactory instead of ProjectFactory
Continue the tutorial and it should work fine now.
In the end it should look like this:
class SimpleProjectFactory : FlavoredProjectFactory
{
private SimpleProjectPackage simpleProjectPackage;
public SimpleProjectFactory(SimpleProjectPackage simpleProjectPackage)
{
this.simpleProjectPackage = simpleProjectPackage;
}
protected override object PreCreateForOuter(object outerProject)
{
return null;
}
}

ExtensionManager null in Visual Studio 2017

In previous versions of Visual Studio I could use the following code to retrieve information about a certain installed extension (vsix):
IVsExtensionManager manager = ServiceProvider.GlobalProvider.GetService(typeof(SVsExtensionManager)) as IVsExtensionManager;
if (manager != null)
{
VsExtension extension = new VsExtension();
IInstalledExtension info = manager.GetInstalledExtension(cExtensionProductId);
}
In the new Visual Studio 2017 version, the 'manager' variable is always null. Microsoft changed the way to retrieve the information (they no longer use the system registry), but I can't find another way to retrieve the info.
Do you know where I can find more information and/or provide a sample of the new implementation?
Thank you in advance!
Please check that for VS 2017 you are using VS 2017 specific references to the extension manager. It should be Microsoft.VisualStudio.ExtensionManager.dll and
Microsoft.VisualStudio.ExtensionEngine.dll.
For a working example see https://vlasovstudio.com/visual-commander/commands.html#ExtensionsList.
Is the ServiceProvider.GlobalProvider.GetService(typeof(SVsExtensionManager)) returns null ? or "as IVsExtensionManager" becomes null ?

How do I open an .sln file in older Visual Studio in C# code?

I implemented a .NET application where the user browses for solution files. The user can choose to open these solution files in Visual Studio.
The problem is that the solutions need to be opened in Visual Studio 2008. Not in Visual Studio 2010 or above. This is necessary to keep the solution files in their original state.
All this should happen in C#. Bellow is the code but it opens the sln files with the default Visual Studio of the user.
if (file.EndsWith(".sln") && File.Exists(file))
System.Diagnostics.Process.Start(file);
Well, you are just launching a new process indicating file to the file and letting Windows to handle everything for you. Windows know that .SLN files should be opened with default associated program and that's it.
To change this behavior you have to programmatically analize the version of your SLN file, and based on SLN version open concrete version of VisualStudio passing as parameter the SLN file.
So you would need to emulate from your C# code something like:
C:\Program Files (x86)\Microsoft Visual Studio 14.0\Common7\IDE>devenv.exe "C:\PATH\SOLUTION.sln"
It could look somehow like this:
private static void Main(string[] args)
{
var slnPath = #"C:\PATH\SOLUTION.sln";
var slnVersion = GetVersion(slnPath);
switch (slnVersion)
{
case ...:
break;
case 14:
var startInfo = new ProcessStartInfo();
startInfo.FileName = #"C:\Program Files (x86)\Microsoft Visual Studio 14.0\Common7\IDE\devenv.exe";
startInfo.Arguments = slnPath;
Process.Start(startInfo);
break;
}
}
I figured out that it isn't necessary to detect the original version, in which the solutions were made. All solutions are created in Visual Studio 2008 by default.
Find the solution in the code bellow:
Add the reference EnvDTE.dll and using EnvDTE; in your code
public static void OpenSlnFile(string file)
{
System.Type type = Type.GetTypeFromProgID("VisualStudio.DTE.9.0");
EnvDTE.DTE dte = (EnvDTE.DTE)System.Activator.CreateInstance(type);
dte.MainWindow.Visible = true;
dte.Solution.Open(file);
}
Which program launched on clent depend on the registry key HKEY_CLASSES_ROOT\VisualStudio.Launcher.sln (referenced by HKEY_CLASSES_ROOT.sln)
When you open the file, it runs "c:\Program Files (x86)\Common Files\Microsoft Shared\MSEnv\VSLauncher.exe" "%1", which reads the version from the .sln file and open the correct version.
About .sln file structure you can read at https://msdn.microsoft.com/en-us/library/bb165951.aspx

How to programatically clean output in Visual Studio 2013

I print output on the console in WPF and ASP.NET-MVC applications by:
System.Diagnostics.Debug.WriteLine("text");
how to programatically clear the output window?
// Import EnvDTE and EnvDTE80 into your project
using EnvDTE;
using EnvDTE80;
protected void ClearOutput()
{
DTE2 ide = (DTE2)System.Runtime.InteropServices.Marshal.GetActiveObject("VisualStudio.DTE.12.0");
ide.ToolWindows.OutputWindow.OutputWindowPanes.Item("Debug").Clear();
System.Runtime.InteropServices.Marshal.ReleaseComObject(ide);
}
This is what I use in VS2013, Win10, x64:
private static void ClearVS2013DebugWindow()
{
// add reference to "C:\Program Files (x86)\Common Files\microsoft shared\MSEnv\PublicAssemblies\envdte.dll"
EnvDTE.DTE ide = (EnvDTE.DTE)System.Runtime.InteropServices.Marshal.GetActiveObject("VisualStudio.DTE.12.0");
if (ide != null)
{
ide.ExecuteCommand("Edit.ClearOutputWindow", "");
System.Runtime.InteropServices.Marshal.ReleaseComObject(ide);
}
}
Credit to the answers above.
Within the underlying APIs, the debug output is a forward only stream - you shouldn't assume that it is only viewable within Visual Studio. Debug output can only be written by an application, and if you need more detailed control, it should be shown within the User Interface of your application (whether that is a console window, which can be cleared; or a WinForms application, or WPF, etc.)

Categories