<?xml version="1.0" encoding="utf-8"?>
<CodeSnippets xmlns="http://schemas.microsoft.com/VisualStudio/2005/CodeSnippet">
  <CodeSnippet Format="1.0.0">
    <Header>
      <Title>Storing Multidimensional Arrays in the Registry</Title>
      <Shortcut>StoringMultidimensionalArraysintheRegistry</Shortcut>
      <Description>Storing Multidimensional Arrays in the Registry [C#]</Description>
      <Author>Zepho Zep</Author>
      <HelpUrl>/PreviewSnippet.aspx?SnippetID=e8655b7f-6a29-4358-91e0-2063bd0df14b</HelpUrl>
      <SnippetTypes>
        <SnippetType>SurroundsWith</SnippetType>
      </SnippetTypes>
    </Header>
    <Snippet>
      <Code Language="csharp"><![CDATA[// Multidimensional array:
byte[,] multi = $arrMultiDArray$
/********* SERIALIZE MULTIDIMENSIONAL ARRAY INTO REGISTRY *********/
using (MemoryStream ms = new MemoryStream())
{
    BinaryFormatter bf = new BinaryFormatter();
    // Serialized the multidimensional array here:
    bf.Serialize(ms, multi);
    // Write the serialized data to the registry:
    Microsoft.Win32.Registry.SetValue(
        @$strRegistryKey$,
        $strRegistryKeyVariable$,
        ms.ToArray());
}
/********* DESERIALIZE MULTIDIMENSIONAL ARRAY FROM REGISTRY  *********/
using (MemoryStream ms = new MemoryStream())
{
    BinaryFormatter bf = new BinaryFormatter();
    // Get the serialized binary data from the registry:
    Object objReg = Microsoft.Win32.Registry.GetValue(
        @$strRegistryKey$,
        $strRegistryKeyVariable$,
        null);
    byte[] serialized = (byte[])objReg;
    // Write the contents of the stream to a buffer for reading later:
    ms.Write(serialized, 0, serialized.Length);
    // Since we wrote to the stream, reset the cursor:
    ms.Position = 0;
    // Deserialize the multidimensional array:
    multi = (byte[,])bf.Deserialize(ms); 
    // Now you have your multidimensional array back!
}]]></Code>
    </Snippet>
  </CodeSnippet>
</CodeSnippets>