【问题标题】:Change VS reload files behaviour更改 VS 重新加载文件的行为
【发布时间】:2016-02-16 18:30:29
【问题描述】:

我有一个 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);

通过同时使用流编写器和 writealltext 我在 ASPNET 5 项目中收到以下消息

该文件在此编辑器中未保存且已更改 外部。要重新加载吗?

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

【问题讨论】:

  • 嗨,我找到了一种限制消息框的方法。从 Visual Studio->工具->选项>文档。我已选中以下复选框。除非有未保存的更改,否则重新加载修改过的文件。有没有办法通过代码重新加载文件?

标签: json visual-studio vsix


【解决方案1】:

其实恰恰相反。由于环境认为文件想要重新加载未保存的更改。

您应该取消选中检测文件更改。当您这样做时,它不会检测到外部更改并且不会警告您,但请注意,如果您在修改后尝试保存文件,您将丢失外部更改。(在您的情况下不是问题我猜测)并且为了查看您必须关闭的更改,不保存文件并重新打开它。

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

【讨论】:

    【解决方案2】:

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

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

    更新

    非常清楚VS设置文件位于

    Documents\Your_VS_Version\Settings\CurrentSettings.vssettings

    您需要加载 xml 并将“AutoloadExternalChanges”更改为“true”。

    【讨论】:

    • 我已经更新了我的问题。分享你的意见。如果您不知道如何通过代码执行此操作,我将为您提供一个实现。
    【解决方案3】:

    您需要告诉环境忽略文件更改。这可以使用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);
                }
            }
        }
    }
    

    【讨论】:

      猜你喜欢
      • 2017-08-01
      • 2013-05-13
      • 1970-01-01
      • 2018-09-04
      • 2012-11-28
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2014-04-26
      相关资源
      最近更新 更多