更改VS重新加载文件的行为

时间:2015-11-15 16:44:11

标签: json visual-studio vsix

我有一个VSIX项目,它将在ASPNET5项目的Project.json文件中进行一些更改。我正在使用以下内容编辑.json文件。

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);

通过使用stream writer和writealltext在ASPNET 5项目中获取以下消息

  

该文件在此编辑器中有未保存的更改,并且已更改   外部。你想重装吗?

如何编辑.json文件而不收到上述消息?

2 个答案:

答案 0 :(得分:1)

这是您要以编程方式检查的选项。我不知道您究竟能做到这一点,但您可以在MSDN(Creating an option pageCreating a setting category)找到有关设置的主题。使用这些主题,您可以了解如何创建选项。

基本上你需要做的是加载VS设置文件(VS.vssettings)并注入另一个Xml行。看看MSDN上的检查设置文件部分

Option to deselect

<强> 更新

非常清楚VS设置文件位于

<强>文档\ Your_VS_Version \设置\ CurrentSettings.vssettings

您需要加载xml并将' AutoloadExternalChanges '更改为值' true '。 Setting section

答案 1 :(得分:1)

您需要告诉环境忽略文件更改。这可以使用IVsFileChangeExIVsDocDataFileChangeControl接口来实现。

这是一个实用程序类(源自您仍可在此处找到的原始Visual Studio 2010 SDK托管包框架示例:http://www.getcodesamples.com/src/8641B4F/98B3955E),应该有所帮助:

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

实用程序类:

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);
            }
        }
    }
}