我原来是用VB写程序,会用到如下形式的数据结构体存储数据public type person
number as long
name as string*10
address as string*20
end type其C#代码对应为
public struct Person
{
public int number;
public string Address; //C#里面貌似没有定长字符串?
public string Name; }现在,我改用C#,发现C#里面没有读取结构体并自动赋值的类库,因为数据量不大,所以不想用数据库,请问C#里面有没有针对记录体进行读写操作的类呢?? 还有C#里面有定长字符串的概念吗??
number as long
name as string*10
address as string*20
end type其C#代码对应为
public struct Person
{
public int number;
public string Address; //C#里面貌似没有定长字符串?
public string Name; }现在,我改用C#,发现C#里面没有读取结构体并自动赋值的类库,因为数据量不大,所以不想用数据库,请问C#里面有没有针对记录体进行读写操作的类呢?? 还有C#里面有定长字符串的概念吗??
public struct Person
{
public int number;
[MarshalAs(UnmanagedType.ByValTStr, SizeConst = 10)]
public string Address; //C#里面貌似没有定长字符串?
[MarshalAs(UnmanagedType.ByValTStr, SizeConst = 20)]
public string Name; } 这样看看
如果是参考
Marshal.PtrToStructure 方法..
今天回帖带祝福,七夕情人节快乐~^_^
****************************************************************[/align=center]
知道格式都可以读出来
using System.Runtime.Serialization.Formatters.Binary;
using System.IO;//使结构可序列化
[Serializable]
public struct Person
{
public int number;
public string Address;
public string Name;
} //存:
IFormatter formatter = new BinaryFormatter();
Person person = new Person();
person.number = 0;
person.Address = "123";
pserson.Name = "456";using (Stream stream = new FileStream("C:\\a.his", FileMode.Create))
{
formatter.Serialize(stream, pserson);
stream.Close();
}
//取
IFormatter formatter = new BinaryFormatter();
using (Stream stream = new FileStream("C:\\a.his", FileMode.Open))
{
Person person = (Person)formatter.Deserialize(stream);
stream.Close();
}