Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/visual-studio-2010/4.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Visual studio VS 2010中是否有允许其在项目文件更改后恢复打开的文件的设置?_Visual Studio_Visual Studio 2010 - Fatal编程技术网

Visual studio VS 2010中是否有允许其在项目文件更改后恢复打开的文件的设置?

Visual studio VS 2010中是否有允许其在项目文件更改后恢复打开的文件的设置?,visual-studio,visual-studio-2010,Visual Studio,Visual Studio 2010,如果我打开了10个文件并修改了我的csproj文件(例如:添加空间),visual studio会抱怨: The project "XYZ" has been modified outside the environment. Press Reload to load the updated project from disk. Press Ignore to ignore the external changes. The change will be used the next time

如果我打开了10个文件并修改了我的
csproj
文件(例如:添加空间),visual studio会抱怨:

The project "XYZ" has been modified outside the environment. Press Reload to load the updated project from disk. Press Ignore to ignore the external changes. The change will be used the next time you open the project. 项目“XYZ”已在环境外部修改。 按“重新加载”从磁盘加载更新的项目。 按Ignore忽略外部更改。下次打开项目时将使用更改。 现在,我真的想重新加载,因为有重要的更改,但我确实希望Visual Studio关闭所有打开的文件,而是希望它刷新仍然存在的文件并关闭丢失的文件


有没有办法获得这种功能?

由于此功能不是由它构建的,因此我编写了以下宏:

Public Sub ReloadProject()
    Dim oldFiles As List(Of String)
    oldFiles = New List(Of String)

    Dim projName = DTE.ActiveDocument.ProjectItem.ContainingProject.Name

    For iDoc = DTE.Documents.Count To 1 Step -1
        Dim name = (DTE.Documents.Item(iDoc).FullName)
        oldFiles.Add(name)
        DTE.Documents.Item(iDoc).Close(vsSaveChanges.vsSaveChangesPrompt)
    Next

    Dim projPath As String = DTE.Solution.Properties.Item("Name").Value.ToString() & "\" & projName

    Dim solutionExplorer As Window = DTE.Windows.Item(Constants.vsWindowKindSolutionExplorer)
    solutionExplorer.Activate()

    Dim solutionHierarchy As UIHierarchy = solutionExplorer.Object
    Dim obj As Object = solutionHierarchy.GetItem(projPath)
    obj.Select(vsUISelectionType.vsUISelectionTypeSelect)

    DTE.ExecuteCommand("Project.UnloadProject")
    DTE.ExecuteCommand("Project.ReloadProject")

    oldFiles.Reverse()
    For Each file In oldFiles
        Dim item = DTE.Solution.FindProjectItem(file)
        If Not item Is Nothing Then
            item.Open()
            item.Document.Activate()
        End If
    Next

End Sub

当我看到恼人的窗口告诉我重新加载时,我忽略了它。然后在重新加载后运行此宏

是的,我编写了一个扩展来解决VS10和VS11中的这个问题:


享受。

由于Visual Studio 2013中无法使用宏,因此您需要一个加载项来运行宏。VisualCommander(可在图库中找到)似乎就是这么做的。我通过VisualCommander调整Sam Saffron的宏,使其在VS 2013中工作

编辑2015年2月:我还改进了它,以记住活动文档的行号和列,并将任何错误记录到Visual Studio宏输出窗口

最新版本如下:


你为什么不在我们的团队聊天中告诉我?你不爱我吗?@Jarrod,我在努力保持我所有的竞争优势,否则我就跟不上你:)这是一个很好的扩展!如果能保存修改堆栈就更好了,这样即使在重新加载后也可以撤消内容。如果能与power tools extension的pin tab功能集成就更好了!
'===============================================================================
' This macro originally written by Sam Saffron, adapted for Visual Studio 2013
'  by Steve Ognibene.  Run in Visual Studio with a macro launcher such as the
'  Visual Commander extension.
' Latest version will be here: https://gist.github.com/nycdotnet/947025d922fa2af87d03
' Original Stack Overflow thread: http://stackoverflow.com/questions/3783648/is-there-a-setting-in-vs-2010-that-will-allow-it-to-recover-open-files-after-a-p/28130299#28130299
'===============================================================================
Imports EnvDTE
Imports EnvDTE80
Imports Microsoft.VisualStudio.Shell
Imports VisualCommanderExt
Imports System
Imports System.Collections.Generic

