//inside the configuration dialog they clicked the "Fix All Relative Paths" button void btnRelativePaths_Click(object sender, EventArgs e) { StringBuilder sb = new StringBuilder(); sb.AppendLine("The current working directory is:"); sb.AppendLine(System.IO.Directory.GetCurrentDirectory()); sb.AppendLine(); sb.AppendLine("The path for the current package is:"); sb.AppendLine(this.pathForPackageForFixButton); sb.AppendLine(); if (string.Compare(System.IO.Path.GetDirectoryName(this.pathForPackageForFixButton), System.IO.Directory.GetCurrentDirectory(), true) != 0) { sb.AppendLine("Since the package is not in the current working directory, no changes to configurations will be made. Please start Visual Studio by double clicking on the .sln file for the project, and make sure the .sln file is in the same directory as the packages."); MessageBox.Show(sb.ToString(), "BIDS Helper - Fix Relative Paths - Problem"); } else { #if DENALI || SQL2014 Cud.Transaction trans = Cud.BeginTransaction(this.packageForFixButton); #endif bool bChanged = false; foreach (Microsoft.SqlServer.Dts.Runtime.Configuration config in this.packageForFixButton.Configurations) { if (config.ConfigurationType == DTSConfigurationType.ConfigFile) { if (string.Compare(System.IO.Path.GetDirectoryName(config.ConfigurationString), System.IO.Directory.GetCurrentDirectory(), true) == 0) { config.ConfigurationString = System.IO.Path.GetFileName(config.ConfigurationString); sb.Append("Configuration ").Append(config.Name).AppendLine(" changed to relative path"); bChanged = true; #if DENALI || SQL2014 trans.ChangeProperty(config, "ConfigurationString"); #endif } } } if (!bChanged) { sb.AppendLine("No configurations were XML configurations with a full hardcoded path to a dtsConfig file in the same directory as the package were found."); } else { //refresh the grid Button btn = (Button)sender; Form form = (Form)btn.Parent; Control packageConfigurationsGridControl1 = form.Controls["packageConfigurationsGridControl1"]; packageConfigurationsGridControl1.GetType().InvokeMember("RefreshConfigurations", System.Reflection.BindingFlags.Public | System.Reflection.BindingFlags.DeclaredOnly | System.Reflection.BindingFlags.Instance | System.Reflection.BindingFlags.InvokeMethod, null, packageConfigurationsGridControl1, new object[] { }); #if DENALI || SQL2014 trans.Commit(); #endif } MessageBox.Show(sb.ToString(), "BIDS Helper - Fix Relative Paths"); } }
public override void Exec() { if (MessageBox.Show("Are you sure you want to change any hardcoded paths pointing to files in the packages directory to relative paths?\r\n\r\nThis command impacts configurations and connection managers.", "BIDS Helper - Fix Relative Paths", MessageBoxButtons.OKCancel) != DialogResult.OK) { return; } try { this.ApplicationObject.StatusBar.Animate(true, vsStatusAnimation.vsStatusAnimationFind); this.ApplicationObject.StatusBar.Text = "Fixing relative paths..."; EnvDTE.Project proj = GetSelectedProjectReference(); Microsoft.DataWarehouse.Interfaces.IConfigurationSettings settings = (Microsoft.DataWarehouse.Interfaces.IConfigurationSettings)((System.IServiceProvider)proj).GetService(typeof(Microsoft.DataWarehouse.Interfaces.IConfigurationSettings)); DataWarehouseProjectManager projectManager = (DataWarehouseProjectManager)settings.GetType().InvokeMember("ProjectManager", System.Reflection.BindingFlags.Public | System.Reflection.BindingFlags.Instance | System.Reflection.BindingFlags.GetProperty | System.Reflection.BindingFlags.FlattenHierarchy, null, settings, null); this.ApplicationObject.ToolWindows.OutputWindow.Parent.SetFocus(); IOutputWindowFactory service = ((System.IServiceProvider)proj).GetService(typeof(IOutputWindowFactory)) as IOutputWindowFactory; IOutputWindow outputWindow = service.GetStandardOutputWindow(StandardOutputWindow.Deploy); outputWindow.Activate(); outputWindow.Clear(); outputWindow.ReportStatusMessage("BIDS Helper is fixing relative paths in all open packages...\r\n"); bool bFoundOpenPackage = false; StringBuilder sb = new StringBuilder(); string sPackageDirectory = string.Empty; foreach (ProjectItem item in proj.ProjectItems) { try { if (!item.get_IsOpen(BIDSViewKinds.Designer)) { continue; } } catch { continue; } if (!bFoundOpenPackage) { bFoundOpenPackage = true; sPackageDirectory = System.IO.Path.GetDirectoryName(item.get_FileNames(0)); outputWindow.ReportStatusMessage("The current working directory is:"); outputWindow.ReportStatusMessage(System.IO.Directory.GetCurrentDirectory()); outputWindow.ReportStatusMessage(string.Empty); outputWindow.ReportStatusMessage("The directory for the packages is:"); outputWindow.ReportStatusMessage(sPackageDirectory); outputWindow.ReportStatusMessage(string.Empty); if (string.Compare(sPackageDirectory, System.IO.Directory.GetCurrentDirectory(), true) != 0) { outputWindow.ReportStatusMessage("PROBLEM:"); outputWindow.ReportStatusMessage("Since the packages are not in the current working directory, no changes to configurations will be made. Please start Visual Studio by double clicking on the .sln file for the project, and make sure the .sln file is in the same directory as the packages."); return; } } Window w = item.Open(BIDSViewKinds.Designer); //opens the designer w.Activate(); IDesignerHost designer = w.Object as IDesignerHost; if (designer == null) { continue; } EditorWindow win = (EditorWindow)designer.GetService(typeof(Microsoft.DataWarehouse.ComponentModel.IComponentNavigator)); Package package = win.PropertiesLinkComponent as Package; if (package == null) { continue; } outputWindow.ReportStatusMessage("Package " + item.Name); #if DENALI || SQL2014 Cud.Transaction trans = Cud.BeginTransaction(package); #endif bool bChanged = false; foreach (Microsoft.SqlServer.Dts.Runtime.Configuration config in package.Configurations) { if (config.ConfigurationType == DTSConfigurationType.ConfigFile) { if (string.Compare(System.IO.Path.GetDirectoryName(config.ConfigurationString), System.IO.Directory.GetCurrentDirectory(), true) == 0) { config.ConfigurationString = System.IO.Path.GetFileName(config.ConfigurationString); outputWindow.ReportStatusMessage(" Configuration " + config.Name + " changed to relative path"); bChanged = true; #if DENALI || SQL2014 trans.ChangeProperty(config, "ConfigurationString"); #endif } } } foreach (ConnectionManager conn in package.Connections) { if (string.IsNullOrEmpty(conn.GetExpression("ConnectionString")) && string.Compare(System.IO.Path.GetDirectoryName(conn.ConnectionString), System.IO.Directory.GetCurrentDirectory(), true) == 0) { conn.ConnectionString = System.IO.Path.GetFileName(conn.ConnectionString); outputWindow.ReportStatusMessage(" Connection " + conn.Name + " changed to relative path"); bChanged = true; #if DENALI || SQL2014 trans.ChangeProperty(conn, "ConnectionString"); #endif } } if (bChanged) { SSISHelpers.MarkPackageDirty(package); } else { outputWindow.ReportStatusMessage(" No changes"); } } if (!bFoundOpenPackage) { outputWindow.ReportStatusMessage("PROBLEM:"); outputWindow.ReportStatusMessage("No packages in this project were open."); return; } outputWindow.Activate(); } catch (Exception ex) { System.Windows.Forms.MessageBox.Show(ex.Message + "\r\n" + ex.StackTrace); } finally { this.ApplicationObject.StatusBar.Animate(false, vsStatusAnimation.vsStatusAnimationFind); this.ApplicationObject.StatusBar.Text = string.Empty; } }