943,741 Members | Top Members by Rank

Ad:
  • C# Discussion Thread
  • Unsolved
  • Views: 1299
  • C# RSS
Sep 21st, 2009
0

how to load/save this?

Expand Post »
Hi all!

Well, the name isn't entirely accurate but since I have found it very hard to find some actual useful information on the topic I decided to name it as such.

Purely as a programming practice I would like to make a calendar application. Something along the lines of Rainlendar (brilliant piece of software).

It should save all my appointments and notes and ToDo things and what not.
But for that to work...

I need to be able to write all the information to my harddrive and load it again when the program is loaded again. Now I know the basics of FileStream, StreamWrite and StreamRead but I don't know how to use that with this kind of information.

I'm thinking of something along the lines of this for the lines in a simple .txt file (every type of file is fine by me by the way):
22-09-09, 12.30-14.00, Driving lessons, yes, 22-09-09, 11.00
The date of the appointment, the time, the text to display, whether and alarm should be activated, and then the date and time of that alarm.

But I don't have a clue how to implement something like this.

Is there anybody willing to hand me some pointers on this? It would really be greatly appreciated!
Similar Threads
Reputation Points: 10
Solved Threads: 0
Junior Poster in Training
ctrl-alt-del is offline Offline
65 posts
since Jul 2008
Sep 21st, 2009
0

Re: how to load/save this?

If the storage file does not need to be text, check out this IO Class: BinaryReader & BinaryWriter , which will simplify much of the data typing IO of your fields.

In addition, consider using the BinaryFormatter class:
C# Syntax (Toggle Plain Text)
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Text;
  5. using System.Collections;
  6. using System.Runtime.Serialization.Formatters.Binary;
  7. using System.IO;
  8.  
  9. namespace TestSerializer
  10. {
  11. public class DataSerializer
  12. {
  13. [Serializable]
  14. public class Data : ArrayList
  15. {
  16. [Serializable]
  17. public struct Field
  18. {
  19. string s1;
  20. string s2;
  21. string s3;
  22. int i1;
  23. int i2;
  24. int i3;
  25.  
  26. public Field(string s1, string s2, string s3, int i1, int i2, int i3)
  27. {
  28. this.s1 = s1;
  29. this.s2 = s2;
  30. this.s3 = s3;
  31. this.i1 = i1;
  32. this.i2 = i2;
  33. this.i3 = i3;
  34. }
  35. }
  36.  
  37. public Data()
  38. {
  39. for (int i = 0; i < 100000; i++)
  40. {
  41. Field fld = new Field(
  42. "1 - Some Text " + i,
  43. "2 - Some Text " + i,
  44. "3 - Some Text " + i,
  45. i * 1,
  46. i * 2,
  47. i * 3
  48. );
  49.  
  50. Add(fld);
  51. }
  52. }
  53. }
  54.  
  55. public DataSerializer()
  56. {
  57. Data data = new Data();
  58.  
  59. BinaryFormatter bf = new BinaryFormatter();
  60. using (Stream stream = new FileStream("data.dat", FileMode.Create, FileAccess.Write))
  61. {
  62. bf.Serialize(stream, data);
  63. stream.Flush();
  64. stream.Close();
  65. }
  66.  
  67. Data dataRead;
  68. using (Stream stream = new FileStream("data.dat", FileMode.Open, FileAccess.Read))
  69. {
  70. dataRead = (Data)bf.Deserialize(stream);
  71. stream.Flush();
  72. stream.Close();
  73. }
  74. }
  75.  
  76. public static void Test()
  77. {
  78. DateTime dt = DateTime.Now;
  79. TimeSpan dtStart = dt.TimeOfDay;
  80.  
  81. DataSerializer ds = new DataSerializer();
  82.  
  83. dt = DateTime.Now;
  84. TimeSpan dtEnd = dt.TimeOfDay;
  85.  
  86. TimeSpan dtTotal = dtEnd - dtStart;
  87.  
  88. Console.WriteLine("DataSerializer Total Time: " + dtTotal);
  89. }
  90.  
  91. }
  92. }

Also, check out this thread using a DataSet with xml read/write: http://www.daniweb.com/forums/thread224213.html

There are so many ways you can do this--these are but a few...
Reputation Points: 341
Solved Threads: 233
Posting Shark
DdoubleD is offline Offline
984 posts
since Jul 2009
Sep 21st, 2009
0

Re: how to load/save this?

Quote ...
The date of the appointment, the time, the text to display, whether and alarm should be activated, and then the date and time of that alarm.
First, you should try to organize your info into a class or struct.
Reputation Points: 2035
Solved Threads: 644
Senior Poster
ddanbe is offline Offline
3,736 posts
since Oct 2008
Sep 21st, 2009
0

Re: how to load/save this?

Once you have followed danny's recommendation and organized your members in to a class/struct you can use the XmlSerializer also to persist your class to disk.
Featured Poster
Reputation Points: 1749
Solved Threads: 735
Senior Poster
sknake is offline Offline
3,948 posts
since Feb 2009
Sep 22nd, 2009
0

Re: how to load/save this?

Moderator
Reputation Points: 2136
Solved Threads: 1228
Posting Genius
adatapost is offline Offline
6,527 posts
since Oct 2008

This thread is more than three months old

No one has posted to this discussion for at least three months. Please let old threads die and do not reply to them unless you feel you have something new and valuable to contribute that absolutely must be added to make the discussion complete. Otherwise, please start a new thread in this forum instead.
Message:
Previous Thread in C# Forum Timeline: Background dynamic gradient
Next Thread in C# Forum Timeline: Choosing the Right Integer





About Us | Contact Us | Advertise | Acceptable Use Policy
Forum Index | Build Custom RSS Feed


Follow us on Twitter


© 2011 DaniWeb® LLC