首先有
byte[] buffer = new byte[LENGTH];
MemoryStream memoryStream = new MemoryStream(buffer);
在您的示例中,您可以使用
DXGridControl_Table.SaveLayoutToStream(_userLayout);
byte[] doSomethingwithyourData = _userLayout.GetBuffer();
var length = _userLayout.Length;
使用该信息,您可以将二进制数据写入任何内容。
请注意,缓冲区包含可能未使用的已分配字节。例如,如果将字符串“test”写入 MemoryStream 对象,则从 GetBuffer 返回的缓冲区长度为 256,而不是 4,其中 252 个字节未使用。要仅获取缓冲区中的数据,请使用 ToArray 方法;但是,ToArray 会在内存中创建数据的副本。
或者
Binary binary = new Binary(_userLayout.ToArray());
就像在另一个答案中所说的那样,二进制文件有一个隐式转换:
public static implicit operator Binary(byte[] value) {
return new Binary(value);
}
你要求的例子。关于用法的一个小例子:
namespace Stackoverflow.Hannish.SaveLayout
{
using System;
using System.Collections.Generic;
using System.IO;
using System.Text;
using System.Windows.Forms;
public partial class Form1 : Form
{
/// <summary>
/// Here we store the layout data as a string. This is the data, that
/// gets saved to disk / database / etc.
/// </summary>
private string layoutdata = string.Empty;
public Form1()
{
this.InitializeComponent();
}
private void Form1_Load(object sender, EventArgs e)
{
// Just some FooBar data.
var data = new List<DataValue>
{
new DataValue { Id = 1, Name = "Xyz", IsCool = true },
new DataValue { Id = 2, Name = "Abc", IsCool = false }
};
this.gridControl1.DataSource = data;
}
private void bnLoadLayout_Click(object sender, EventArgs e)
{
using (var stream = new MemoryStream())
{
var strdata = Encoding.Default.GetBytes(this.layoutdata);
stream.Write(strdata, 0, strdata.Length);
stream.Seek(0, SeekOrigin.Begin);
this.gridView1.RestoreLayoutFromStream(stream);
}
}
private void bnSaveLayout_Click(object sender, EventArgs e)
{
using (var stream = new MemoryStream())
{
this.gridView1.SaveLayoutToStream(stream);
this.layoutdata = Encoding.Default.GetString(stream.ToArray());
}
}
}
}
还有一些字节到文件魔术:
private void bnLoadBinLayout_Click(object sender, EventArgs e)
{
using (FileStream fstream = File.Open("Layoutdata.bin", FileMode.Open))
{
int length = (int)fstream.Length;
byte[] buffer = new byte[length];
fstream.Read(buffer, 0, length);
var memstream = new MemoryStream(buffer);
this.gridView1.RestoreLayoutFromStream(memstream);
}
}
private void bnSaveBinLayout_Click(object sender, EventArgs e)
{
using (FileStream fstream = File.Create("Layoutdata.bin"))
{
var memstream = new MemoryStream();
this.gridView1.SaveLayoutToStream(memstream);
fstream.Write(memstream.GetBuffer(), 0, (int)memstream.Length);
}
}
......就像一个例子。DevExpress GridView 可以使用 SaveLayoutToXml() 保存布局本身;