0

Using a ListView, I need to display (large) icons for an array of executables.

Is there a standard way of doing this / a "Pattern" (whether Design or otherwise)?

A wrinkle: these .exes should be runnable from this ListView ONLY. If a person were to navigate to the .exe via Explorer, they should not be able to run them from there. IOW, the user must log in to the system before seeing the array of program icons (and what they see will depend on their role)*, and that is the ONLY gateway to run those apps.

  • So, these app icons must be added programmatically.

Ideas?

Update:

Trying to use the code below to create a "Quick and Dirty" app.

Here's my code:

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.IO;

namespace ListViewWithAppIcons {
    public partial class Form1 : Form {
        public Form1() {
            InitializeComponent();
        }

        private void Form1_Load(object sender, EventArgs e) {
            DirectoryInfo dir = new DirectoryInfo(@"C:\SpotRun");
            foreach (FileInfo file in dir.GetFiles()) {
                try {
                    this.imageList1.Images.Add(Image.FromFile(file.FullName)); 
                } catch {
                    Console.WriteLine("This is not a Duck-billed Platypus");
                }
            }
            this.listView1.View = View.LargeIcon;
            this.imageList1.ImageSize = new Size(32, 32);
            this.listView1.LargeImageList = this.imageList1;
            //or 
            //this.listView1.View = View.SmallIcon; 
            //this.listView1.SmallImageList = this.imageList1; 

            for (int j = 0; j < this.imageList1.Images.Count; j++) {
                ListViewItem item = new ListViewItem();
                item.ImageIndex = j;
                this.listView1.Items.Add(item);
            } 

        }
    }
}

..and here's the "code generated by a tool" (not me, the other tool):

namespace ListViewWithAppIcons {
    partial class Form1 {
        /// <summary>
        /// Required designer variable.
        /// </summary>
        private System.ComponentModel.IContainer components = null;

        /// <summary>
        /// Clean up any resources being used.
        /// </summary>
        /// <param name="disposing">true if managed resources should be disposed; otherwise, false.</param>
        protected override void Dispose(bool disposing) {
            if (disposing && (components != null)) {
                components.Dispose();
            }
            base.Dispose(disposing);
        }

        #region Windows Form Designer generated code

        /// <summary>
        /// Required method for Designer support - do not modify
        /// the contents of this method with the code editor.
        /// </summary>
        private void InitializeComponent() {
            this.components = new System.ComponentModel.Container();
            this.listView1 = new System.Windows.Forms.ListView();
            this.imageList1 = new System.Windows.Forms.ImageList(this.components);
            this.SuspendLayout();
            // 
            // listView1
            // 
            this.listView1.Dock = System.Windows.Forms.DockStyle.Fill;
            this.listView1.Location = new System.Drawing.Point(0, 0);
            this.listView1.Name = "listView1";
            this.listView1.Size = new System.Drawing.Size(555, 408);
            this.listView1.TabIndex = 0;
            this.listView1.UseCompatibleStateImageBehavior = false;
            // 
            // imageList1
            // 
            this.imageList1.ColorDepth = System.Windows.Forms.ColorDepth.Depth8Bit;
            this.imageList1.ImageSize = new System.Drawing.Size(16, 16);
            this.imageList1.TransparentColor = System.Drawing.Color.Transparent;
            // 
            // Form1
            // 
            this.AutoScaleDimensions = new System.Drawing.SizeF(6F, 13F);
            this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font;
            this.ClientSize = new System.Drawing.Size(555, 408);
            this.Controls.Add(this.listView1);
            this.Name = "Form1";
            this.Text = "Form1";
            this.Load += new System.EventHandler(this.Form1_Load);
            this.ResumeLayout(false);

        }

        #endregion

        private System.Windows.Forms.ListView listView1;
        private System.Windows.Forms.ImageList imageList1;
    }
}
B. Clay Shannon-B. Crow Raven
  • 8,547
  • 144
  • 472
  • 862
  • break it down into several questions. 1. the list view show us the code you have that doesn't work. 2. There is no "patterns" to populating a listview with rows that contain icons, its just binding the listview to a datasource. 3. exe's only runnable from this ListView is a separate question all together. ps I didn't downvote you but am not going to get you back to zero – Jeremy Thompson Mar 28 '12 at 00:12
  • It would take a lot more than one upvote to get me back to zero, my friend. – B. Clay Shannon-B. Crow Raven Mar 28 '12 at 03:55
  • I dont' have code that doesn't work, I was just wondering if the problem had already been solved by cleverer folks. I'm a fair bit of the way there, now, but not convinced my way is "very sporting" (random/superfluous Princess Bride reference). – B. Clay Shannon-B. Crow Raven Mar 28 '12 at 03:57

1 Answers1

1

Hi Clay, I need to reference a superfluous Princess Bride!

An example off MSDN so you know your coding it up sweet:

private void Form_Load(object sender, EventArgs e)
{
    DirectoryInfo dir = new DirectoryInfo(@"c:\pic");
    foreach (FileInfo file in dir.GetFiles())
    {
        try
        {
            this.imageList1.Images.Add(Image.FromFile(file.FullName));
        }
        catch
        {
            Console.WriteLine("This is not an image file");
        }
    }
    this.listView1.View = View.LargeIcon;
    this.imageList1.ImageSize = new Size(32, 32);
    this.listView1.LargeImageList = this.imageList1;
    //or
    //this.listView1.View = View.SmallIcon;
    //this.listView1.SmallImageList = this.imageList1;

    for (int j = 0; j < this.imageList1.Images.Count; j++)
    {
        ListViewItem item = new ListViewItem();
        item.ImageIndex = j;
        this.listView1.Items.Add(item);
    }
}

To prevent people from opening the exe's (except from your program) is pretty hard, it would be easy if you wrote all those applications and could require a secret argument (as in like a command line arg) be passed in to start the other applications. But using Process Monitor/Explorer users could find out the secret key.

Alternatively you could hide the exe's in some folder but the trick here is that the name of the exe will be shown in Task Manager and once users see this they could search for the exe. I mean you could get around this using my technique here, but how deep is the rabits hole: How to hide C# application from taskmanager processtab? - see my answer, its got the most votes.

Maybe a better solution than all this mucking around hiding stuf from users would be - Kiosk mode: http://support.microsoft.com/kb/555463

Community
  • 1
  • 1
Jeremy Thompson
  • 61,933
  • 36
  • 195
  • 321
  • This does have some useful code, I think, but I took it and made the simplest possible app (all I did was add a ListView and an ImageList, changed the directory (to one that only contains two files, and added "using system.IO" to get it to compile. On the first iteration through the foreach loop, it fails with an OutOfMemoryException. It doesn't show if you just run the app (the ListView simply remains empty), but stepping through it, the "catch" is reached, and that is the exception shown. – B. Clay Shannon-B. Crow Raven Mar 28 '12 at 22:13
  • you can email me the solution & files, my email is in my profile or simply post the code that we can use to reproduce the error into another question. Thanks Clay – Jeremy Thompson Mar 28 '12 at 22:16
  • I looked at the Kiosk mode, which would be great for certain scenarios, but the users will need their computers for "other stuff," too - not just these apps. – B. Clay Shannon-B. Crow Raven Mar 28 '12 at 23:26
  • 1
    Okay, I'll post all from my "Quick and Dirty" project and see what you think. – B. Clay Shannon-B. Crow Raven Mar 28 '12 at 23:27