Method Save to File
bool SaveToFile(string Filepath)
{
if (File.Exists(Filepath))
File.Delete(Filepath);
System.IO.StreamWriter file = new System.IO.StreamWriter(Filepath);
foreach (Inventory item in _colInventory)
{
string Line = InventoryToLine(item);
file.WriteLine(Line);
}
file.Close();
return true;
Method Inventory to Line
string InventoryToLine(Inventory item)
{
StringBuilder sb = new StringBuilder();
sb.Append(item.Id);
sb.Append('|');
sb.Append(item.Name);
sb.Append('|');
sb.Append(item.SerialNumber);
sb.Append('|');
sb.Append(item.ImagePath);
Debug.WriteLine(sb.ToString());
return sb.ToString ();
}
Method Read from File
bool LoadFromFile(string Filepath)
{
if (!File.Exists(Filepath))
return false;
string line;
System.IO.StreamReader file = new System.IO.StreamReader(Filepath);
while ((line = file.ReadLine()) != null)
{
Inventory item;
if (ParseInventory(line, out item))
_colInventory.Add(item);
}
file.Close();
return true;
Method Parse Inventory
bool ParseInventory(string Text, out Inventory item)//utk splitkan data by bar
{
item = new Inventory();
string[] arr = Text.Split('|');
if (arr.Length != 4)
return false;
item.Id = arr[0];
item.Name = arr[1];
item.SerialNumber = arr[2];
item.ImagePath = arr[3];
return true;
}
Method Populate List
void PopulateList()
{
Inventory first = new Inventory();
first.Id ="1";
first.Name ="First Item Name";
first.SerialNumber ="First Item Serial Number";
first.ImagePath = "C:\\Users\\user\\Desktop\\ibuDeeja.jpg";
_colInventory.Add(first);
Inventory second = new Inventory();
second.Id = "2";
second.Name = "Second Item Name";
second.SerialNumber = "Second Item Serial Number";
second.ImagePath = "C:\\Users\\user\\Pictures\\2013-02-24\\003.jpg";
_colInventory.Add(second);
Inventory third = new Inventory();
third.Id = "3";
third.Name = "Third Item Name";
third.SerialNumber = "Third Item Serial Number";
third.ImagePath = "C:\\Users\\user\\Pictures\\2013-02-24\\011.jpg";
_colInventory.Add(third);
Method Parse Inventory
bool ParseInventory(string Text, out Inventory item)//utk splitkan data by bar
{
item = new Inventory();
string[] arr = Text.Split('|');
if (arr.Length != 4)
return false;
item.Id = arr[0];
item.Name = arr[1];
item.SerialNumber = arr[2];
item.ImagePath = arr[3];
return true;
}
Method Inventory to Line
string InventoryToLine(Inventory item)
{
StringBuilder sb = new StringBuilder();
sb.Append(item.Id);
sb.Append('|');
sb.Append(item.Name);
sb.Append('|');
sb.Append(item.SerialNumber);
sb.Append('|');
sb.Append(item.ImagePath);
Debug.WriteLine(sb.ToString());
return sb.ToString ();
}