1. 以下是操作WMI的ActiveX的代码示例,实现的功能是列出浏览者机器上的磁盘驱动器。(当然这个控件以后还要实现浏览机器上所有文件的功能,但是该文中没有提供)
HelloWorld.cs
using System;
using System.Collections;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Windows.Forms;
using System.Management;
using System.Globalization;
using System.Runtime.InteropServices;

namespace K2ActiveX


{

/**//// <summary>
/// HelloWorld 的摘要说明。
/// </summary>
public class HelloWorld : System.Windows.Forms.UserControl

{
private System.Windows.Forms.TreeView tvFolders;
private System.Windows.Forms.ListView lvFiles;
private System.Windows.Forms.TextBox txtError;

/**//// <summary>
/// 必需的设计器变量。
/// </summary>
private System.ComponentModel.Container components = null;

public HelloWorld()

{
// 该调用是 Windows.Forms 窗体设计器所必需的。
InitializeComponent();

// Populate TreeView with Drive list
try

{
PopulateDriveList();
}
catch( System.Exception e )

{
txtError.Text = e.ToString();
}

}


Dispose( bool disposing ) // 清理所有正在使用的资源。#region Dispose( bool disposing ) // 清理所有正在使用的资源。

/**//// <summary>
/// 清理所有正在使用的资源。
/// </summary>
protected override void Dispose( bool disposing )

{
if( disposing )

{
if(components != null)

{
components.Dispose();
}
}
base.Dispose( disposing );
}

#endregion


组件设计器生成的代码#region 组件设计器生成的代码

/**//// <summary>
/// 设计器支持所需的方法 - 不要使用代码编辑器
/// 修改此方法的内容。
/// </summary>
private void InitializeComponent()

{
this.tvFolders = new System.Windows.Forms.TreeView();
this.lvFiles = new System.Windows.Forms.ListView();
this.txtError = new System.Windows.Forms.TextBox();
this.SuspendLayout();
//
// tvFolders
//
this.tvFolders.ImageIndex = -1;
this.tvFolders.Location = new System.Drawing.Point(0, 0);
this.tvFolders.Name = "tvFolders";
this.tvFolders.SelectedImageIndex = -1;
this.tvFolders.Size = new System.Drawing.Size(176, 472);
this.tvFolders.TabIndex = 0;
//
// lvFiles
//
this.lvFiles.Location = new System.Drawing.Point(184, 0);
this.lvFiles.Name = "lvFiles";
this.lvFiles.Size = new System.Drawing.Size(408, 344);
this.lvFiles.TabIndex = 1;
//
// txtError
//
this.txtError.Location = new System.Drawing.Point(184, 368);
this.txtError.Multiline = true;
this.txtError.Name = "txtError";
this.txtError.ScrollBars = System.Windows.Forms.ScrollBars.Vertical;
this.txtError.Size = new System.Drawing.Size(392, 96);
this.txtError.TabIndex = 2;
this.txtError.Text = "";
//
// HelloWorld
//
this.Controls.Add(this.txtError);
this.Controls.Add(this.lvFiles);
this.Controls.Add(this.tvFolders);
this.Name = "HelloWorld";
this.Size = new System.Drawing.Size(592, 480);
this.Load += new System.EventHandler(this.HelloWorld_Load);
this.ResumeLayout(false);

}
#endregion


所有的私有方法#region 所有的私有方法


PopulateDriveList() // This procedure populate the TreeView with the Drive list#region PopulateDriveList() // This procedure populate the TreeView with the Drive list

/**//// <summary>
/// This procedure populate the TreeView with the Drive list
/// </summary>
private void PopulateDriveList()

{
TreeNode nodeTreeNode;
int imageIndex = 0;
int selectIndex = 0;

const int Removable = 2;
const int LocalDisk = 3;
const int Network = 4;
const int CD = 5;
//const int RAMDrive = 6;
this.Cursor = Cursors.WaitCursor;
//clear TreeView
tvFolders.Nodes.Clear();
nodeTreeNode = new TreeNode("My Computer",0,0);
tvFolders.Nodes.Add(nodeTreeNode);

//set node collection
TreeNodeCollection nodeCollection = nodeTreeNode.Nodes;

//Get Drive list
ManagementObjectCollection queryCollection = getDrives();
foreach ( ManagementObject mo in queryCollection)

{
switch (int.Parse( mo["DriveType"].ToString()))

{
case Removable: //removable drives
imageIndex = 5;
selectIndex = 5;
break;
case LocalDisk: //Local drives
imageIndex = 6;
selectIndex = 6;
break;
case CD: //CD rom drives
imageIndex = 7;
selectIndex = 7;
break;
case Network: //Network drives
imageIndex = 8;
selectIndex = 8;
break;
default: //defalut to folder
imageIndex = 2;
selectIndex = 3;
break;
}
//create new drive node
nodeTreeNode = new TreeNode(mo["Name"].ToString() + "\\" ,imageIndex,selectIndex);

//add new node
nodeCollection.Add(nodeTreeNode);

}

//Init files ListView
InitListView();

this.Cursor = Cursors.Default;

}


#endregion


getDrives() // Get drive collection#region getDrives() // Get drive collection

/**//// <summary>
/// Get drive collection
/// </summary>
/// <returns>queryCollection</returns>
protected ManagementObjectCollection getDrives()

{
//get drive collection
ManagementObjectSearcher query = new ManagementObjectSearcher("SELECT * From Win32_LogicalDisk ");
ManagementObjectCollection queryCollection = query.Get();
return queryCollection;
}


#endregion


InitListView() // Init List View#region InitListView() // Init List View


/**//// <summary>
/// Init List View
/// </summary>
protected void InitListView()

{
//init ListView control
lvFiles.Clear(); //clear control
//create column header for ListView
lvFiles.Columns.Add("Name",150,System.Windows.Forms.HorizontalAlignment.Left);
lvFiles.Columns.Add("Size",75, System.Windows.Forms.HorizontalAlignment.Right);
lvFiles.Columns.Add("Created", 140, System.Windows.Forms.HorizontalAlignment.Left);
lvFiles.Columns.Add("Modified", 140, System.Windows.Forms.HorizontalAlignment.Left);
}

#endregion

private void HelloWorld_Load(object sender, System.EventArgs e)

{
}

#endregion

}
}

