| | |
Problem with serialization
Please support our C# advertiser: Intel Parallel Studio Home
Thread Solved |
•
•
Join Date: Feb 2008
Posts: 17
Reputation:
Solved Threads: 0
I used this code to serialize:
and program creating without problems file car3.car.
But when I used this program to desesrialize:
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.
C# Syntax (Toggle Plain Text)
using System; using System.IO; using System.Runtime.Serialization.Formatters.Binary; [Serializable] public class Car { public string Make; public string Model; public uint Year; public byte Color; } public class Exercise { static void Main(string[] args) { Car vehicle = new Car(); vehicle.Make = "Lexus"; vehicle.Model = "LS"; vehicle.Year = 2007; vehicle.Color = 4; FileStream stmCar = new FileStream("Car3.car", FileMode.Create); BinaryFormatter bfmCar = new BinaryFormatter(); bfmCar.Serialize(stmCar, vehicle); } }
But when I used this program to desesrialize:
C# Syntax (Toggle Plain Text)
using System; using System.IO; using System.Runtime.Serialization.Formatters.Binary; [Serializable] public class Car { public string Make; public string Model; public uint Year; public byte Color; } class Program { static void Main(string[] args) { FileStream stmCar = new FileStream("Car3.car", FileMode.Open); BinaryFormatter bfmCar = new BinaryFormatter(); //1 Car vehicle = (Car)bfmCar.Deserialize(stmCar); Console.WriteLine("Make: {0}", vehicle.Make); Console.WriteLine("Model: {0}", vehicle.Model); Console.WriteLine("Year: {0}", vehicle.Year); Console.Write("Color: "); byte clr = vehicle.Color; switch (clr) { case 1: Console.WriteLine("Black"); break; case 2: Console.WriteLine("Gray"); break; case 3: Console.WriteLine("White"); break; case 4: Console.WriteLine("Red"); break; case 5: Console.WriteLine("Blue"); break; } stmCar.Close(); } }
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.
•
•
Join Date: Feb 2008
Posts: 17
Reputation:
Solved Threads: 0
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.
•
•
Join Date: Aug 2008
Posts: 1,735
Reputation:
Solved Threads: 186
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
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.
•
•
Join Date: Feb 2008
Posts: 17
Reputation:
Solved Threads: 0
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...
Why is in that way - I don't know. I only guess...
Last edited by piotr_kast; Feb 7th, 2009 at 5:14 am.
•
•
Join Date: Aug 2008
Posts: 1,735
Reputation:
Solved Threads: 186
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
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.
![]() |
Similar Threads
- [LINQ, C#]Problem with autogenerated DataClasses.designer.cs (ASP.NET)
- .NET Serialization & Remoting - Mouse and Keystrokes (VB.NET)
- Strange wofstream problem /bug (C++)
- Don't know where the problem is ? (VB.NET)
- VB6 Serialization (Visual Basic 4 / 5 / 6)
- Problem deserializing objects from multiple classes (C#)
- XML Serialization (C#)
- Serialization concepts for structures (C++)
Other Threads in the C# Forum
- Previous Thread: Application hangs.
- Next Thread: Extract File Name
| Thread Tools | Search this Thread |
Tag cloud for C#
.net access algorithm array barchart bitmap box buttons c# chat check checkbox class client color combobox control conversion csharp custom database datagrid datagridview dataset datetime degrees draganddrop drawing encryption enum event excel file files form format forms ftp function gdi+ httpwebrequest image index input install java label list listbox listener login mandelbrot math mouseclick mysql networking object operator oracle path photoshop picturebox pixelinversion post prime programming radians regex remote remoting resource richtextbox save saving serialization server sleep socket sql statistics stream string table tcp text textbox thread time timer treeview update usercontrol validation view visualstudio webbrowser windows winforms wpf xml






