/// <summary> /// Create a Visual Studio process. /// </summary> /// <param name="info">Startup information.</param> private void StartNewInstance(VsIdeStartupInfo startupInfo) { Contract.Assert(startupInfo != null); Contract.Assert(_process == null, "VisualStudioIde.StartNewInstance: _process should be null!"); Process process = new Process(); process.StartInfo.UseShellExecute = false; if (startupInfo.WorkingDirectory != null) { process.StartInfo.WorkingDirectory = startupInfo.WorkingDirectory; } // Note that this needs to be partial (not $-terminated) as we partially match/replace. Regex versionRegex = new Regex(@"^[0-9]+\.[0-9]+"); string hiveVersion = versionRegex.Match(startupInfo.RegistryHive).Value; string hiveSuffix = versionRegex.Replace(startupInfo.RegistryHive, string.Empty); if (!string.IsNullOrEmpty(hiveSuffix)) { process.StartInfo.Arguments = "/RootSuffix " + hiveSuffix + " /Log"; } else { process.StartInfo.Arguments = "/Log"; } process.StartInfo.FileName = VsRegistry.GetVsLocation(hiveVersion); Contract.Assert(!string.IsNullOrEmpty(process.StartInfo.FileName)); // Prevent the settings popup on startup if (!VsRegistry.UserSettingsArchiveExists(startupInfo.RegistryHive)) { ResetVSSettings(process.StartInfo.FileName, hiveSuffix); if (!VsRegistry.UserSettingsArchiveExists(startupInfo.RegistryHive)) { throw new VsIdeTestHostException("Unable to reset VS settings."); } } process.Exited += new EventHandler(ProcessExited); process.EnableRaisingEvents = true; if (!process.Start()) { throw new VsIdeTestHostException("Failed to start Visual Studio process."); } _process = process; string progId = string.Format(CultureInfo.InvariantCulture, "{0}.{1}", VisualStudioIde.BaseProgId, hiveVersion); _dte = GetDteFromRot(progId, _process.Id); if (_dte == null) { throw new VsIdeTestHostException("Failed to get the DTE object from Visual Studio process. Please make sure that the Add-in is registered in Tools->Add-in Manager to load in when Visual Studio starts."); } }
/// <summary> /// Constructor. Starts new instance of VS IDE. /// </summary> public VisualStudioIde(VsIdeStartupInfo info) { Contract.Assert(info != null); if (string.IsNullOrEmpty(info.RegistryHive)) { info.RegistryHive = VsRegistry.GetDefaultVersion(); if (string.IsNullOrEmpty(info.RegistryHive)) { throw new VsIdeTestHostException(string.Format(CultureInfo.InvariantCulture, "Cannot find installation of Visual Studio in '{0}' registry hive.", info.RegistryHive)); } } StartNewInstance(info); }
/// <summary> /// Create a Visual Studio process. /// </summary> /// <param name="info">Startup information.</param> private void StartNewInstance(VsIdeStartupInfo startupInfo) { Contract.Assert(startupInfo != null); Contract.Assert(_process == null, "VisualStudioIde.StartNewInstance: _process should be null!"); Process process = new Process(); process.StartInfo.UseShellExecute = false; if (startupInfo.WorkingDirectory != null) { process.StartInfo.WorkingDirectory = startupInfo.WorkingDirectory; } // Note that this needs to be partial (not $-terminated) as we partially match/replace. Regex versionRegex = new Regex(@"^[0-9]+\.[0-9]+"); string hiveVersion = versionRegex.Match(startupInfo.RegistryHive).Value; string hiveSuffix = versionRegex.Replace(startupInfo.RegistryHive, string.Empty); if (!string.IsNullOrEmpty(hiveSuffix)) { process.StartInfo.Arguments = "/RootSuffix " + hiveSuffix + " /Log"; } else { process.StartInfo.Arguments = "/Log"; } process.StartInfo.FileName = VsRegistry.GetVsLocation(hiveVersion); Contract.Assert(!string.IsNullOrEmpty(process.StartInfo.FileName)); // Prevent the settings popup on startup if (!VsRegistry.UserSettingsArchiveExists(startupInfo.RegistryHive)) { ResetVSSettings(process.StartInfo.FileName, hiveSuffix); if (!VsRegistry.UserSettingsArchiveExists(startupInfo.RegistryHive)) { throw new VsIdeTestHostException("Unable to reset VS settings."); } } process.Exited += new EventHandler(ProcessExited); process.EnableRaisingEvents = true; if (!process.Start()) { throw new VsIdeTestHostException("Failed to start Visual Studio process."); } _process = process; string progId = string.Format(CultureInfo.InvariantCulture, "{0}.{1}", VisualStudioIde.BaseProgId, hiveVersion); _dte = GetDteFromRot(progId, _process.Id); if (_dte == null) { throw new VsIdeTestHostException("Failed to get the DTE object from Visual Studio process. Please make sure that the Add-in is registered in Tools->Add-in Manager to load in when Visual Studio starts."); } }
/// <summary> /// Constructor. Starts new instance of VS IDE. /// </summary> public VisualStudioIde(VsIdeStartupInfo info) { Contract.Assert(info != null); if (string.IsNullOrEmpty(info.RegistryHive)) { info.RegistryHive = VsRegistry.GetDefaultVersion(); if (string.IsNullOrEmpty(info.RegistryHive)) { throw new VsIdeTestHostException(string.Format(CultureInfo.InvariantCulture, "Cannot find installation of Visual Studio in '{0}' registry hive.", info.RegistryHive)); } } StartNewInstance(info); }