/*
s = new FileStream("C: empGoo.txt", FileMode.Create);
or use forward (Unix-style) slashes:
s = new FileStream("C:/temp/Goo.txt", FileMode.Create);
or use the at sign (@), which is a control-character suppressor:
s = new FileStream(@"C: empGoo.txt", FileMode.Create);
*/
File Stream
Deep Copy with MemoryStream
//http://simpledbbrowser.codeplex.com/
//License: Microsoft Public License (Ms-PL)
using System.Diagnostics;
using System.IO;
using System.Runtime.Serialization.Formatters.Binary;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
namespace AWS.Framework.WPF.Utility
{
public sealed class Helpers
{
public static T DeepCopy<T>(object toClone)
{
using (MemoryStream ms = new MemoryStream())
{
BinaryFormatter bf = new BinaryFormatter();
bf.Serialize(ms, toClone);
ms.Position = 0;
return (T)bf.Deserialize(ms);
}
}
}
}
MemoryStream.ToArray, MemoryStream.ReadDecimal
using System;
using System.IO;
class MainClass
{
public static byte[] DecimalToByteArray (decimal src){
using (MemoryStream stream = new MemoryStream()) {
using (BinaryWriter writer = new BinaryWriter(stream)){
writer.Write(src);
return stream.ToArray();
}
}
}
public static decimal ByteArrayToDecimal (byte[] src){
using (MemoryStream stream = new MemoryStream(src)){
using (BinaryReader reader = new BinaryReader(stream)){
return reader.ReadDecimal();
}
}
}
public static void Main()
{
byte[] b = null;
b = BitConverter.GetBytes(true);
Console.WriteLine(BitConverter.ToString(b));
Console.WriteLine(BitConverter.ToBoolean(b, 0));
b = BitConverter.GetBytes(3678);
Console.WriteLine(BitConverter.ToString(b));
Console.WriteLine(BitConverter.ToInt32(b, 0));
b = DecimalToByteArray(285998345545.563846696m);
Console.WriteLine(BitConverter.ToString(b));
Console.WriteLine(ByteArrayToDecimal(b));
}
}
Use MemoryStream and BinaryReader to convert Byte array to decimal
using System;
using System.IO;
class Test {
public static byte[] DecimalToByteArray (decimal src) {
using (MemoryStream stream = new MemoryStream()) {
using (BinaryWriter writer = new BinaryWriter(stream)){
writer.Write(src);
return stream.ToArray();
}
}
}
public static decimal ByteArrayToDecimal (byte[] src) {
using (MemoryStream stream = new MemoryStream(src)) {
using (BinaryReader reader = new BinaryReader(stream)) {
return reader.ReadDecimal();
}
}
}
public static void Main() {
byte[] b = BitConverter.GetBytes(true);
// Convert a decimal to a byte array and display.
b = DecimalToByteArray(12345678987654321.563846696m);
Console.WriteLine(BitConverter.ToString(b));
// Convert a byte array to a decimal and display.
Console.WriteLine(ByteArrayToDecimal(b));
}
}
Use MemoryStream and BinaryWriter to convert decimal to byte array
using System;
using System.IO;
class Test {
// Create a byte array from a decimal.
public static byte[] DecimalToByteArray (decimal src) {
using (MemoryStream stream = new MemoryStream()) {
using (BinaryWriter writer = new BinaryWriter(stream)){
writer.Write(src);
return stream.ToArray();
}
}
}
public static void Main() {
byte[] b = DecimalToByteArray(285998345545.563846696m);
// Convert a decimal to a byte array and display.
Console.WriteLine(BitConverter.ToString(b));
}
}
MemoryStream.Write
using System;
using System.IO;
public class MemTest {
public static void Main() {
MemoryStream memOut = new MemoryStream();
byte[] bs = { 1, 2, 3, 4, 5, 6 };
memOut.Write(bs, 0, bs.Length);
memOut.Seek(+3, SeekOrigin.Begin);
byte b = (byte)memOut.ReadByte();
Console.WriteLine("Value: " + b);
}
}
Create a MemoryStream
using System;
using System.IO;
class MemStreamApp
{
static void Main(string[] args)
{
MemoryStream m = new MemoryStream(64);
Console.WriteLine(“Length: {0} Position: {1} Capacity: {2}”,m.Length, m.Position, m.Capacity);
for (int i = 0; i < 64; i++) { m.WriteByte((byte)i); } Console.WriteLine("Length: {0} Position: {1} Capacity: {2}",m.Length, m.Position, m.Capacity); byte[] ba = m.GetBuffer(); foreach (byte b in ba) { Console.Write("{0,-3}", b); } m.Close(); } } [/csharp]