使用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(""));
}
}
}
}
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(""));
}
}
}
}
相关文章推荐
- 使用C#在程序运行时创建文件关联
- 该文件没有程序与之关联来运行该操作。请在控制面板的目录选项中创建关联 解决代码
- 该文件没有程序与之关联来运行该操作。请在控制面板的目录选项中创建关联 解决代码
- CLR基础,CLR运行过程,使用dos命令创建、编译、运行C#文件,查看IL代码
- 该文件没有程序与之关联来执行该操作。请在控制面板的文件夹选项中创建关联 解决代码
- 使用.NET编辑器构建和运行C#程序
- 使用AIR创建目录搜索/文件搜索程序。
- C# 2.0:使用匿名方法、迭代程序和局部类来创建优雅的代码
- 使用.bat文件运行Java程序的一个示例
- 安装SQLSERVER2000时出现"以前的某个程序安装已在安装计算机上创建挂起的文件操作。运行安装程序之前必须重新启动计算机”错误
- C#使用file无法修改netware操作系统下的文件创建日期
- 使用Mutex实现程序单实例运行(c#)
- c#中SiteMap文件的创建及使用
- c# 获取当前运行程序文件,函数,行号
- SQLServer2000的"以前的某个程序安装已在安装计算机上创建挂起的文件操作。运行安装程序之前必须重新启动计算机"错误
- 以前的某个程序安装在计算机上创建挂起文件操作,运SQL_server卸载了重装,往往是装不了.在安装SQL200的时候有个提示出现“以前的某个程序安装在计算机上创建挂起文件操作,运行安装程序之前必须重新启动计算机”,重起以后同样是这样..是什么原因阿?"后
- 通过文件锁实现,程序开始运行时,先判断文件是否存在,若存在则表明该程序已经在运行了,如果不存在就用open函数创建该文件,程序退出时关闭文件并删除文件。
- win7里边使用telnet命令为什么提示telnet不是内部或外部命令,也不是可运行的程序或批处理文件
- 使用 SqlBulkCopy 大量复制文字文件之 C# 程序代码