I'm writing a VS Package where I need to store the time when the user start my package, and when the user close the Visual Studio.
The problem is, I don't know how to get the closing event for the Visual Studio. Can anyone give me any information about how to detect if the VS is closing?
Note: When I search from the internet, I got the following similar problem to mine: How do you cancel a ToolWindowPane or Visual Studio IDE close operation via a VSPackage?, but when I try it, this solution is to detect and do something when the Package window is closed, and cannot detecting when the Visual Studio is closed.
Any help is really appreciated.
Thanks
Just to make it explicit and close this problem.
This is the snapshot of code to check if VS is closing:
// Create 2 variables below
private DTE2 m_applicationObject = null;
DTEEvents m_packageDTEEvents = null;
Then in Initialize add this:
// Link the Event when VS CLOSING
m_packageDTEEvents = ApplicationObject.Events.DTEEvents;
m_packageDTEEvents.OnBeginShutdown += new _dispDTEEvents_OnBeginShutdownEventHandler(HandleVisualStudioShutDown);
Two other methods that you need:
public DTE2 ApplicationObject
{
get
{
if (m_applicationObject == null)
{
// Get an instance of the currently running Visual Studio IDE
DTE dte = (DTE)GetService(typeof(DTE));
m_applicationObject = dte as DTE2;
}
return m_applicationObject;
}
}
And
public void HandleVisualStudioShutDown()
{
MessageBox.Show("Exiting Visual Studio. Bye");
}
Related
This is a cosmetic issue but my application has a default icon on the control panel. Many other applications have custom icons.
My application does have custom icons for the menu and task bar.
How can the icon displayed on the Control Panel be changed using Visual Studio 2015 or later?
Update:
There has been a change in how Visual Studio creates installers. I'm not sure when it occurred, but 2015 definitely does not have a "deployment project". The majority of the hits on Google suggest going to the deployment project properties which does not exist under VS 2015 apps.
This was why I included the tag for visual-studio-2015. Sorry, not to have mentioned that in the original question. It would have been good information.
Using the registry is a possibility but the registry path mentioned, HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\Windows\CurrentVersion\Uninstall, does not exist. It does sound kludgy to have the application check it's own icon in the registry all the time. It sounds like an installer function to me.
A post on the Microsoft Developer Network provided an answer. It also modifies the registry. I enhanced it by removing hard-coded values for the application name and the icon file.
// These references are needed:
// using System.Reflection;
// using System.Deployment.Application;
// using System.IO;
// using Microsoft.Win32;
private static void SetAddRemoveProgramsIcon(string iconName)
{
// only run if deployed
if (System.Deployment.Application.ApplicationDeployment.IsNetworkDeployed && ApplicationDeployment.CurrentDeployment.IsFirstRun)
{
try
{
string assemblyTitle="";
object[] titleAttributes = Assembly.GetEntryAssembly().GetCustomAttributes(typeof(AssemblyTitleAttribute), true);
if (titleAttributes.Length > 0 && titleAttributes[0] is AssemblyTitleAttribute)
{
assemblyTitle = (titleAttributes[0] as AssemblyTitleAttribute).Title;
}
string iconSourcePath = Path.Combine(System.Windows.Forms.Application.StartupPath, iconName);
if (!File.Exists(iconSourcePath))
{
return;
}
RegistryKey myUninstallKey = Registry.CurrentUser.OpenSubKey(#"Software\Microsoft\Windows\CurrentVersion\Uninstall");
string[] mySubKeyNames = myUninstallKey.GetSubKeyNames();
for (int i = 0; i < mySubKeyNames.Length; i++)
{
RegistryKey myKey = myUninstallKey.OpenSubKey(mySubKeyNames[i], true);
object myValue = myKey.GetValue("DisplayName");
if (myValue != null && myValue.ToString() == assemblyTitle)
{
myKey.SetValue("DisplayIcon", iconSourcePath);
break;
}
}
}
catch (Exception) { }
}
return;
}
The original article by Robin Shahan is here: RobinDotNet
For WPF application we need to replace the following code
string iconSourcePath = Path.Combine(System.Windows.Forms.Application.StartupPath, iconName);
Replace with below code
string iconSourcePath = Path.Combine(System.Environment.GetFolderPath(Environment.SpecialFolder.Startup), "TestIcon.ico");
I know you want 2015 but others may be looking for this in newer versions, like I was.
In Visual Studio 2019 Community we can go to the properties panel for the main setup project and the top property is AddRemoveProgramsIcon.
I have just come through this case today. I know it is old but will be useful for new seekers. To expose icon in Control Panel do the following:
Make a folder in [solution Folder][Project Folder]\bin\debug\images
Copy your icon in the new folder
In Set Up project always refer to the icon in the new created folder.
Solved my problem easily
I have developed a COM+ Component in C# be inheriting ServicedComponent.
Here is how it looks like:
[Transaction(TransactionOption.Required)]
[ClassInterface(ClassInterfaceType.AutoDual)]
[EventTrackingEnabledAttribute(true)]
[JustInTimeActivation]
[ObjectPooling(Enabled = true, MinPoolSize = 10, MaxPoolSize = 30, CreationTimeout = 15000)]
[Synchronization]
class MyComponent: System.EnterpriseServices.ServicedComponent
{
[AutoComplete(true)]
public string getHello()
{//2nd breakpoint
ContextUtil.SetComplete();
return "HelloWorld";
}
}
I have another test project from which I call this component.
class Program
{
static void Main(string[] args)
{
MyComponent myComp = new MyComponent();
myComp.getHello();//1st Breakpoint
}
}
I am not able to reach 2nd Breakpoint. This was working before I switched to VS 2012. Strange thing is after switching to 2012 its no longer working in VS 2010 too.
I've already tried,
Attach to process
Unchecked "Enable Just My Code" in debug settings
Can someone please give direction from here?
UPDATE 1
From the links given by Mike, I tried symchk for my DLL in the same folder where DLL and PDB files were there. It fails with error saying PDB mismatched or not found. I don't know how to resolve this error.
You may be missing the .pdb file in your project.
Check this microsoft link out for an explanation: https://msdn.microsoft.com/en-us/library/yd4f8bd1(vs.71).aspx
I am trying to include a custom language support for Visual Studio.
To start with, I need to have GoToDefinition support. And I am struggling to get the context menu to include this command.
I have defined a ContentTypeDefinition and have included the FileExtensionToContentTypeDefinition such as:
internal sealed class GaugeFileContentType
{
[Export]
[Name("Gauge")]
[BaseDefinition("code")]
internal static ContentTypeDefinition GaugeContentTypeDefinition = null;
[Export]
[FileExtension(".spec")]
[ContentType("Gauge")]
internal static FileExtensionToContentTypeDefinition GaugeFileExtensionDefinition = null;
}
Now, despite this, on debugging, I see that DTE.ActiveDocument.Type is text, despite me adding the [BaseDefinition('code')] attribute. What am I missing here?
Are the above definitions enough to tell Visual Studio to bring up Context menu for code?
I am using Visual Studio 2013 Ultimate.
After a few days of head banging, I managed to figure out a way.
I was using the Experimental Instance for debugging, and it did not clean and reinstall the extension, and thus Visual Studio continued to treat the ContentType as 'Plain Text', since that was what I had originally.
When I build a VSIX and installed, opened the same file in a new instance of Visual Studio, it brought up the right context menu.
However, it brought out more than what I wanted (i.e Run Unit Tests from Resharper). So I did some more digging up.
In order to ensure that Visual Studio can handle a command, it checks for it by calling IOleCommandTarget.QueryStatus method.
All I had to do was set the CommandFlag as (uint)OLECMDF.OLECMDF_ENABLED | (uint)OLECMDF.OLECMDF_SUPPORTED and return VSConstants.S_OK when the cmdId is VSConstants.VSStd97CmdID.GotoDefn.
The final method looks like this:
public int QueryStatus(ref Guid pguidCmdGroup, uint cCmds, OLECMD[] prgCmds, IntPtr pCmdText)
{
if ((VSConstants.VSStd97CmdID)prgCmds[0].cmdID == VSConstants.VSStd97CmdID.GotoDefn)
{
prgCmds[0].cmdf = (uint)OLECMDF.OLECMDF_ENABLED | (uint)OLECMDF.OLECMDF_SUPPORTED;
return VSConstants.S_OK;
}
return Next.QueryStatus(pguidCmdGroup, cCmds, prgCmds, pCmdText);
}
I have a c# winform program which is translated to both Spanish and Portuguese. When debugging the program itself translates to the current windows chosen culture, and I also have a button to manually translate the program:
private void SetLanguage(string cul)
{
Thread.CurrentThread.CurrentUICulture = new CultureInfo(cul);
foreach (System.Windows.Forms.Control control in Controls)
{
var resources = new ComponentResourceManager(typeof(FrmLogin));
resources.ApplyResources(control, control.Name, new CultureInfo(cul));
}
}
but when I install said application (with the Setup solution in visual studio) nothing works. The program loads default language (Spanish) and the button to manually translate doesn't work.
I'm using visual studio 2010. "Localizable" property is set to true.
Thanks!
I've been experiemnting with the community version of ILNumerics 3.2.1.0 with .Net 4.0 in Visual Studio 2010 pro on Windows 7, and going through the documentation I succesfully get a windows form project to display a chart, using the code below.
public partial class Form2 : Form
{
public Form2()
{
InitializeComponent();
}
private void ilPanel1_Load(object sender, EventArgs e)
{
ILSurface mySurface = new ILSurface(ILSpecialData.sincf(100, 200));
ILPlotCube myCube = new ILPlotCube(twoDMode: false);
myCube.Add(mySurface);
ilPanel1.Scene.Add(myCube);
}
}
If I try exactly the same code but from inside a VSTO Excel 2010 application all that is displayed in the form is the designer view of the ILPanel, blue circle on white background. I don't get any error messages. Am I missing something obvious? or does anyone have a solution of how to get the chart to display in VSTO?
Update
Thanks to Philliproso for pointing out the IsDesignMode() method. As pointed out in various places, including this question, Detecting design mode from a Control's constructor , the following method is not ideal, but for me is has provided a quick fix to allow me to evaluate ILNumerics.
public static bool IsDesignMode() {
if (System.Windows.Forms.Application.ExecutablePath.IndexOf("devenv.exe", StringComparison.OrdinalIgnoreCase) > -1)
{
return true;
}
return false;
}
This is the same issue as here:
Ilnumerics Ilpanel not activating when compiled in a winform into a dll when loaded into matlab
in-a-winform-into-a-dll-when-loa
Using VSTO as host for ILNumerics lets the panels assume, it was loaded in a designer. We are currently collecting possible workarounds and solutions. One solution might be to introduce a flag in the Settings of ILNumerics:
Hosted [default: false]
Your situation would require the flag to be enabled. In hosted mode, a blacklist of common designers could be checked at runtime and compared to the current entry assembly. Any other suggestions?