Problem with serialization

Please support our C# advertiser: Intel Parallel Studio Home
Thread Solved

Join Date: Feb 2008
Posts: 17
Reputation: piotr_kast is an unknown quantity at this point 
Solved Threads: 0
piotr_kast piotr_kast is offline Offline
Newbie Poster

Problem with serialization

 
0
  #1
Feb 6th, 2009
I used this code to serialize:

  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:
  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.
Reply With Quote Quick reply to this message  
Join Date: Aug 2008
Posts: 1,735
Reputation: LizR has a spectacular aura about LizR has a spectacular aura about 
Solved Threads: 186
LizR LizR is offline Offline
Posting Virtuoso

Re: Problem with serialization

 
0
  #2
Feb 6th, 2009
it cant find your file to read in, thats the problem. Copy the car3.car into the debug directory of your new project
Did I just hear "You gotta help us, Doc. We've tried nothin' and we're all out of ideas" ? Is this you? Dont let this be you! I will put in as much effort as you seem to.
Reply With Quote Quick reply to this message  
Join Date: Feb 2008
Posts: 17
Reputation: piotr_kast is an unknown quantity at this point 
Solved Threads: 0
piotr_kast piotr_kast is offline Offline
Newbie Poster

Re: Problem with serialization

 
0
  #3
Feb 6th, 2009
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.
Reply With Quote Quick reply to this message  
Join Date: Aug 2008
Posts: 1,735
Reputation: LizR has a spectacular aura about LizR has a spectacular aura about 
Solved Threads: 186
LizR LizR is offline Offline
Posting Virtuoso

Re: Problem with serialization

 
0
  #4
Feb 6th, 2009
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.
Did I just hear "You gotta help us, Doc. We've tried nothin' and we're all out of ideas" ? Is this you? Dont let this be you! I will put in as much effort as you seem to.
Reply With Quote Quick reply to this message  
Join Date: Feb 2008
Posts: 17
Reputation: piotr_kast is an unknown quantity at this point 
Solved Threads: 0
piotr_kast piotr_kast is offline Offline
Newbie Poster

Re: Problem with serialization

 
0
  #5
Feb 7th, 2009
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.
Reply With Quote Quick reply to this message  
Join Date: Aug 2008
Posts: 1,735
Reputation: LizR has a spectacular aura about LizR has a spectacular aura about 
Solved Threads: 186
LizR LizR is offline Offline
Posting Virtuoso

Re: Problem with serialization

 
0
  #6
Feb 7th, 2009
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
Did I just hear "You gotta help us, Doc. We've tried nothin' and we're all out of ideas" ? Is this you? Dont let this be you! I will put in as much effort as you seem to.
Reply With Quote Quick reply to this message  
Join Date: Feb 2008
Posts: 46
Reputation: BlackSun is an unknown quantity at this point 
Solved Threads: 4
BlackSun BlackSun is offline Offline
Light Poster

Re: Problem with serialization

 
1
  #7
Feb 7th, 2009
try to write the full path of "car3.car" ;
Reply With Quote Quick reply to this message  
Join Date: Feb 2008
Posts: 17
Reputation: piotr_kast is an unknown quantity at this point 
Solved Threads: 0
piotr_kast piotr_kast is offline Offline
Newbie Poster

Re: Problem with serialization

 
0
  #8
Feb 7th, 2009
Thanks BlackSun! Now program don't need first serialize and working perfect!
Reply With Quote Quick reply to this message  
Join Date: Feb 2008
Posts: 46
Reputation: BlackSun is an unknown quantity at this point 
Solved Threads: 4
BlackSun BlackSun is offline Offline
Light Poster

Re: Problem with serialization

 
0
  #9
Feb 7th, 2009
u welcome my friend
Reply With Quote Quick reply to this message  
Join Date: Aug 2008
Posts: 1,735
Reputation: LizR has a spectacular aura about LizR has a spectacular aura about 
Solved Threads: 186
LizR LizR is offline Offline
Posting Virtuoso

Re: Problem with serialization

 
0
  #10
Feb 7th, 2009
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.
Did I just hear "You gotta help us, Doc. We've tried nothin' and we're all out of ideas" ? Is this you? Dont let this be you! I will put in as much effort as you seem to.
Reply With Quote Quick reply to this message  
Reply

This thread has been marked solved.
Perhaps start a new thread instead?
Message:


Thread Tools Search this Thread



About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC