VB.NET 2008 Get resource file as IO.Stream


DevX Home    Today's Headlines   Articles Archive   Tip Bank   Forums   

Results 1 to 5 of 5

Thread: VB.NET 2008 Get resource file as IO.Stream

Hybrid View

  1. #1
    Join Date
    Jul 2010
    Posts
    5

    VB.NET 2008 Get resource file as IO.Stream

    I am developing a VB.NET 2008 application, consisting of a main EXE application and several resource DLLs.

    I need two functions for the DLL:

    1) one function to get a list of all files in the resources of the DLL.
    2) one function to return 'by name' a resource file as a System.IO.Stream type.

    NOTE: The files will not be "Embedded Resource" (this doubles the size of the DLL), so I think cannot use 'GetManifestResourceStream'.

    I currently use:

    Return New System.IO.MemoryStream(My.Resources.MyFileName)

    but I want it to be called by name.



    I am adding the resources using the VS 2008 "Resources" tab, from the My Project screen, I'm not manually editing Resources.resx

    As I said before, it works with:

    Return New System.IO.MemoryStream(My.Resources.MyFileName)

    but I need to get the resources by name, which also works by using

    GetMAnifestResourceStream

    but this requires to set the resources Build Action to "Embedded Resource", which doubles the size of my DLL file.

    I currently use "None" as Build Action, but I can use any Build Action that does not increase the file size more than the actual resources size.


    Thank you in advance.

  2. #2
    Join Date
    Feb 2004
    Location
    Longueuil, Québec
    Posts
    577
    I might not understand well what you are trying to do, but at first sight there are contradictions in what you are telling.

    You are telling us that you are developing an application (a .EXE) and several resources DLL. Then you state that the resources will not be embedded resources, otherwise it doubles the size of the DLL.

    This does make sense. Embedded resources are compiled into the .EXE. When you do so, you do not need to have the resources in a DLL, since they are included in the application. Thus, the DLL should not double in size, it should "disappear".

    The .EXE will become bigger, but without detriment to the performance of the application. On the contrary, loading an .EXE with embedded resources is probably less demanding on the system than loading an .EXE with external resources in a DLL. I have noticed that (although the difference is minimal) in my own application that almost always deal with both embedded and external resources since here in Quebec, we often have to develop multilingual applications.

    Also, when you do not embed the resources, they are not compiled into "several resources DLL". Unless you are developing a multilingual application (with resource files such as App.fr.resx, all the .resx files are combined into only one DLL.

    If you are developing a multi-language application, you have a different resource dll for each language. But in such a case, you do not need to know the resources by filename, you just have to change the culture of the application (My.Application.ChangeUICulture("fr") to switch to French) for the framework to automatically switch to the proper language.

    Either you are doing something out of the ordinary with your resources, either you do not understand what resources are and how to use them.

    Which is which?
    Jacques Bourgeois
    JBFI
    http://www3.sympatico.ca/jbfi/homeus.htm

  3. #3
    Join Date
    Jul 2010
    Posts
    5
    Here is a review of my question.

    I am developing a VB.NET 2008 application, consisting of a main EXE application and several resource DLLs.

    I am having trouble with two functions for the DLL:

    1) one function to get a list of all files in the resources of the DLL.
    2) one function to return 'by name' a resource file as a System.IO.Stream type.


    -----------------------------------------------------
    I will try to explain the whole process step by step.

    For example, I put 3 resource files in the DLL. To do that I do the following steps, in the Visual Studio 2008:

    - I double click on 'My Project' in the 'Solution Explorer'
    - I click on 'Resources' tab
    - From 'Add Resource' top menu, I click on 'Add Existing File...'
    - Then an 'Open Dialog' opens
    - I select 3 files that I want to incorporate in the DLL
    - Then my files get added in the 'Resources' screen, and a 'Resources' folder with my files appears in the 'Solution Explorer'


    Now, if I click on each file in the 'Resources' folder from the 'Solution Explorer', in the property grid, the properties of the file appear.


    In the documentation it is suggested to set the 'Build Action' property to 'Embedded Resource'. This makes it easy to read and enumerate the resources, but this property set to 'Embedded Resource' also doubles the file size (the DLL size is double the size of all the files added)


    To keep the file size normal, I set the 'Build Action' property to 'None'.
    The 'None' property also incorporates the files into the DLL, but the DLL now has the correct size (meaning the sum of all added files)


    Now I am trying to make two functions for the DLL:

    1) one function to get a list of all files in the resources of the DLL.
    2) one function to return 'by name' a resource file as a System.IO.Stream type.


    I suggest to do all these steps before you answer, so you understand my request.
    -----------------------------------------------------


    When the 'Build Action' property was set to 'Embedded Resource', there were some ways to enumerate resource files by name, using 'GetMAnifestResourceStream'.

    When the 'Build Action' property is set to 'None' the 'GetMAnifestResourceStream' doesn't work anymore.

    Also, When the 'Build Action' property is set to 'None', I can only use:
    Return New System.IO.MemoryStream(My.Resources.MyFileName) to get a file from the resources, but I have to write manually 'My.Resources.MyFileName' at design time, for each file added in resources. Also I can not enumerate the resources.


    Could you please give me some suggestions on enumerating and retrieving the files by name at runtime.

    Please, don't ask questions like 'why do you need this', or suggest that I set 'Build Action' to 'Embedded Resource' as a solution.


    Thank you in advance.

  4. #4
    Join Date
    Feb 2004
    Location
    Longueuil, Québec
    Posts
    577
    First, I would like to point out that asking somebody who want to help you not to ask questions like 'why do you need this' is very bad. How can we help you or offer you alternatives if we do not know what you are aiming at. Very often, a problem does not come from a bad operation, but from a bad idea of how to do something in the start.

    At the same time, it might prevent other users from learning from your own experience. Nobody is here to help only one person. Those who help know that by helping one, we help many.

    For instance, I wonder why you want to iterate through the names of the files. Since a resource is something that cannot be changed once you have compiled it into the application (onless you are using your resources in a satellite DLL, which you did not mention), you should know the name of the files from the starts and My.Resources.FileName should be sufficient.

    Same for asking not to
    suggest that I set 'Build Action' to 'Embedded Resource' as a solution
    . It might well be the only solution.

    It's like asking someone: love me but don't kiss me or touch me. It limits the love. It limits the scope of what we can do to help you.

    -----

    When you say that you are setting the Build Action to 'None', on which file(s) are you doing so? You are not specifying that information.

    Is that on the .resx?

    Then no wonder you cannot access them, the .resx is not incorporated in the DLL. The compiler simply disregard the .resx at compile time.

    You need to set Build Action to "Embedded Resource" if you want to include the resources in the DLL (sorry). Sure, it will enlarge the DLL... because it will then also contains the resources over the compiled code.
    Jacques Bourgeois
    JBFI
    http://www3.sympatico.ca/jbfi/homeus.htm

  5. #5
    Join Date
    Jul 2010
    Posts
    5
    I found the solution, I was actually adding the files two times.

    http://www.vbdotnetforums.com/vb-net...tml#post121927

    Now I can use, GetMAnifestResourceStream.

    Thank you again.
    Thank you very much for your support.

Similar Threads

  1. Replies: 1
    Last Post: 03-20-2008, 02:04 AM
  2. Replies: 1
    Last Post: 01-25-2006, 12:12 PM
  3. Replies: 2
    Last Post: 05-04-2005, 06:26 PM
  4. wav file fade in out help
    By jase_dukerider in forum C++
    Replies: 2
    Last Post: 04-14-2005, 07:48 PM
  5. NullPointerException when reading text file
    By Andrew McLellan in forum Java
    Replies: 3
    Last Post: 05-09-2001, 05:34 PM

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
HTML5 Development Center
 
 
FAQ
Latest Articles
Java
.NET
XML
Database
Enterprise
Questions? Contact us.
C++
Web Development
Wireless
Latest Tips
Open Source


   Development Centers

   -- Android Development Center
   -- Cloud Development Project Center
   -- HTML5 Development Center
   -- Windows Mobile Development Center