HelloWorld.html:
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN">
<HTML>
<HEAD>
<TITLE> New Document </TITLE>
<META NAME="Generator" CONTENT="EditPlus">
<META NAME="Author" CONTENT="">
<META NAME="Keywords" CONTENT="">
<META NAME="Description" CONTENT="">
</HEAD>
<BODY>
<object id="helloworld" classid="http://localhost/K2ActiveX.dll#K2ActiveX.HelloWorld" Width="800" Height="600" VIEWASTEXT> </object>
</BODY>
</HTML>
浏览HelloWorld.Html,出现如下的错误:

怎么回事哪?
后来参考了
http://www.cnblogs.com/homer/archive/2005/01/04/86473.html
中的方法,为ActiveX控件加上GUID,运行结果就正常了,效果如下:

可是又出现了一个新问题,那就是当我在局域网中的其他机器上访问这个页面的时候,竟然无法显示:

晕,到底是什么原因阿
多亏csdn上兄弟的帮助:
Internet选项-->安全-->受信任的站点-->站点-->http://192.168.0.88
然后:
自定义级别-->对没有标记为安全的ActiveX控件进行初始化和脚本运行(启用)
下载未签名的ActiveX控件(启用)
这个问题算是解决了。在局域网可以正常显示了。
但是又出现了一个新问题:那就是当我把dll文件和html文件都拷贝到虚拟主机上以后,浏览html文件的时候是个红叉,设置了IE安全性也不行,
看起来光拷贝dll文件和html文件不行,虚拟主机上安装了.net框架,我注册dll文件又提示找不到程序的入口点,那么到底还需要做什么才能保证这个ActiveX控件在新的虚拟主机上也能正常工作哪?
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· 如何编写易于单元测试的代码
· 10年+ .NET Coder 心语,封装的思维:从隐藏、稳定开始理解其本质意义
· .NET Core 中如何实现缓存的预热?
· 从 HTTP 原因短语缺失研究 HTTP/2 和 HTTP/3 的设计差异
· AI与.NET技术实操系列:向量存储与相似性搜索在 .NET 中的实现
· 10年+ .NET Coder 心语 ── 封装的思维:从隐藏、稳定开始理解其本质意义
· 地球OL攻略 —— 某应届生求职总结
· 提示词工程——AI应用必不可少的技术
· Open-Sora 2.0 重磅开源!
· 周边上新:园子的第一款马克杯温暖上架