Change VS reload files behaviour
Asked Answered
S

3

7

I am having one VSIX project, which will made some changes in Project.json file of ASPNET5 project. am using the following to edit .json file.

ProjectJson jsonObj = JsonConvert.DeserializeObject<ProjectJson>(jsonContents);
jsonObj = JsonConvert.DeserializeObject<ProjectJson>(jsonContents);

var resultJson = JsonConvert.SerializeObject(jsonObj, Formatting.Indented);

JsonSerializer serializer = new JsonSerializer();
using (StreamWriter sw = new StreamWriter(projectObjects.ProjectJsonPath))
{
     var writer = new JsonTextWriter(sw);
     serializer.Serialize(writer, resultJson);
}

// File.WriteAllText(projectObjects.ProjectJsonPath, resultJson);

by using both stream writer and writealltext am getting the following message in ASPNET 5 project

The file has unsaved changes inside this editor and has been changed externally. do you want to reload it?

how to edit .json file without getting the above message?

Southwest answered 15/11, 2015 at 16:44 Comment(1)
Hi I found one way to restrict the message box. from Visual Studio->Tools->options>Documents. I have checked the following check box. reload modified files unless there are unsaved changes. Is there any way to reload a file by code?Southwest
G
3

Its actually the opposite. Since the environment thinks that the file wants to reload with unsaved changes.

You should uncheck the detect file changes. And when you do, it won't detect the external changes and will not warn you, beware though, that if you try to save the file after it has been modified you will lose the external change.(not a problem in your case I guess) and in order to see the changes you will have to close, not save the file and reopen it.

Source : VS2008: Disable asking whether to reload files changed outside the IDE

Disable file change detection

Garrygarson answered 23/11, 2015 at 17:4 Comment(0)
H
1

This is the option you want to check programmatically. I don't know how exactly you can do that but you can find topics about settings at MSDN (Creating an option page and Creating a setting category). Using those topics you can have a sense of how options are created.

Basically what you need to do is to load VS settings file (VS.vssettings) and inject another Xml line. (Have a look at Examining the Settings File section on MSDN)

Option to deselect

Update

To be extremely clear the VS settings file is located under

Documents\Your_VS_Version\Settings\CurrentSettings.vssettings

and you need to load the xml and change 'AutoloadExternalChanges' to value 'true'. Setting section

Hypnosis answered 19/11, 2015 at 7:1 Comment(1)
I have updated my question. Share your thoughts. If you don't know how to do this by code I will provide you an implementation.Hypnosis
K
1

You need to tell the environment to ignore file changes. This can be achieved using the IVsFileChangeEx and IVsDocDataFileChangeControl interfaces.

Here is a utility class (derived from the original Visual Studio 2010 SDK Managed Package Framework sample that you can still find here: http://www.getcodesamples.com/src/8641B4F/98B3955E) that should help:

using (SuspendFileChanges suspend = new SuspendFileChanges(site, filePath))
{
    // do something with files
    suspend.Sync(); // if you optionally want to tell the IDE it has changed
}

The utility class:

public class SuspendFileChanges: IDisposable
{
    private readonly IServiceProvider _serviceProvider;
    private readonly List<string> _urls;
    private readonly IVsDocDataFileChangeControl[] _controls;

    public SuspendFileChanges(IServiceProvider serviceProvider, string url)
        : this(serviceProvider, new string[] { url })
    {
    }

    public SuspendFileChanges(IServiceProvider serviceProvider, params string[] urls)
    {
        if (serviceProvider == null)
            throw new ArgumentNullException("serviceProvider");

        if (urls == null)
            throw new ArgumentNullException("urls");

        _serviceProvider = serviceProvider;
        _urls = new List<string>(urls);
        _controls = new IVsDocDataFileChangeControl[_urls.Count];

        // or use Package.GetGlobalService ...
        IVsRunningDocumentTable rdt = (IVsRunningDocumentTable)serviceProvider.GetService(typeof(SVsRunningDocumentTable));
        IVsFileChangeEx fileChange = (IVsFileChangeEx)serviceProvider.GetService(typeof(SVsFileChangeEx));

        for(int i = 0; i < _urls.Count; i++)
        {
            string url = _urls[i];
            if (url == null)
                continue;

            fileChange.IgnoreFile(0, url, 1);

            IVsHierarchy hierarchy;
            uint itemId;
            uint docCookie;
            IntPtr docData;
            rdt.FindAndLockDocument((uint)_VSRDTFLAGS.RDT_NoLock, url, out hierarchy, out itemId, out docData, out docCookie);
            if (docData != IntPtr.Zero)
            {
                _controls[i] = Marshal.GetObjectForIUnknown(docData) as IVsDocDataFileChangeControl;
                if (_controls[i] != null)
                {
                    _controls[i].IgnoreFileChanges(1);
                }
                Marshal.Release(docData);
            }
        }
    }

    public void Sync()
    {
        IVsFileChangeEx fileChange = (IVsFileChangeEx)_serviceProvider.GetService(typeof(SVsFileChangeEx));
        if (fileChange == null)
            throw new InvalidOperationException();

        foreach (string url in _urls)
        {
            if (url == null)
                continue;

            fileChange.SyncFile(url);
        }
    }

    public void Dispose()
    {
        IVsFileChangeEx fileChange = (IVsFileChangeEx)_serviceProvider.GetService(typeof(SVsFileChangeEx));
        if (fileChange != null)
        {
            foreach (string url in _urls)
            {
                if (url == null)
                    continue;

                fileChange.IgnoreFile(0, url, 0);
            }
        }

        foreach (IVsDocDataFileChangeControl control in _controls)
        {
            if (control != null)
            {
                control.IgnoreFileChanges(0);
            }
        }
    }
}
Kathlenekathlin answered 25/11, 2015 at 6:44 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.