System File Association(转)

Introduction
Ever wanted to programmatically associate a file type on the system with your application, but didn’t like the idea of digging through the registry yourself? If so, then this article and code are right for you.

Background
File associations in Windows have two parts, the extension itself and the ProgID (programmatic identifier). While an extension does not have to be associated with any ProgID, if it is, it can only be associated with a single one. On the other hand, a ProgID can have multiple extensions associated with it.

The attached code includes classes such as:

FileAssociationInfo: provides properties to determine (or set) what ProgID the extension is associated with (ProgID), what sort of file the system considers it to be (PerceivedType), the MIME type of the file (ContentType), and what programs will appear in the extensions (OpenWithList).
ProgramAssociationInfo: functions similarly to FileAssociationInfo and provides properties to set how the shell should handle the file type (EditFlags), the command verbs and programs the ProgID supports (Verbs), and the file types icon (DefaultIcon).
AssociationManager: provides a simplistic method to determine if certain extensions are associated with a given ProgID. It also provides the ability to associate those types or to create a brand new association between already specified extensions and a ProgID.
Examples
Our first step is to create an instance of the FileAssociationInfo class and specify the extension we wish to deal with into the constructor. Next we see if the extension already exists and if it doesn’t, we create it with the specified ProgID (MyProgramName), and then set up the optional ContentType and OpenWithList properties.

Hide Copy Code
FileAssociationInfo fai = new FileAssociationInfo(“.bob”);
if (!fai.Exists)
{
fai.Create(“MyProgramName”);

     //Specify MIME type (optional)
     fai.ContentType = "application/myfile";

     //Programs automatically displayed in open with list
     fai.OpenWithList = new string[]
    { "notepad.exe", "wordpad.exe", "someotherapp.exe" };
   }

Finally, we create an instance of the ProgramAssociationInfo class and specify the ProgID we wish to deal with in its constructor. Should this ProgID not exist, we create it and specify both a description for the program type (shared between all files using this ProgID) and the command verb that is used in selecting different ways to load the file.

Hide Copy Code
ProgramAssociationInfo pai = new ProgramAssociationInfo(fai.ProgID);
if (!pai.Exists)
{
pai.Create
(
//Description of program/file type
“My Program’s File Type”,

     new ProgramVerb
          (
          //Verb name
          "Open",
          //Path and arguments to use
          @"C:\SomePath\MyApp.exe %1"
          )
        );

     //optional
     pai.DefaultIcon = new ProgramIcon(@"C:\SomePath\SomeIcon.ico");
   }

Full sample
The link at the top of this article includes a simplistic GUI that demonstrates all the capabilities of the FileAssociationInfo and ProgramAssociationInfo classes.

Word of warning
This code requires administrative access (especially under Vista) when used to create or modify extensions, and on some systems, it also requires elevated permission to read.

License
This article has no explicit license attached to it but may contain usage terms in the article text or the download files themselves. If in doubt please contact the author via the discussion board below.

A list of licenses authors might use can be found here

咦 主程序?是E:\AI_System\还是E:\AI_System\agent还是E:\AI_System\agent\conscious_system啊? 这是之前的主体架构AI_System/ ├── agent/ # AI核心系统 (智能体的"大脑") │ ├── autonomous_agent.py # 主智能体 (自我意识) │ ├── cognitive_architecture.py # 认知架构 (思维过程) │ ├── communication_system.py # 通信系统 (语言能力) │ ├── affective_system.py # 情感系统 (情绪状态) │ ├── health_system.py # 健康系统 (能量管理) │ ├── memory_system.py # 记忆系统 (长期记忆) │ ├── model_manager.py # 模型管理 (技能学习) │ ├── environment_interface.py # 环境接口 (新增:感知外部世界) │ └── action_executor.py # 动作执行器 (新增:与环境交互) ├── core/ # 系统核心 │ ├── config.py # 系统配置 │ └── environment.py # 环境定义 (新增) ├── cognitive_arch │ ├── decision_system.py │ ├── life_scheduler.py # 生活系统 │ └── …其他模块 ├── web_ui/ # 用户界面 (智能体的"窗户") │ ├── server.py #主服务文件 │ ├── templates/ │ │ ├── agent_interface.html # 主界面 │ │ ├── file_browser.html # 文件浏览器 │ │ ├── life_dashboard.html # 生活仪表盘 │ │ └── environment_view.html # 环境视图 (新增) │ └── static/ # 静态文件 │ ├── css/ │ │ ├── style.css # 主样式 │ │ └── environment.css # 环境视图样式 (新增) │ └── js/ │ ├── app.js # 主逻辑 │ └── environment.js # 环境交互 (新增) ├── models/ # 模型存储 (智能体的"知识库") ├── resources/ # 资源目录 (新增:智能体的"物品") │ ├── skins/ # 皮肤/外观 │ ├── furniture/ # 虚拟家具 │ └── tools/ # 工具/插件 ├── logs/ # 系统日志 ├── .env # 环境配置 ├── start_system.bat # 系统启动脚本 └── environment.db # 环境数据库 (新增) 这是你最新添加到架构 你看看放哪 E:\AI_System\cognitive_arch ├── consciousness/ # 意识模块主目录 │ ├── __init__.py # 包初始化文件 │ ├── core_awareness.py # 核心自我意识功能 │ ├── meta_cognition.py # 元认知能力 │ ├── attention_control.py # 注意力机制 │ ├── emotional_model.py # 情感模拟 │ ├── goal_manager.py # 目标管理系统 │ └── integration.py # 模块整合接口 ├── decision_system.py # 决策系统(需与意识模块交互) ├── life_scheduler.py # 生活调度器(需与意识模块交互) └── memory_system.py # 记忆系统(需与意识模块交互) E:\AI_System\agent\conscious_system\ ├── __init__.py # 包初始化文件 ├── layers/ # 意识层次模块 │ ├── unconscious.py # 无意识层处理 │ ├── preconscious.py # 前意识层处理 │ └── conscious.py # 意识层核心处理 ├── cognitive/ # 认知处理模块 │ ├── processor.py # 认知处理器 │ └── workload_manager.py # 认知负载管理 ├── memory/ # 记忆系统 │ ├── memory_system.py # 记忆系统核心 │ └── emotional_association.py # 情感关联处理 ├── perception/ # 感知模块 │ └── stimulus.py # 刺激处理 ├── states/ # 状态管理 │ └── conscious_state.py # 意识状态管理 ├── conscious_framework.py # 意识框架 ├── conscious_layer.py # 意识层协调器 ├── conscious_memory.py # 意识记忆接口 ├── main.py # 系统入口 ├── perception_interface.py # 感知接口 └── self_awareness.py # 自我意识模块
最新发布
08-11
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值