20

I'm working with CSS, HTML, and JavaScript in my classes, and I've recently started working with Visual Studio 2015. In Notepad++, there's an add-on that allowed me to automatically save all my opened documents as soon as it lost focus. This saved me a lot of time, as I go back and forth frequently between the page I'm working on and the coding for it.

I'm hoping there is a similar add-on for Visual Studio 2015, so I can stop forgetting to save before checking my work on its web page! Anyone know of any?

Alan Nelson
  • 1,129
  • 2
  • 11
  • 27

3 Answers3

41

You can use the following extension for Visual Commander to auto save files on switching away from Visual Studio:

public class E : VisualCommanderExt.IExtension
{
    public void SetSite(EnvDTE80.DTE2 DTE_, Microsoft.VisualStudio.Shell.Package package)
    {
        DTE = DTE_;
        System.Windows.Application.Current.Deactivated += OnDeactivated;
    }

    public void Close()
    {
        System.Windows.Application.Current.Deactivated -= OnDeactivated;
    }

    private void OnDeactivated(object sender, System.EventArgs e)
    {
        try
        {
            DTE.ExecuteCommand("File.SaveAll");
        }
        catch (System.Exception ex)
        {
        }
    }

    private EnvDTE80.DTE2 DTE;
}
Sergey Vlasov
  • 26,641
  • 3
  • 64
  • 66
  • 25
    I got it to work thankyou! Slightly more detailed instructions for others: 1. Download and install visual commander. 2. Click "VCmd" tab in the top of visual studio. 3. Extensions... 4. Add 5. LANGUAGE >> C# (This one took me a while lol) 6. Paste above code over entire document. 7. Compile >> Save >> Install 8. It works! Thanks ;) – Alan Nelson Nov 11 '15 at 00:27
  • Thanks for your help, but I want to lost focus visual studio itself. Could you please give me the correct code with lost focus event? – Trung Jun 05 '17 at 05:05
  • @Trung Sorry, I don't understand what lost focus event are you referring to. – Sergey Vlasov Jun 07 '17 at 03:58
  • @Sergey Vlasov: Above code help auto file save, when you focus out of the Visual Studio scope, that likes you must focus to other application (Chrome, Skype, Excel, ...) then the file in Visual Studio will be saved. But I need an event that help auto save when I focus to another tab in Visual Studio itself. Example: You have opened two tabs with **TabA** and **TabB**, then you edit **TabA** and you focus to **TabB** and then **TabA** must be saved. Thanks, – Trung Jun 22 '17 at 18:12
  • 2
    @Trung Use DTE.Events.WindowEvents.WindowActivated https://vlasovstudio.com/visual-commander/extensions.html#StatusBar – Sergey Vlasov Jun 23 '17 at 03:37
  • 7
    Why is this not an option within VS?! It should be the default behaviour IMO. – Will Calderwood Feb 28 '18 at 15:45
  • anybody know how to change this code to make it save changes all the time without delay and without loose focus/switch away ? – Adel Mourad Jun 10 '18 at 13:07
4

The solution above with

DTE.ExecuteCommand("File.SaveAll");

is too slow in case of 1000+ projects in solution, MSVS UI hangs for several seconds on each losing focus event and extremely consumes CPU, even if there are no unsaved changes.

I've edited OnDeactivated() method, and it works much faster in my cases:

private void OnDeactivated(object sender, System.EventArgs e)
{
    try
    {
        EnvDTE.Documents docs = DTE.Documents;

        for (int i = 1; i <= docs.Count; i++) {
            EnvDTE.Document doc = docs.Item(i);
            if (!doc.Saved) {
                doc.Save();
            }
        }
    }
    catch (System.Exception ex)
    {
    }
}
DmitriyH
  • 420
  • 4
  • 15
1

There is an extension that does this, "Auto Save File." It saves when Visual Studio loses focus, the file loses focus, or a set amount of time has passed since the last edit. The default is 5 seconds.

https://marketplace.visualstudio.com/items?itemName=HRai.AutoSaveFile

kindrobot
  • 1,309
  • 1
  • 10
  • 19