Untitled

 avatar
user_8783231
plain_text
a year ago
3.5 kB
2
Indexable
Never
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using NuGet.Common;
using NuGet.Commands;
using NuGet.Configuration;
using System.Threading;
using System.Threading.Tasks;
using NuGet.Protocol;
using NuGet.Protocol.Core.Types;
using NuGet.Versioning;
using NuGet.ProjectManagement;
using System.IO;
using NuGet.Packaging;
using System.Diagnostics;
using NuGet.ProjectModel;
using EnvDTE;
using Microsoft.VisualStudio.Shell;
using NuGet.VisualStudio;
using Microsoft.VisualStudio.OLE.Interop;
using System.IO.Packaging;
using EnvDTE80;
using System.ComponentModel.Composition;
using NuGet;
using NuGet.Packaging.Core;
using System.Xml.Linq;



namespace ConsoleApp1
{
    class Program
    {
        static void Main(string[] args)
        {
            DTE2 dte = GetDTE();
            if (dte == null)
            {
                Console.WriteLine("Unable to get DTE object. Please ensure Visual Studio is running.");
                return;
            }
            string command = "Update-Package";
            ThreadHelper.JoinableTaskFactory.Run(async () =>
            {
                await ThreadHelper.JoinableTaskFactory.SwitchToMainThreadAsync();
                try
                {
                    dte.ExecuteCommand("View.PackageManagerConsole");
                    var outputWindow = dte.ToolWindows.OutputWindow;
                    OutputWindowPane packageManagerPane = null;
                    foreach (OutputWindowPane pane in outputWindow.OutputWindowPanes)
                    {
                        if (pane.Name == "Package Manager Console")
                        {
                            packageManagerPane = pane;
                            break;
                        }
                    }
                    if (packageManagerPane != null)
                    {
                        packageManagerPane.Clear();
                        packageManagerPane.Activate();
                        packageManagerPane.OutputString($"Executing command: {command}\n");
                        dte.ExecuteCommand("View.PackageManagerConsole");
                        packageManagerPane.OutputString(command);
                        dte.ExecuteCommand("View.PackageManagerConsole");
                        packageManagerPane.OutputString("\n");
                        dte.ExecuteCommand("View.PackageManagerConsole");
                        packageManagerPane.OutputString("exit");
                        dte.ExecuteCommand("View.PackageManagerConsole");
                    }
                    else
                    {
                        Console.WriteLine("Package Manager pane not found.");
                    }
                }
                catch (Exception ex)
                {
                    Console.WriteLine($"Error executing Package Manager Console command: {ex.Message}");
                }
            });
            Console.ReadKey();
        }
        static DTE2 GetDTE()
        {
            try
            {
                DTE2 dte = (DTE2)System.Runtime.InteropServices.Marshal.GetActiveObject("VisualStudio.DTE.17.0");
                return dte;
            }
            catch (Exception ex)
            {
                Console.WriteLine($"Error retrieving DTE object: {ex.Message}");
                return null;
            }
        }
    }
}