Iterate folders in my Resources folder.
Iterate folders in my Resources folder.
Iterate folders in my Resources folder.
Iterate folders in my Resources folder.
Iterate folders in my Resources folder.
Iterate folders in my Resources folder. Iterate folders in my Resources folder. Iterate folders in my Resources folder. Iterate folders in my Resources folder. Iterate folders in my Resources folder. Iterate folders in my Resources folder. Iterate folders in my Resources folder. Iterate folders in my Resources folder.
Iterate folders in my Resources folder. Iterate folders in my Resources folder.
Iterate folders in my Resources folder.
Go Back  Xtreme Visual Basic Talk > > > Iterate folders in my Resources folder.


Reply
 
Thread Tools Display Modes
  #1  
Old 01-07-2011, 12:26 PM
WhatsMyUsername's Avatar
WhatsMyUsername WhatsMyUsername is offline
Regular
 
Join Date: Jan 2011
Location: Currently? Costa Rica
Posts: 52
Default Iterate folders in my Resources folder.


Hello. I have downloaded Visual Basic 2010 express.

My application is very simple:

In my Resources folder, there are four folders. Each has a couple .png images in them.
In my form, I have four checklists and one picturebox.

When my application loads, I want each checklist to populate with the names of the images (one checklist for each folder)

When the user clicks on a checklist, the Picturebox will display the corresponding image.

I managed to do pretty much everything, but my problem is, how the heck do I access the Resources folder (and the four folders it holds), when my application is built and shared to other people?
Reply With Quote
  #2  
Old 01-07-2011, 02:21 PM
AtmaWeapon's Avatar
AtmaWeaponIterate folders in my Resources folder. AtmaWeapon is offline
Fabulous Florist

Forum Leader
* Guru *
 
Join Date: Feb 2004
Location: Austin, TX
Posts: 9,500
Default

I'm sort of unclear on what you're asking; you access files the same way on different machines. I'll try and use a little bit of psychic powers: either you're using absolute paths or you haven't tried it on someone else's computer yet and don't know if it will work.

If you haven't tried it yet, try it. That's the easiest way for me to explain relative paths. By default, if you pass a partial path like "Resources\img1.png" to a method, it assumes you are passing it a path relative to your current directory. So if your project is in "C:\Users\You\Documents\Visual Studio 2010\Projects\YourProject\bin\Debug", the above string would resolve to "C:\...\bin\Debug\Resources\img1.png". If your application's in a different directory, the working directory will be that directory so you'll get the right path in the end.

