Microsoft.VsSDK.IntegrationTestLibrary.TestUtils.WriteEmbeddedResourceToBinaryFile C# (CSharp) Method

WriteEmbeddedResourceToBinaryFile() public static method

Writes an embedded resource to a file.
public static WriteEmbeddedResourceToBinaryFile ( Assembly assembly, string embeddedResourceName, string fileName ) : void
assembly System.Reflection.Assembly The name of the assembly that the embedded resource is defined.
embeddedResourceName string The name of the embedded resource.
fileName string The file to write the embedded resource's content.
return void
        public static void WriteEmbeddedResourceToBinaryFile(Assembly assembly, string embeddedResourceName, string fileName)
        {
            // Get file contents
            Stream stream = assembly.GetManifestResourceStream(embeddedResourceName);
            if (stream == null)
                throw new InvalidOperationException("Failed to get embedded resource '" + embeddedResourceName + "' from assembly '" + assembly.FullName);

            // Write to file
            BinaryWriter sw = null;
            FileStream fs = null;
            try
            {
                byte[] fileContentsAsBytes = new byte[stream.Length];
                stream.Read(fileContentsAsBytes, 0, (int)stream.Length);

                FileMode mode = FileMode.CreateNew;
                if (File.Exists(fileName))
                {
                    mode = FileMode.Truncate;
                }

                fs = new FileStream(fileName, mode);

                sw = new BinaryWriter(fs);
                sw.Write(fileContentsAsBytes);
            }
            finally
            {
                if (fs != null)
                {
                    fs.Close();
                }
                if (sw != null)
                {
                    sw.Close();
                }
            }
        }