We have a bunch of SSIS packages that use .dtsxConfig files for various variable transformations, but mainly for the connection strings. While doing some SQL tracing, we noticed that almost all our packages have the same application name since they're mostly copies of each other. To make tracing clearer, I want to replace all the existing ApplicationName values in the config files with the filename.
This worked on two sample files in the test directory (and didn't run on the two files with different extensions so that's good), but because I'm going to run this against production files, I'd like to make sure there's no gaping logic holes or possibility of bad replacements (and code improvements since I'm pretty new)
Sample file below code
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace RenameConfigApplicationNames
{
class Program
{
static void Main(string[] args)
{
string filepath = "C:\\SSIS\\Development\\Configurations";
DirectoryInfo d = new DirectoryInfo(filepath);
foreach (var file in d.GetFiles("*.dtsConfig"))
{
string fName = file.Name.ToString();
string fPath = file.FullName.ToString();
string pureFileName = Path.GetFileNameWithoutExtension(fName);
string fileContent = GetFileContent(fPath, pureFileName);
string newFileName = Path.Combine(file.DirectoryName, pureFileName + "_Temp.txt");
File.WriteAllText(newFileName, fileContent);
file.Delete();
File.Move(newFileName, fPath);
}
}
private static string GetFileContent(string fPath,string fName)
{
string[] fInfo = File.ReadAllLines(fPath);
StringBuilder sb = new StringBuilder(1024);
foreach (string line in fInfo)
{
if (line.Contains(";Application Name="))
{
int start = line.IndexOf(";Application Name=") + 18;
int end = line.IndexOf(";", start + 1);
int len = line.Length - end;
string prefix = line.Substring(0, start);
string suffix = line.Substring(end, len);
string final = prefix + fName + suffix;
sb.AppendLine(final);
}
else
{
sb.AppendLine(line);
}
}
return sb.ToString();
}
}
}
SAMPLE FILE
<?xml version="1.0"?>
<DTSConfiguration>
<DTSConfigurationHeading>
<DTSConfigurationFileInfo GeneratedBy="Domain\User" GeneratedFromPackageName="SomeOldPackage" GeneratedFromPackageID="{big GUID}" GeneratedDate="4/01/2010 11:00:00 AM"/>
</DTSConfigurationHeading>
<Configuration ConfiguredType="Property" Path="\Package.Connections[CN-Destination1].Properties[ConnectionString]" ValueType="String">
<ConfiguredValue>Data Source=SERVERNAME\NAMEDINSTANCE;Initial Catalog=Database;Integrated Security=Maybe;Application Name=SSIS-SomeOldPackage-{another GUID}Servername,Database;</ConfiguredValue>
</Configuration>
<Configuration ConfiguredType="Property" Path="\Package.Connections[CN-Destination2].Properties[ConnectionString]" ValueType="String">
<ConfiguredValue>Data Source=SERVERNAME;Initial Catalog=Database;Integrated Security=Maybe;Application Name=SSIS-SomeOldPackage-{another GUID}Servername,Database;</ConfiguredValue>
</Configuration>
<Configuration ConfiguredType="Property" Path="\Package.Variables[User::EmailDistribution].Properties[Value]" ValueType="String">
<ConfiguredValue>[email protected]</ConfiguredValue>
</Configuration>
</DTSConfiguration>
The only change in the file is the ApplicationName in these two lines:
<ConfiguredValue>Data Source=SERVERNAME\NAMEDINSTANCE;Initial Catalog=Database;Integrated Security=Maybe;Application Name=*FileNameNoExtension;</ConfiguredValue>
</Configuration>
<Configuration ConfiguredType="Property" Path="\Package.Connections[CN-Destination2].Properties[ConnectionString]" ValueType="String">
<ConfiguredValue>Data Source=SERVERNAME;Initial Catalog=Database;Integrated Security=Maybe;Application Name=FileNameNoExtension;</ConfiguredValue>
</Configuration>