codecsharp <--test.. testar code taggen
<codecsharp>
using System;
using System.IO;
using System.Runtime.Serialization.Formatters.Binary;
namespace AktiePortfölj
{
///
/// Summary description for Class1.
///
class Class1
{
///
/// The main entry point for the application.
///
[STAThread]
static void Main(string[] args)
{
string Aktie = "Ericsson";
decimal Köp = 50;
decimal Sälj = 0;
DateTime dtKöp = DateTime.Now;
DateTime dtSälj = DateTime.Now.AddMonths(6);
string filePath = @"c:\aktiefiler\" + Aktie + "_" + dtKöp.ToShortDateString().Replace(" ", "_") + ".dat";
MinaAktier ma = new MinaAktier(Aktie, Köp, Sälj, dtKöp, dtSälj);
FileStream fs = new FileStream(filePath, FileMode.Create);
BinaryFormatter bf = new BinaryFormatter();
bf.Serialize(fs, ma);
Console.WriteLine("Aktien har sparats till {0}", filePath);
}
}
[Serializable()]
public class MinaAktier
{
public MinaAktier(string AktieNamn,
decimal KöpKurs,
decimal SäljKurs,
DateTime KöpDatum,
DateTime SäljDatum)
{
aktieNamn = AktieNamn;
köpKurs = KöpKurs;
säljKurs = SäljKurs;
köpDatum = KöpDatum;
säljDatum = SäljDatum;
}
private string aktieNamn;
private decimal köpKurs;
private decimal säljKurs;
private DateTime köpDatum;
private DateTime säljDatum;
}
}
</code>
<codecsharp>
using System;
using System.Collections;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Web;
using System.Web.SessionState;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.HtmlControls;
using System.IO;
using System.Runtime.Serialization.Formatters.Binary;
namespace NetSamplesCS
{
///
/// Summary description for SeDerializeClass.
///
public class SeDerializeClass : System.Web.UI.Page
{
protected System.Web.UI.WebControls.Button btnDeSeri;
protected System.Web.UI.WebControls.DropDownList ddlStock;
protected System.Web.UI.WebControls.DropDownList ddlFile;
protected System.Web.UI.WebControls.DropDownList ddlBindClass;
protected System.Web.UI.WebControls.Label lblCls1;
protected System.Web.UI.WebControls.Label lblCls2;
protected System.Web.UI.WebControls.Button btnSeri;
private void Page_Load(object sender, System.EventArgs e)
{
if(!IsPostBack)
{
DirectoryInfo di = new DirectoryInfo(@"c:\aktiefiler\");
this.ddlFile.Items.Clear();
foreach(FileInfo file in di.GetFiles("*.dat"))
{
this.ddlFile.Items.Add(new ListItem(file.ToString()));
}
}
}
#region Web Form Designer generated code
override protected void OnInit(EventArgs e)
{
//
// CODEGEN: This call is required by the ASP.NET Web Form Designer.
//
InitializeComponent();
base.OnInit(e);
}
///
/// Required method for Designer support - do not modify
/// the contents of this method with the code editor.
///
private void InitializeComponent()
{
this.btnSeri.Click += new System.EventHandler(this.btnSerialize_Click);
this.btnDeSeri.Click += new System.EventHandler(this.btnDeSerialize_Click);
this.Load += new System.EventHandler(this.Page_Load);
}
#endregion
private void btnSerialize_Click(object sender, System.EventArgs e)
{
if (this.ddlStock.SelectedItem.Value != "0")
{
string Aktie = this.ddlStock.SelectedItem.Value;
decimal Köp = 50;
decimal Sälj = 0;
DateTime dtKöp = DateTime.Now;
DateTime dtSälj = DateTime.Now.AddMonths(6);
string filePath = @"c:\aktiefiler\" + Aktie + "_" + dtKöp.ToShortDateString().Replace(" ", "_") + ".dat";
MinaAktier ma = new MinaAktier(Aktie, Köp, Sälj, dtKöp, dtSälj);
using(FileStream fs = new FileStream(filePath, FileMode.Create))
{
BinaryFormatter bf = new BinaryFormatter();
bf.Serialize(fs, ma);
}
}
}
private void btnDeSerialize_Click(object sender, System.EventArgs e)
{
string Aktie = this.ddlFile.SelectedItem.Value;
string filePath = @"c:\aktiefiler\" + Aktie;
using(FileStream fs = new FileStream(filePath, FileMode.Open))
{
BinaryFormatter bf = new BinaryFormatter();
MinaAktier ma = (MinaAktier)bf.Deserialize(fs);
this.lblCls1.Text = ma.AktieNamn;
this.lblCls2.Text = ma.KöpDatum.ToString("d");
this.ddlBindClass.Items.Add(new ListItem(ma.SäljDatum.ToString("d")));
this.ddlBindClass.Items.Add(new ListItem(string.Format("{0}", ma.KöpKurs)));
this.ddlBindClass.Items.Add(new ListItem(string.Format("{0}", ma.SäljKurs)));
}
}
}
[Serializable()]
public class MinaAktier
{
public MinaAktier(string AktieNamn,
decimal KöpKurs,
decimal SäljKurs,
DateTime KöpDatum,
DateTime SäljDatum)
{
aktieNamn = AktieNamn;
köpKurs = KöpKurs;
säljKurs = SäljKurs;
köpDatum = KöpDatum;
säljDatum = SäljDatum;
}
public MinaAktier()
{
}
public string AktieNamn
{
get{ return aktieNamn; }
}
public decimal KöpKurs
{
get{ return köpKurs; }
}
public decimal SäljKurs
{
get{ return säljKurs; }
}
public DateTime KöpDatum
{
get{ return köpDatum; }
}
public DateTime SäljDatum
{
get{ return säljDatum; }
}
private string aktieNamn;
private decimal köpKurs;
private decimal säljKurs;
private DateTime köpDatum;
private DateTime säljDatum;
}
}
</code>
code c# <---
using System;
using System.Collections;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Web;
using System.Web.SessionState;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.HtmlControls;
using System.IO;
using System.Runtime.Serialization.Formatters.Binary;
namespace NetSamplesCS
{
///
/// Summary description for SeDerializeClass.
///
public class SeDerializeClass : System.Web.UI.Page
{
protected System.Web.UI.WebControls.Button btnDeSeri;
protected System.Web.UI.WebControls.DropDownList ddlStock;
protected System.Web.UI.WebControls.DropDownList ddlFile;
protected System.Web.UI.WebControls.DropDownList ddlBindClass;
protected System.Web.UI.WebControls.Label lblCls1;
protected System.Web.UI.WebControls.Label lblCls2;
protected System.Web.UI.WebControls.Button btnSeri;
private void Page_Load(object sender, System.EventArgs e)
{
if(!IsPostBack)
{
DirectoryInfo di = new DirectoryInfo(@"c:\aktiefiler\");
this.ddlFile.Items.Clear();
foreach(FileInfo file in di.GetFiles("*.dat"))
{
this.ddlFile.Items.Add(new ListItem(file.ToString()));
}
}
}
#region Web Form Designer generated code
override protected void OnInit(EventArgs e)
{
//
// CODEGEN: This call is required by the ASP.NET Web Form Designer.
//
InitializeComponent();
base.OnInit(e);
}
///
/// Required method for Designer support - do not modify
/// the contents of this method with the code editor.
///
private void InitializeComponent()
{
this.btnSeri.Click += new System.EventHandler(this.btnSerialize_Click);
this.btnDeSeri.Click += new System.EventHandler(this.btnDeSerialize_Click);
this.Load += new System.EventHandler(this.Page_Load);
}
#endregion
private void btnSerialize_Click(object sender, System.EventArgs e)
{
if (this.ddlStock.SelectedItem.Value != "0")
{
string Aktie = this.ddlStock.SelectedItem.Value;
decimal Köp = 50;
decimal Sälj = 0;
DateTime dtKöp = DateTime.Now;
DateTime dtSälj = DateTime.Now.AddMonths(6);
string filePath = @"c:\aktiefiler\" + Aktie + "_" + dtKöp.ToShortDateString().Replace(" ", "_") + ".dat";
MinaAktier ma = new MinaAktier(Aktie, Köp, Sälj, dtKöp, dtSälj);
using(FileStream fs = new FileStream(filePath, FileMode.Create))
{
BinaryFormatter bf = new BinaryFormatter();
bf.Serialize(fs, ma);
}
}
}
private void btnDeSerialize_Click(object sender, System.EventArgs e)
{
string Aktie = this.ddlFile.SelectedItem.Value;
string filePath = @"c:\aktiefiler\" + Aktie;
using(FileStream fs = new FileStream(filePath, FileMode.Open))
{
BinaryFormatter bf = new BinaryFormatter();
MinaAktier ma = (MinaAktier)bf.Deserialize(fs);
this.lblCls1.Text = ma.AktieNamn;
this.lblCls2.Text = ma.KöpDatum.ToString("d");
this.ddlBindClass.Items.Add(new ListItem(ma.SäljDatum.ToString("d")));
this.ddlBindClass.Items.Add(new ListItem(string.Format("{0}", ma.KöpKurs)));
this.ddlBindClass.Items.Add(new ListItem(string.Format("{0}", ma.SäljKurs)));
}
}
}
[Serializable()]
public class MinaAktier
{
public MinaAktier(string AktieNamn,
decimal KöpKurs,
decimal SäljKurs,
DateTime KöpDatum,
DateTime SäljDatum)
{
aktieNamn = AktieNamn;
köpKurs = KöpKurs;
säljKurs = SäljKurs;
köpDatum = KöpDatum;
säljDatum = SäljDatum;
}
public MinaAktier()
{
}
public string AktieNamn
{
get{ return aktieNamn; }
}
public decimal KöpKurs
{
get{ return köpKurs; }
}
public decimal SäljKurs
{
get{ return säljKurs; }
}
public DateTime KöpDatum
{
get{ return köpDatum; }
}
public DateTime SäljDatum
{
get{ return säljDatum; }
}
private string aktieNamn;
private decimal köpKurs;
private decimal säljKurs;
private DateTime köpDatum;
private DateTime säljDatum;
}
}