Public Class C
    Implements ICommand

    Private DTE as DTE2

    Sub Run(InboundDTE As DTE2, package As Package) Implements ICommand.Run
        Me.DTE = InboundDTE
        ReloadProject(DTE)
    End Sub

    Public Sub ReloadProject(DTE As DTE2)
        Try

            Dim oldFiles As New List(Of String)
            Dim iDoc As Object

            If DTE.ActiveDocument Is Nothing Then
                WriteOutput("You must have a document open to reload a project with this macro.")
                Exit Sub
            End If

            Dim projName = DTE.ActiveDocument.ProjectItem.ContainingProject.Name

            Dim activeDocFullName = DTE.ActiveDocument.FullName
            Dim objSel As TextSelection = DTE.ActiveDocument.Selection
            Dim objActive As VirtualPoint = objSel.ActivePoint
            Dim intOriginallyActiveRowNumber As Integer = objActive.Line
            Dim intOriginallyActiveColumnNumber As Integer = objActive.LineCharOffset

            For iDoc = DTE.Documents.Count To 1 Step -1
                Dim name = (DTE.Documents.Item(iDoc).FullName)
                oldFiles.Add(name)
                DTE.Documents.Item(iDoc).Close(vsSaveChanges.vsSaveChangesPrompt)
            Next

            Dim projPath As String = DTE.Solution.Properties.Item("Name").Value.ToString() & "\" & projName

            Dim solutionExplorer As Window = DTE.Windows.Item(Constants.vsWindowKindSolutionExplorer)
            solutionExplorer.Activate()

            Dim solutionHierarchy As UIHierarchy = solutionExplorer.Object
            Dim obj As Object = solutionHierarchy.GetItem(projPath)
            obj.Select(vsUISelectionType.vsUISelectionTypeSelect)

            DTE.ExecuteCommand("Project.UnloadProject")
            DTE.ExecuteCommand("Project.ReloadProject")

            oldFiles.Reverse()

            're-open all previously open files
            For Each file As Object In oldFiles
                Dim item = DTE.Solution.FindProjectItem(file)
                If Not item Is Nothing Then
                    item.Open()
                    item.Document.Activate()
                End If
            Next

            'reactivate previously active file
            For Each file As Object In oldFiles
                If file = activeDocFullName Then
                    Dim item = DTE.Solution.FindProjectItem(file)
                    If Not item Is Nothing Then
                        item.Document.Activate()
                        Dim ts as TextSelection = item.Document.Selection
                        ts.MoveToLineAndOffset(intOriginallyActiveRowNumber,intOriginallyActiveColumnNumber,false)
                    End If
                End If
            Next
        Catch Ex as Exception
            WriteOutput("Exception: " & ex.Message & System.Environment.NewLine & ex.StackTrace)
        End Try

    End Sub

    Private Function GetMacroOutputPane() As OutputWindowPane
        Dim ow As OutputWindow = _
            DTE.Windows.Item(Constants.vsWindowKindOutput).Object()

        Dim outputPane As OutputWindowPane

        Try
            outputPane = ow.OutputWindowPanes.Item("Macros")
        Catch ex As Exception
            outputPane = ow.OutputWindowPanes.Add("Macros")
        End Try

        Return outputPane
    End Function

    Private Sub WriteOutput(ByVal s As String)
        Dim buffer = New System.Text.StringBuilder

        buffer.Append(Date.Now.ToLongTimeString())
        buffer.Append(" ")
        buffer.Append(s)
        buffer.Append(System.Environment.NewLine)

        Dim output As String = buffer.ToString()
        Dim outputPane As OutputWindowPane = GetMacroOutputPane()

        outputPane.OutputString(output)
    End Sub

End Class