TECHNOLOGIES
FORUMS
JOBS
BOOKS
EVENTS
INTERVIEWS
Live
MORE
LEARN
Training
CAREER
MEMBERS
VIDEOS
NEWS
BLOGS
Sign Up
Login
No unread comment.
View All Comments
No unread message.
View All Messages
No unread notification.
View All Notifications
Answers
Post
An Article
A Blog
A News
A Video
An EBook
An Interview Question
Ask Question
Forums
Monthly Leaders
Forum guidelines
pagefault pagefault
NA
12
10.8k
System.IO.FileNotFoundException when using BinaryFormatter.
Mar 14 2012 8:22 AM
Dear all,
I'm relatively new to C# and have a problem that stumps me. I think it might be related to something called an 'assembly'. I store and retrieve data to/from file 'serializing' two ArrayLists ('
BinaryFormatter
') and also store two plain integers using '
BinaryWriter
'. See code functions "
LoadFromFile()
" and "
StoreToFile()
".
The program works just fine until I try to run it in another directory.
That is to say I copied and compiled all the source files into the new directory. I then also copied the datafile ("
videostore.bin
") created by the identical same program in the old directory and expected it to work just the same in the new directory, why shouldn't it, right ?
I get this error message (just the head, it's a lot longer).
//
System.IO.FileNotFoundException: Could not load file or assembly 'Videostore' or one of its dependencies. The system cannot find the file specified.
File name: 'Videostore'
//
The exception occurs on the first Deserialize statement.
Videos = (ArrayList) bf.Deserialize (stream);
It finds the file. That is not the problem. If I remove the file then it hits 'Exception' on the previous line "
stream = File.Open (...
" as it should. Further If I comment out (remove) the two Deserialize statements then it will not make 'Exception' but instead read two integers (which gets nonsense values now of course).
If I use "
StoreToFile()
" to create a new datafile in the new directory (instead of using the identical datafile copied from the old directory) and then tries "
LoadFromFile()
" anew, then it will work. It is as if it refuses to load an identical file if it hasn't written it itself. Bizarre ...
It must be some kind of contextual information is written somewhere when the file is created by "
StoreToFile()
". But it only needs this for more complex file operations like 'Serialize', the simpler read/write of integers works without throwing exceptions.
I suspect I need to supply additional info, perhaps in the form of an 'assembly' file for it to work.
using System;
using System.Collections;
using System.IO;
using System.Runtime.Serialization.Formatters.Binary;
class filedebug
{
public ArrayList Videos;
public ArrayList Customers;
int a=0, b=0;
static void Main ()
{
(new filedebug ()).Run ();
}
void Run()
{
Console.WriteLine( LoadFromFile ());
Console.WriteLine( StoreToFile ());
}
public bool LoadFromFile ()
{
bool success = true;
FileStream stream = null;
BinaryFormatter bf = new BinaryFormatter ();
try
{
stream = File.Open ("videostore.bin", FileMode.Open, FileAccess.Read);
Videos = (ArrayList) bf.Deserialize (stream);
Customers = (ArrayList) bf.Deserialize (stream);
using (BinaryReader br = new BinaryReader(stream))
{
a = br.ReadInt32();
b = br.ReadInt32();
}
}
catch (Exception e)
{
Console.WriteLine(e);
success = false;
}
finally
{
if (stream != null)
stream.Close ();
}
return success;
}
public bool StoreToFile ()
{
bool success = true;
FileStream stream = null;
BinaryFormatter bf = new BinaryFormatter ();
try
{
stream = File.Open ("videostore.bin", FileMode.Create, FileAccess.Write);
bf.Serialize (stream, Videos);
bf.Serialize (stream, Customers);
using (BinaryWriter bw = new BinaryWriter(stream))
{
bw.Write (a);
bw.Write (b);
}
}
catch (Exception)
{
success = false;
}
finally
{
if (stream != null)
stream.Close ();
}
return success;
}
}
Reply
Answers (
2
)
Page not fully loading
rounding to nearest .5