On the other hand, you could have hard-coded the path to the files on your own hard drive. Don't do that. The program may not work on someone else's machine if you do that. If you need to get to a directory that's not your program's directory (like the user's My Documents folder), there's a trick.

The [url=http://msdn.microsoft.com/en-us/library/14tx8hby.aspx]System.Environment.GetFolderPath()[/i] method will return the path to special folders on the system. The [url=http://msdn.microsoft.com/en-us/library/system.io.path.aspx]System.IO.Path[/i] class has a Combine() method that can be used to glue together paths without worrying about where to put the "\" character. Here's a snippet that finds a "Resources" folder in the current user's My Documents" folder:
Code:
Dim myDocumentsPath As String = Environment.GetFolderPath(Environment.SpecialFolder.MyDocuments)
Dim resourceLocation As String = "Resources\img1.png"
Dim imagePath As String = IO.Path.Combine(myDocumentsPath, resourceLocation)
__________________
.NET Resources
My FAQ threads | Tutor's Corner | Code Library
I would bet money 2/3 of .NET questions are already answered in one of these three places.
Reply With Quote
  #3  
Old 01-07-2011, 03:05 PM
WhatsMyUsername's Avatar
WhatsMyUsername WhatsMyUsername is offline
Regular
 
Join Date: Jan 2011
Location: Currently? Costa Rica
Posts: 52
Default

Wait, if I build my project with the images inside folders in the Resources folder, getting one, single executable file, and I run it, my project will still find \Resources\Folder1\Image.png? For everyone?
Reply With Quote
  #4  
Old 01-07-2011, 03:22 PM
AtmaWeapon's Avatar
AtmaWeaponIterate folders in my Resources folder. AtmaWeapon is offline
Fabulous Florist

Forum Leader
* Guru *
 
Join Date: Feb 2004
Location: Austin, TX
Posts: 9,500
Default

If you deploy the folder along with the application and the directory is in the same place relative to the executable, yes.
__________________
.NET Resources
My FAQ threads | Tutor's Corner | Code Library
I would bet money 2/3 of .NET questions are already answered in one of these three places.
Reply With Quote
  #5  
Old 01-07-2011, 03:23 PM
WhatsMyUsername's Avatar
WhatsMyUsername WhatsMyUsername is offline
Regular
 
Join Date: Jan 2011
Location: Currently? Costa Rica
Posts: 52
Default

I see.... but, I don't want other people to see or the folder and its subfolders!
Reply With Quote
  #6  
Old 01-07-2011, 03:46 PM
AtmaWeapon's Avatar
AtmaWeaponIterate folders in my Resources folder. AtmaWeapon is offline
Fabulous Florist

Forum Leader
* Guru *
 
Join Date: Feb 2004
Location: Austin, TX
Posts: 9,500
Default

In that case, life just got a little harder. If you want to access the resources but don't want to use folders on the hard drive, you have to use embedded resources. It's not something I have a lot of experience with and it's relatively hard to demonstrate. Have a look at this ancient article on the subject; the code might be difficult to use by this point (it targets .NET 1.0) but it's the first place I've ever seen a good explanation for how to figure out the name of a particular resource. Managing Application Resources is probably a more up-to-date start to start looking, but you'll have to read a lot more to make it useful.

It's a heck of a lot easier to just let the files be on the hard drive.
__________________
.NET Resources
My FAQ threads | Tutor's Corner | Code Library
I would bet money 2/3 of .NET questions are already answered in one of these three places.
Reply With Quote
  #7  
Old 01-07-2011, 05:20 PM
WhatsMyUsername's Avatar
WhatsMyUsername WhatsMyUsername is offline
Regular
 
Join Date: Jan 2011
Location: Currently? Costa Rica
Posts: 52
Default

Before you posted that I got a super idea..... very silly actually.

I didn't want to share my resources, but I didn't know about embed resources either. So... I decided to manually-code the listing & image loading. Which is a lot. Sooo I made another application, that would read all my files and output a bunch of code for me to copy and paste.. haha.................

But here's something: Now I access my images through My.Resources.ImageName, but, is it possible to replace that .ImageName by a variable?
Like this:

var = "Image01.png"
My.Resources.var

Is that possible?

Sure I could also make my other app automatically give me a bunch of code for copy and pasting, but heck...
Reply With Quote
  #8  
Old 01-07-2011, 06:54 PM
jantje jantje is offline
Junior Contributor
 
Join Date: Dec 2004
Posts: 255
Default

Or it might be an option to use virtualization. Most good programs are not free.
If I were in your shoes I'd go for BoxedAppSDK or Molebox Virtualization Studio.
If money isn't an issue then VMWare's Thinapp is quite good or Xenocode
Reply With Quote
  #9  
Old 01-08-2011, 09:43 AM
AtmaWeapon's Avatar
AtmaWeaponIterate folders in my Resources folder. AtmaWeapon is offline
Fabulous Florist

Forum Leader
* Guru *
 
Join Date: Feb 2004
Location: Austin, TX
Posts: 9,500
Default

I have no idea what jantje is talking about; virtualization has nothing to do with this problem.

Let's go down the rabbit hole then.

Normally, you'd use properties on the My.Resources object to get at your resources. If you want to enumerate them, you have to go one level lower in the abstraction. From my investigation, you can't do this via that object. Instead, you have to turn to the reflection API.

Note this doesn't work at all if you're using the My.Resources object. That technique doesn't embed the images; it stores them in a separate .resx file. You could probably make this technique work with that, but I'm unfamiliar with how you'd get the reflection API to load that .resx file. To make this work, the images should be added to your project and you should use the properties page to set their "Build Action" property to "Embedded resource". Keep in mind this will make your build time longer and make your EXE look bigger. Some users complain when what looks like a simple game has a 50MB executable, but are satisfied when it's a 30KB executable with 49MB of images.

System.Reflection.Assembly represents a .NET assembly (DLL or EXE). You can get one for your application using the Shared GetExecutingAssembly() method. From there, you can call GetManifestResourceNames() to get the name of all resources in the assembly:
Code:
Private Function GetResourceNames() As String()
    Dim assembly As Reflection.Assembly = Reflection.Assembly.GetExecutingAssembly()
    Dim resourceNames() As String = assembly.GetManifestResourceNames()
    Return resourceNames
End Function
Not all of these are images; VB .NET packs a couple of extra resources along to support the My.Resources object, and you might have other kinds of resources. If your file names don't make the resource type clear, you'll be left having to try and load each resource with GetManifestResourceStream() and taking the failure to mean "that's not the right one":
Code:
Private Function GetImageResources(ByVal resourceNames() As String) As Image()
    Dim images As New List(Of Image)()
    Dim assembly As Reflection.Assembly = Reflection.Assembly.GetExecutingAssembly()
    For Each resourceName As String In resourceNames
        Using s As IO.Stream = assembly.GetManifestResourceStream(resourceName)
            If s IsNot Nothing Then
                ' The resource exists, try to make an image
                Try
                    images.Add(Image.FromStream(s))
                Catch ex As ArgumentException
                End Try
            End If
        End Using
    Next

    Return images.ToArray()
End Function
Obviously, if you can pare down the list of names to just your images, you can skip the ugly try/catch. If you want to group resources by folder, you'll have to parse the folder name out of the name returned by GetManifestResourceNames().

I'll keep looking to see if it looks like My.Resources has facilities to support this, but it doesn't look like it.

With respect to post #7, there would be if My.Resources had some of the methods on Assembly. It doesn't. I don't know if it's impossible, but I don't see any API to provide for it. I'll get back to you on that one.
__________________
.NET Resources
My FAQ threads | Tutor's Corner | Code Library
I would bet money 2/3 of .NET questions are already answered in one of these three places.
Reply With Quote
  #10  
Old 01-08-2011, 10:11 AM
AtmaWeapon's Avatar
AtmaWeaponIterate folders in my Resources folder. AtmaWeapon is offline
Fabulous Florist

Forum Leader
* Guru *
 
Join Date: Feb 2004
Location: Austin, TX
Posts: 9,500
Default

Ahh, found it.

The resources used by My.Resources are always stored in an embedded resource file named "Resources.resources". You can use a ResourceReader to get the names and values of the resources in the file.

The only bit that's a lot worse is figuring out the full name of the file, because you have to read it from the assembly resources then pass the stream to ResourceReader:
Code:
Function GetImages() As Image()
    Dim images As New List(Of Image)()

    Dim assembly As Reflection.Assembly = Reflection.Assembly.GetExecutingAssembly()
    Dim baseName As String = My.Resources.ResourceManager.BaseName
    Dim fileName As String = String.Format("{0}.{1}", baseName, "resources")
    Using resourceStream As IO.Stream = assembly.GetManifestResourceStream(fileName)
I observed the base name to be "<namespace>.Resources"; this feels dirty. Now that we've got that resource stream, we can create a ResourceReader. You use it by enumerating; it returns key/value pairs of each property. Thus, we can try to cast each value to an image and see if it works (again, if you can tell by the name that might be a better approach):
Code:
        Dim reader As New Resources.ResourceReader(resourceStream)
        For Each resource As DictionaryEntry In reader
            Dim value As Image = TryCast(resource.Value, Image)
            If value IsNot Nothing Then
                images.Add(value)
            End If
        Next
    End Using

    Return images.ToArray()
End Function
(The stuff about the file size is true here too, since the .resources file is embedded in the assembly.)
__________________
.NET Resources
My FAQ threads | Tutor's Corner | Code Library
I would bet money 2/3 of .NET questions are already answered in one of these three places.

Last edited by AtmaWeapon; 01-08-2011 at 10:30 AM.
Reply With Quote
  #11  
Old 01-08-2011, 03:35 PM
jantje jantje is offline
Junior Contributor
 
Join Date: Dec 2004
Posts: 255
Default

Quote:
Originally Posted by AtmaWeapon View Post
I have no idea what jantje is talking about; virtualization has nothing to do with this problem.
It does. It's a possible solution.
You can leave your code 100% intact like it is now and simply embed and thus hide the files/folders you don't want your end user to see.
Reply With Quote
  #12  
Old 01-08-2011, 03:49 PM
PlausiblyDamp's Avatar
PlausiblyDampIterate folders in my Resources folder. PlausiblyDamp is offline
Ultimate Contributor

Forum Leader
* Expert *
 
Join Date: Nov 2003
Location: Newport, Wales
Posts: 2,058
Default

So given the OP wanted a way to ship his application with resources and would rather not have these resources in a folder. Your suggestion would involve the end user installing a virtual environment, installing an os into this virtual environment and then installing the application into this virtual environment and the resources would still be in a folder anyway - this didn't really solve the problem posed in the post.
__________________
Intellectuals solve problems; geniuses prevent them.
-- Albert Einstein

Posting Guidelines Forum Rules Use the code tags
Reply With Quote
  #13  
Old 01-08-2011, 05:28 PM
jantje jantje is offline
Junior Contributor
 
Join Date: Dec 2004
Posts: 255
Default

Quote:
Originally Posted by PlausiblyDamp View Post
So given the OP wanted a way to ship his application with resources and would rather not have these resources in a folder. Your suggestion would involve the end user installing a virtual environment, installing an os into this virtual environment and then installing the application into this virtual environment and the resources would still be in a folder anyway - this didn't really solve the problem posed in the post.
No, application virtualization doesn't require that at all! You simply embed the files (after compiling) to the executable and to the application, the files are still in their native paths, where in truth, they are embedded in the exe.
Don't confuse application virtualization with virtual machines.
The end user won't notice any difference, nor has to do additional steps to install the program.
In fact, you could even make an application portable by virtualizing certain areas.
Reply With Quote
Reply


Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off

Forum Jump

Advertisement:





Free Publications
The ASP.NET 2.0 Anthology
101 Essential Tips, Tricks & Hacks - Free 156 Page Preview. Learn the most practical features and best approaches for ASP.NET.
subscribe
Programmers Heaven C# School Book -Free 338 Page eBook
The Programmers Heaven C# School book covers the .NET framework and the C# language.
subscribe
Build Your Own ASP.NET 3.5 Web Site Using C# & VB, 3rd Edition - Free 219 Page Preview!
This comprehensive step-by-step guide will help get your database-driven ASP.NET web site up and running in no time..
subscribe
Iterate folders in my Resources folder.
Iterate folders in my Resources folder.
Iterate folders in my Resources folder. Iterate folders in my Resources folder.
Iterate folders in my Resources folder.
Iterate folders in my Resources folder.
Iterate folders in my Resources folder. Iterate folders in my Resources folder. Iterate folders in my Resources folder. Iterate folders in my Resources folder. Iterate folders in my Resources folder. Iterate folders in my Resources folder. Iterate folders in my Resources folder.
Iterate folders in my Resources folder.
Iterate folders in my Resources folder.
 
Iterate folders in my Resources folder.
Iterate folders in my Resources folder.
 
-->