您的位置:首页 > 编程语言 > C#

使用C#在程序运行时创建文件关联

2008-09-22 11:20 429 查看
using System;

using System.Collections.Generic;

using System.Windows.Forms;

using Microsoft.Win32;

namespace AppC

{

static class Program

{

/// <summary>

/// 应用程序的主入口点。

/// </summary>

[STAThread]

static void Main(string[] args)

{

Application.EnableVisualStyles();

Application.SetCompatibleTextRenderingDefault(false);

/// 是否通过关联打开的软件

if (args.Length > 0)

Application.Run(new mainForm(args[0].ToString()));

else

{

string keyName;

string keyValue;

keyName = "WPCFile";

keyValue = "资源包文件";

RegistryKey isExCommand = null;

bool isCreateRegistry = true;

try

{

/// 检查 文件关联是否创建

isExCommand = Registry.ClassesRoot.OpenSubKey(keyName);

if (isExCommand == null)

{

isCreateRegistry = true;

}

else

{

if (isExCommand.GetValue("Create").ToString() == Application.ExecutablePath.ToString())

{

isCreateRegistry = false;

}

else

{

Registry.ClassesRoot.DeleteSubKeyTree(keyName);

isCreateRegistry = true;

}

}

}

catch (Exception)

{

isCreateRegistry = true;

}

/// 假如 文件关联 还没有创建,或是关联位置已被改变

if (isCreateRegistry)

{

try

{

RegistryKey key, keyico;

key = Registry.ClassesRoot.CreateSubKey(keyName);

key.SetValue("Create", Application.ExecutablePath.ToString());

keyico = key.CreateSubKey("DefaultIcon");

keyico.SetValue("", Application.ExecutablePath + ",0");

key.SetValue("", keyValue);

key = key.CreateSubKey("Shell");

key = key.CreateSubKey("Open");

key = key.CreateSubKey("Command");

/// 关联的位置

key.SetValue("", "/"" + Application.ExecutablePath.ToString() + "/" /"%1/"");

/// 关联的文件扩展名,

keyName = ".wpc";

keyValue = "WPCFile";

key = Registry.ClassesRoot.CreateSubKey(keyName);

key.SetValue("", keyValue);

}

catch (Exception)

{

}

}

Application.Run(new mainForm(""));

}

}

}

}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: 
相关文章推荐