I used this code to serialize:

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);
    }
}

and program creating without problems file car3.car.

But when I used this program to desesrialize:

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.

Recommended Answers

All 9 Replies

it cant find your file to read in, thats the problem. Copy the car3.car into the debug directory of your new project

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.

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

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...

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

try to write the full path of "car3.car" ;

Thanks BlackSun! Now program don't need first serialize and working perfect!

u welcome my friend

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

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.