[Okta Webinar] Learn how to a build a cloud-first strategyRegister Now

x
?
Solved

Accessing binary file as embedded resouce to deserialize objects

Posted on 2006-05-09
10
Medium Priority
?
718 Views
Last Modified: 2010-08-05
I have an application that uses supporting files, such as images, html pages, and a binary file.  I have set these files to be embedded resources and, with the images, I have been able to access them to set them as icons and images for the forms I am using.

However for the binary file, I cannot determine how to access it to use throughout the application.

The current code accesses the file from a hardcoded directory path :

FileStream fileStream = new FileStream(DataFile, FileMode.Open);
BinaryFormatter binFormat = new BinaryFormatter();
try
{
    Monday = (Day)binFormat.Deserialize(fileStream);
    Tuesday = (Day)binFormat.Deserialize(fileStream);
    Wednesday = (Day)binFormat.Deserialize(fileStream);
    Thursday = (Day)binFormat.Deserialize(fileStream);
    Friday = (Day)binFormat.Deserialize(fileStream);
    Saturday = (Day)binFormat.Deserialize(fileStream);
    Sunday = (Day)binFormat.Deserialize(fileStream);
    counter = (int)binFormat.Deserialize(fileStream);
}
catch (Exception ex)
{
    MessageBox.Show("failed to deserialize. reason: " + ex.Message);
    throw;
}
finally
{
    fileStream.Close();
}



How can this be changed to use the file as an embedded resource instead of accessing it from a hardcoded directory path?

Thanks.
0
Comment
Question by:harton
  • 6
  • 4
10 Comments
 
LVL 37

Expert Comment

by:gregoryyoung
ID: 16644578
I am assuming you know how to add the file as a resource already and just need to know how to get it and use it with the serializer ...

Stream stream = Assembly.GetCallingAssembly().GetManifestResourceStream(DefaultNamespace + "\" + Filename)

then pass the stream to the serializer.

Cheers,

Greg Young
0
 

Author Comment

by:harton
ID: 16644671
I tried this earlier and an exception was thrown with the following message :

Parameter " cannot be null.
Parameter name : serializationStream

However your code differs from mine in that I was simply passing the value "DATA.dat" whereas yours includes DefaultNamespace + "\" + Filename


is it making a difference that I am not specifying the namespace?
0
 

Author Comment

by:harton
ID: 16644856
FOLLOW-UP :

I use the following code to see all resources in the assembly :

            Assembly a = Assembly.GetExecutingAssembly();
            string[] resNames = a.GetManifestResourceNames();
            String result = String.Format("Found {0} resources\r\n", resNames.Length);
            foreach (string s in resNames)
            {
                result += s + "\r\n";
            }
            MessageBox.Show(result);

My binary file, DATA.dat is not even there.  It shows the following info :

Found 10 resources
ReservationScheduler.Resources.optionicon.gif
ReservationScheduler.icon.ico
ReservationScheduler.Resources.logo.jpg
ReservationScheduler.Resources.icon.ico
ReservationScheduler.Properties.Resources.resources
ReservationScheduler.Resources.logo.gif
ReservationScheduler.PrintRange.resources
ReservationScheduler.Option.resources
ReservationScheduler.Resources.print.gif
ReservationScheduler.ReservationScheduler.resources


This explains the exception thrown earlier - the file was not found so the stream going into the binary formatter was null.

How can I get this file?
Thanks.
0
Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
LVL 37

Expert Comment

by:gregoryyoung
ID: 16644900
Have you made sure its setup as a resource?
0
 

Author Comment

by:harton
ID: 16644968
When I open resources.resx in vs2005, i can see all the resource files I have added.  I see DATA.dat in the files section, it is labeled as DATA (no .dat)

I have not worked with this before so I am unsure if something more needs to be done.

If you would like to see the code, I can be reached at *edited*

I would post code here, but don't want to make it public.

gregoryyoung: edited to remove email
0
 
LVL 37

Expert Comment

by:gregoryyoung
ID: 16645132
right click on the item .. select properties ..

in the properties screen you should see "Build Action" select "Embedded Resource" ...

Cheers,

Greg
0
 

Author Comment

by:harton
ID: 16645162
I did that, they are compiled into the app, I just cannot access them
0
 

Author Comment

by:harton
ID: 16645246
I finally got it to work, but there are 2 issues :

1 - I have noticed a HUGE performance slowdown.

2 - when I use the HTML file this way with a streamWriter, it says the stream is not writeable

0
 
LVL 37

Accepted Solution

by:
gregoryyoung earned 2000 total points
ID: 16645287
The stream is not writable .. it is a read only resource in your DLL.

The performance hit should not be that bad unless you are accessing it alot?

Cheers,

Greg
0
 

Author Comment

by:harton
ID: 16645382
I didn't know it is read-only - that being the case I now need to save it locally.

Thanks for the responses.  
0

Featured Post

Free Tool: SSL Checker

Scans your site and returns information about your SSL implementation and certificate. Helpful for debugging and validating your SSL configuration.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Introduction This article series is supposed to shed some light on the use of IDisposable and objects that inherit from it. In essence, a more apt title for this article would be: using (IDisposable) {}. I’m just not sure how many people would ge…
Introduction Hi all and welcome to my first article on Experts Exchange. A while ago, someone asked me if i could do some tutorials on object oriented programming. I decided to do them on C#. Now you may ask me, why's that? Well, one of the re…
this video summaries big data hadoop online training demo (http://onlineitguru.com/big-data-hadoop-online-training-placement.html) , and covers basics in big data hadoop .
When cloud platforms entered the scene, users and companies jumped on board to take advantage of the many benefits, like the ability to work and connect with company information from various locations. What many didn't foresee was the increased risk…
Suggested Courses
Course of the Month18 days, 15 hours left to enroll

834 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question