在ListView中显示exe图标的方法

时间:2012-03-27 17:56:41

标签: c# .net winforms listview listviewitem

使用ListView,我需要为可执行文件数组显示(大)图标。

有没有标准的方法来执行此操作/“模式”(无论是设计还是其他方式)?

皱纹:这些.exes应该只能从这个ListView运行。如果一个人通过资源管理器导航到.exe,他们应该无法从那里运行它们。 IOW,用户必须先登录系统才能看到程序图标数组(以及他们看到的内容取决于他们的角色)*,这是运行这些应用程序的唯一网关。

  • 因此,必须以编程方式添加这些应用程序图标。

想法?

更新

尝试使用以下代码创建“快速而肮脏”的应用。

这是我的代码:

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);
            } 

        }
    }
}

..这里是“工具生成的代码”(不是我,其他工具):

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;
    }
}

1 个答案:

答案 0 :(得分:1)

嗨克莱,我需要引用一个多余的公主新娘!

关闭MSDN的示例,以便您知道自己的编码很好:

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);
    }
}

为了防止人们打开exe(除了你的程序)是非常困难的,如果你编写所有这些应用程序并且可能需要一个秘密参数(如命令行arg)传入以启动它会很容易其他应用程序。但使用Process Monitor / Explorer用户可以找到密钥。

或者你可以在一些文件夹中隐藏exe,但这里的技巧是exe的名称将显示在任务管理器中,一旦用户看到它,他们就可以搜索exe。我的意思是你可以在这里使用我的技术解决这个问题,但是兔子洞的深度有多深:How to hide C# application from taskmanager processtab? - 看我的回答,得票最多。

也许比用户隐藏stuf的所有这些方法更好的解决方案是 - Kiosk模式:http://support.microsoft.com/kb/555463