943,840 Members | Top Members by Rank

Ad:
  • C# Discussion Thread
  • Marked Solved
  • Views: 1109
  • C# RSS
Feb 6th, 2009
0

Problem with serialization

Expand Post »
I used this code to serialize:

C# Syntax (Toggle Plain Text)
  1. using System;
  2. using System.IO;
  3. using System.Runtime.Serialization.Formatters.Binary;
  4.  
  5. [Serializable]
  6. public class Car
  7. {
  8. public string Make;
  9. public string Model;
  10. public uint Year;
  11. public byte Color;
  12. }
  13.  
  14. public class Exercise
  15. {
  16. static void Main(string[] args)
  17. {
  18. Car vehicle = new Car();
  19.  
  20. vehicle.Make = "Lexus";
  21. vehicle.Model = "LS";
  22. vehicle.Year = 2007;
  23. vehicle.Color = 4;
  24.  
  25. FileStream stmCar = new FileStream("Car3.car", FileMode.Create);
  26. BinaryFormatter bfmCar = new BinaryFormatter();
  27.  
  28. bfmCar.Serialize(stmCar, vehicle);
  29. }
  30. }
and program creating without problems file car3.car.

But when I used this program to desesrialize:
C# Syntax (Toggle Plain Text)
  1. using System;
  2. using System.IO;
  3. using System.Runtime.Serialization.Formatters.Binary;
  4.  
  5. [Serializable]
  6. public class Car
  7. {
  8. public string Make;
  9. public string Model;
  10. public uint Year;
  11. public byte Color;
  12. }
  13.  
  14. class Program
  15. {
  16. static void Main(string[] args)
  17. {
  18. FileStream stmCar = new FileStream("Car3.car", FileMode.Open);
  19. BinaryFormatter bfmCar = new BinaryFormatter();
  20. //1 Car vehicle = (Car)bfmCar.Deserialize(stmCar);
  21.  
  22. Console.WriteLine("Make: {0}", vehicle.Make);
  23. Console.WriteLine("Model: {0}", vehicle.Model);
  24. Console.WriteLine("Year: {0}", vehicle.Year);
  25. Console.Write("Color: ");
  26. byte clr = vehicle.Color;
  27.  
  28. switch (clr)
  29. {
  30. case 1:
  31. Console.WriteLine("Black");
  32. break;
  33. case 2:
  34. Console.WriteLine("Gray");
  35. break;
  36. case 3:
  37. Console.WriteLine("White");
  38. break;
  39. case 4:
  40. Console.WriteLine("Red");
  41. break;
  42. case 5:
  43. Console.WriteLine("Blue");
  44. break;
  45. }
  46.  
  47. stmCar.Close();
  48. }
  49. }

I get error in line //1 "System.IO.FileNotFoundException was unhandled Message="Could not find file 'E:\\WindowsCS\\Learning CSharp\\Test21\\bin\\Debug\\Car3.car'."
Source="mscorlib"
FileName="E:\\WindowsCS\\Learning CSharp\\Test21\\bin\\Debug\\Car3.car"
StackTrace:
at System.IO.__Error.WinIOError(Int32 errorCode, String maybeFullPath)
at System.IO.FileStream.Init(String path, FileMode mode, FileAccess access, Int32 rights, Boolean useRights, FileShare share, Int32 bufferSize, FileOptions options, SECURITY_ATTRIBUTES secAttrs, String msgPath, Boolean bFromProxy)
at System.IO.FileStream..ctor(String path, FileMode mode)
at Test21.Program.Main(String[] args) in E:\WindowsCS\Learning CSharp\Test21\Program.cs:line 20
at System.AppDomain._nExecuteAssembly(Assembly assembly, String[] args)
at System.AppDomain.ExecuteAssembly(String assemblyFile, Evidence assemblySecurity, String[] args)
at Microsoft.VisualStudio.HostingProcess.HostProc.RunUsersAssembly()
at System.Threading.ThreadHelper.ThreadStart_Context(Object state)
at System.Threading.ExecutionContext.Run(ExecutionContext executionContext, ContextCallback callback, Object state)
at System.Threading.ThreadHelper.ThreadStart()

