Looks like this file is a binary serialized version of the Microsoft.SqlServer.Management.UserSettings.SqlStudio
class defined in the Microsoft.SqlServer.Management.UserSettings, Version=10.0.0.0, Culture=neutral, PublicKeyToken=89845dcd8080cc91 assembly (located at c:\Program Files\Microsoft SQL Server\100\Tools\Binn\VSShell\Common7\IDE\Microsoft.SqlServer.Management.UserSettings.dll).
With a bit of development skill (Visual Studio or even Powershell) you can deserialize this file into the original class, find the entries you want to remove and re-serialize the file back out.
This should give you the idea (working on a copy of the .bin file)...
var binaryFormatter = new BinaryFormatter();
var inStream = new MemoryStream(File.ReadAllBytes(@"c:\temp\SqlStudio.bin"));
var settings = (SqlStudio) binaryFormatter.Deserialize(inStream);
foreach (var pair in settings.SSMS.ConnectionOptions.ServerTypes)
{
ServerTypeItem serverTypeItem = pair.Value;
List<ServerConnectionItem> toRemove = new List<ServerConnectionItem>();
foreach (ServerConnectionItem server in serverTypeItem.Servers)
{
if (server.Instance != "the server you want to remove")
{
continue;
}
toRemove.Add(server);
}
foreach (ServerConnectionItem serverConnectionItem in toRemove)
{
serverTypeItem.Servers.RemoveItem(serverConnectionItem);
}
}
MemoryStream outStream = new MemoryStream();
binaryFormatter.Serialize(outStream, settings);
byte[] outBytes = new byte[outStream.Length];
outStream.Position = 0;
outStream.Read(outBytes, 0, outBytes.Length);
File.WriteAllBytes(@"c:\temp\SqlStudio.bin", outBytes);
After Adrian's question, I tried this again on a Win7 x64 box using Visual Studio 2010. I found the same error so, after digging a bit I found it took a number of steps to resolve.
- Set the Platform target to 'x86' in the project properties
- add a reference to Microsoft.SqlServer.Management.SDK.SqlStudio (on my box this was at c:\Program Files\Microsoft SQL Server\100\Tools\Binn\VSShell\Common7\IDE\Microsoft.SqlServer.Management.Sdk.SqlStudio.dll)
- add a reference to Microsoft.SqlServer.Management.UserSettings (in the same directory at the previous one)
- perform custom assembly resolution
The custom assembly resolution took a bit of doing since it wasn't obvious (to me, at least) why the CLR wouldn't just resolve the assembly correctly and why Visual Studio wouldn't allow me to add the reference manually. I'm talking about the SqlWorkbench.Interfaces.dll.
The updated code looks like this:
internal class Program
{
static void Main(string[] args)
{
AppDomain.CurrentDomain.AssemblyResolve += CurrentDomain_AssemblyResolve;
var binaryFormatter = new BinaryFormatter();
var inStream = new MemoryStream(File.ReadAllBytes(@"c:\temp\SqlStudio.bin"));
var settings = (SqlStudio) binaryFormatter.Deserialize(inStream);
foreach (var pair in settings.SSMS.ConnectionOptions.ServerTypes)
{
ServerTypeItem serverTypeItem = pair.Value;
List<ServerConnectionItem> toRemove = new List<ServerConnectionItem>();
foreach (ServerConnectionItem server in serverTypeItem.Servers)
{
if (server.Instance != "the server you want to remove")
{
continue;
}
toRemove.Add(server);
}
foreach (ServerConnectionItem serverConnectionItem in toRemove)
{
serverTypeItem.Servers.RemoveItem(serverConnectionItem);
}
}
MemoryStream outStream = new MemoryStream();
binaryFormatter.Serialize(outStream, settings);
byte[] outBytes = new byte[outStream.Length];
outStream.Position = 0;
outStream.Read(outBytes, 0, outBytes.Length);
File.WriteAllBytes(@"c:\temp\SqlStudio.bin", outBytes);
}
private static Assembly CurrentDomain_AssemblyResolve(object sender, ResolveEventArgs args)
{
Debug.WriteLine(args.Name);
if (args.Name.StartsWith("SqlWorkbench.Interfaces"))
{
return Assembly.LoadFrom(@"C:\Program Files (x86)\Microsoft SQL Server\100\Tools\Binn\VSShell\Common7\IDE\SqlWorkbench.Interfaces.dll");
}
return Assembly.Load(args.Name);
}
}
binaryFormatter.Serialize(outStream, settings);
throws an exception:Type 'System.Runtime.Serialization.TypeLoadExceptionHolder' in Assembly 'mscorlib, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089' is not marked as serializable.
Do you know how I can serialize that back to a bin file? – Maloy