Posts mit dem Label Visual Studio werden angezeigt. Alle Posts anzeigen
Posts mit dem Label Visual Studio werden angezeigt. Alle Posts anzeigen

Dienstag, 11. April 2006

Binary Serialization Code Snippet

In addtition to the wonderful new Code Snippet Functionality in Visual Studio 2005, i provide a binary serialization code snippet for C#. This is only to save to a binary File.

The Code Snippets from Microsoft, available here, have only the Xml Serialization.

Just put it in your #PROGRAMFILES#\Microsoft Visual Studio 8\VC#\Snippets\1033\Visual C# Directory.

Have phun,

tingle

<?xml version="1.0" encoding="UTF-8"?>
<CodeSnippets xmlns="
http://schemas.microsoft.com/VisualStudio/2005/CodeSnippet">
<CodeSnippet Format="1.0.0">
<Header>
<Title>Write Class Data to a Binary File</Title>
<Author>Joerg Brunke</Author>
<Description>Writes the data from a class to a Binary file using the BinaryFormatter class.</Description>
<Shortcut>binWriteClass</Shortcut>
<SnippetTypes>
<SnippetType>Expansion</SnippetType>
</SnippetTypes>
</Header>
<Snippet>
<Declarations>
<Literal>
<ID>filename</ID>
<ToolTip>Replace with name of the file to write the data to.</ToolTip>
<Default>Data.bin</Default>
</Literal>
<Literal>
<ID>dataInstance</ID>
<ToolTip>Replace with the instance of the class containing the data.</ToolTip>
<Default>dataToWrite</Default>
</Literal>
</Declarations>
<Code Language="csharp">
<![CDATA[using(System.IO.FileStream fs = new System.IO.FileStream(@"$filename$", System.IO.FileMode.OpenOrCreate, System.IO.FileAccess.Write))
{
try
{
System.Runtime.Serialization.Formatters.Binary.BinaryFormatter bf = new System.Runtime.Serialization.Formatters.Binary.BinaryFormatter();
bf.Serialize(fs, $dataInstance$);
}
catch(System.Exception ex)
{
}
}$end$]]>
</Code>
</Snippet>
</CodeSnippet>
</CodeSnippets>