I tried to make this file work, but I didin't understand what must I do. Please help.
Similar Threads
Reputation Points: 10
Solved Threads: 0
Newbie Poster
piotr_kast is offline Offline
21 posts
since Feb 2008
Feb 6th, 2009
0

Re: Problem with serialization

it cant find your file to read in, thats the problem. Copy the car3.car into the debug directory of your new project
Reputation Points: 196
Solved Threads: 190
Posting Virtuoso
LizR is offline Offline
1,735 posts
since Aug 2008
Feb 6th, 2009
0

Re: Problem with serialization

Yes I moved car3.car to directory with deserialization exe file. When I debuging this program step by step in Visual Studio 2005, program opened this file. Using System.IO.Directory.GetCurrentDirectory() I checked if it's really this file opened, and string assigned to this method indicates that I opened correct file. But still I had error when program try execute line marked as //1.
Reputation Points: 10
Solved Threads: 0
Newbie Poster
piotr_kast is offline Offline
21 posts
since Feb 2008
Feb 6th, 2009
0

Re: Problem with serialization

But its not finding it. So, either its not looking where you think (use something like the FileMon from what used to be sysinternals, or, such) to check where its looking coz its not looking where your file is.

Of course it does tell you exactly where its looking but its worth checking anyway
Last edited by LizR; Feb 6th, 2009 at 12:52 pm.
Reputation Points: 196
Solved Threads: 190
Posting Virtuoso
LizR is offline Offline
1,735 posts
since Aug 2008
Feb 7th, 2009
0

Re: Problem with serialization

I checked LizR all your suggestions - and I'm greatfull for them - but problem turns out elsewhere. Finally turns out that I cannot do deserialization without doing serialization first in this same program.
Why is in that way - I don't know. I only guess...
Last edited by piotr_kast; Feb 7th, 2009 at 5:14 am.
Reputation Points: 10
Solved Threads: 0
Newbie Poster
piotr_kast is offline Offline
21 posts
since Feb 2008
Feb 7th, 2009
0

Re: Problem with serialization

But that wasnt the error you had. The error you had was 100% it couldnt find the file, not that it couldnt read what was in it
Reputation Points: 196
Solved Threads: 190
Posting Virtuoso
LizR is offline Offline
1,735 posts
since Aug 2008
Feb 7th, 2009
1

Re: Problem with serialization

try to write the full path of "car3.car" ;
Reputation Points: 28
Solved Threads: 5
Light Poster
BlackSun is offline Offline
46 posts
since Feb 2008
Feb 7th, 2009
0

Re: Problem with serialization

Thanks BlackSun! Now program don't need first serialize and working perfect!
Reputation Points: 10
Solved Threads: 0
Newbie Poster
piotr_kast is offline Offline
21 posts
since Feb 2008
Feb 7th, 2009
0

Re: Problem with serialization

u welcome my friend
Reputation Points: 28
Solved Threads: 5
Light Poster
BlackSun is offline Offline
46 posts
since Feb 2008
Feb 7th, 2009
0

Re: Problem with serialization

Sigh..

So the fact Ive been telling you it couldnt find the file you reckon had nothing to do with it.. I did tell you to check as it was telling you exactly where it was looking... grr
Last edited by LizR; Feb 7th, 2009 at 2:11 pm.
Reputation Points: 196
Solved Threads: 190
Posting Virtuoso
LizR is offline Offline
1,735 posts
since Aug 2008

This thread is solved

Either the thread starter or a moderator has marked this thread as solved. You can most likely trust the responses and answers given. There is most likely no reason for any further responses to be posted here. If you have a related question, please start a new thread in this forum instead.

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: Application hangs.
Next Thread in C# Forum Timeline: Extract File Name





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


Follow us on Twitter


© 2011 DaniWeb® LLC