Beispiel #1
0
        public static bool LaunchVisualStudio(string Arguments, out string ErrorMessage)
        {
            ErrorMessage = "";
            VisualStudioInstallation Install = VisualStudioInstallations.GetPreferredInstallation();

            if (Install == null)
            {
                ErrorMessage = string.Format("Unable to get Visual Studio installation");
                return(false);
            }
            try
            {
                System.Diagnostics.Process VSProcess = new System.Diagnostics.Process {
                    StartInfo = new ProcessStartInfo(Install.DevEnvPath, Arguments)
                };
                VSProcess.Start();
                VSProcess.WaitForInputIdle();
            }
            catch (Exception Ex)
            {
                ErrorMessage = Ex.Message;
                return(false);
            }

            return(true);
        }
Beispiel #2
0
        public static VisualStudioInstallation GetPreferredInstallation(int MajorVersion = 0)
        {
            if (CachedInstalls.Count == 0)
            {
                return(null);
            }

            if (MajorVersion == 0)
            {
                return(CachedInstalls.First());
            }

            VisualStudioInstallation Installation = CachedInstalls.FirstOrDefault(Install => { return(Install.MajorVersion == MajorVersion); });

            return(Installation);
        }
Beispiel #3
0
        static List <VisualStudioInstallation> GetVisualStudioInstallations()
        {
            CachedInstalls.Clear();

            try
            {
                SetupConfiguration  Setup      = new SetupConfiguration();
                IEnumSetupInstances Enumerator = Setup.EnumAllInstances();

                ISetupInstance[] Instances = new ISetupInstance[1];
                for (; ;)
                {
                    int NumFetched;
                    Enumerator.Next(1, Instances, out NumFetched);

                    if (NumFetched == 0)
                    {
                        break;
                    }

                    ISetupInstance2 Instance = (ISetupInstance2)Instances[0];
                    if ((Instance.GetState() & InstanceState.Local) == InstanceState.Local)
                    {
                        string   VersionString = Instance.GetInstallationVersion();
                        string[] Components    = VersionString.Split('.');

                        if (Components.Length == 0)
                        {
                            continue;
                        }

                        int    MajorVersion;
                        string InstallationPath = Instance.GetInstallationPath();
                        string DevEnvPath       = Path.Combine(InstallationPath, "Common7\\IDE\\devenv.exe");

                        if (!int.TryParse(Components[0], out MajorVersion) || (MajorVersion != 15 && MajorVersion != 16))
                        {
                            continue;
                        }


                        if (!File.Exists(DevEnvPath))
                        {
                            continue;
                        }

                        VisualStudioInstallation Installation = new VisualStudioInstallation()
                        {
                            BaseDir = InstallationPath, DevEnvPath = DevEnvPath, MajorVersion = MajorVersion, ROTMoniker = string.Format("!VisualStudio.DTE.{0}.0", MajorVersion)
                        };

                        CachedInstalls.Add(Installation);
                    }
                }
            }
            catch (Exception Ex)
            {
                MessageBox.Show(string.Format("Exception while finding Visual Studio installations {0}", Ex.Message));
            }

            // prefer newer versions
            CachedInstalls.Sort((A, B) => { return(-A.MajorVersion.CompareTo(B.MajorVersion)); });

            return(CachedInstalls);
        }