请教一个问题,关于自定义格式的文件 C#,文件 解决方案 » 免费领取超大流量手机卡,每月29元包185G流量+100分钟通话, 中国电信官方发货 系统中可以设置某个后缀的文件默认以哪个程序打开,也就是说你双击你的.kgp文件的时候系统会根据.kgp去找设定的默认打开这类文件的程序参考:http://jingyan.baidu.com/article/c45ad29cf71476051753e219.html 原文: http://blog.csdn.net/jiutao_tang/article/details/6563646C#编程实现自定义文件类型关联应用程序在我们自己编写的应用中,经常会用自定义类型的文件的来保存与应用相关的数据,比如.xcf文件就是XCodeFactory应用程序的项目文件。如果没 有向Windows注册表注册该文件类型,那么.xcf文件的图标将是windows的文件默认图标,并且你双击一个a.xcf文件,也不会自动启动 XCodeFactory应用程序来加载a.xcf文件。如何使.xcf文件的图标变成我自己喜爱的图标、如何完成像点击.doc文件就自动打开word 程序的功能,下面将告诉你解决方案。 我们可以通过手动修改注册表来完成上述任务,更好的方式是,通过程序来实现。这样,在安装应用程序时,就可以自动的注册自定义文件类型 了。我通过FileTypeRegister静态类来完成这些功能。首先,将注册需要用到的信息封装成FileTypeRegInfo,定义如下:public class FileTypeRegInfo{ /// <summary> /// 目标类型文件的扩展名 /// </summary> public string ExtendName; //".xcf" /// <summary> /// 目标文件类型说明 /// </summary> public string Description; //"XCodeFactory项目文件" /// <summary> /// 目标类型文件关联的图标 /// </summary> public string IcoPath; /// <summary> /// 打开目标类型文件的应用程序 /// </summary> public string ExePath; public FileTypeRegInfo() { } public FileTypeRegInfo(string extendName) { this.ExtendName = extendName; }}/// <summary> /// FileTypeRegister 用于注册自定义的文件类型。 /// zhuweisky 2005.08.31 /// </summary> public class FileTypeRegister { #region RegisterFileType /// <summary> /// RegisterFileType 使文件类型与对应的图标及应用程序关联起来。 /// </summary> public static void RegisterFileType(FileTypeRegInfo regInfo) { if (FileTypeRegistered(regInfo.ExtendName)) { return; } string relationName = regInfo.ExtendName.Substring(1, regInfo.ExtendName.Length - 1).ToUpper() + "_FileType"; RegistryKey fileTypeKey = Registry.ClassesRoot.CreateSubKey(regInfo.ExtendName); fileTypeKey.SetValue("", relationName); fileTypeKey.Close(); RegistryKey relationKey = Registry.ClassesRoot.CreateSubKey(relationName); relationKey.SetValue("", regInfo.Description); RegistryKey iconKey = relationKey.CreateSubKey("DefaultIcon"); iconKey.SetValue("", regInfo.IcoPath); RegistryKey shellKey = relationKey.CreateSubKey("Shell"); RegistryKey openKey = shellKey.CreateSubKey("Open"); RegistryKey commandKey = openKey.CreateSubKey("Command"); commandKey.SetValue("", regInfo.ExePath + " %1"); relationKey.Close(); } /// <summary> /// GetFileTypeRegInfo 得到指定文件类型关联信息 /// </summary> public static FileTypeRegInfo GetFileTypeRegInfo(string extendName) { if (!FileTypeRegistered(extendName)) { return null; } FileTypeRegInfo regInfo = new FileTypeRegInfo(extendName); string relationName = extendName.Substring(1, extendName.Length - 1).ToUpper() + "_FileType"; RegistryKey relationKey = Registry.ClassesRoot.OpenSubKey(relationName); regInfo.Description = relationKey.GetValue("").ToString(); RegistryKey iconKey = relationKey.OpenSubKey("DefaultIcon"); regInfo.IcoPath = iconKey.GetValue("").ToString(); RegistryKey shellKey = relationKey.OpenSubKey("Shell"); RegistryKey openKey = shellKey.OpenSubKey("Open"); RegistryKey commandKey = openKey.OpenSubKey("Command"); string temp = commandKey.GetValue("").ToString(); regInfo.ExePath = temp.Substring(0, temp.Length - 3); return regInfo; } /// <summary> /// UpdateFileTypeRegInfo 更新指定文件类型关联信息 /// </summary> public static bool UpdateFileTypeRegInfo(FileTypeRegInfo regInfo) { if (!FileTypeRegistered(regInfo.ExtendName)) { return false; } string extendName = regInfo.ExtendName; string relationName = extendName.Substring(1, extendName.Length - 1).ToUpper() + "_FileType"; RegistryKey relationKey = Registry.ClassesRoot.OpenSubKey(relationName, true); relationKey.SetValue("", regInfo.Description); RegistryKey iconKey = relationKey.OpenSubKey("DefaultIcon", true); iconKey.SetValue("", regInfo.IcoPath); RegistryKey shellKey = relationKey.OpenSubKey("Shell"); RegistryKey openKey = shellKey.OpenSubKey("Open"); RegistryKey commandKey = openKey.OpenSubKey("Command", true); commandKey.SetValue("", regInfo.ExePath + " %1"); relationKey.Close(); return true; } /// <summary> /// FileTypeRegistered 指定文件类型是否已经注册 /// </summary> public static bool FileTypeRegistered(string extendName) { RegistryKey softwareKey = Registry.ClassesRoot.OpenSubKey(extendName); if (softwareKey != null) { return true; } return false; } #endregion }[STAThread]static void Main(string[] args){ if ((args != null) && (args.Length > 0)) { string filePath = ""; for (int i = 0; i < args.Length; i++) { filePath += " " + args[i]; } MainForm.XcfFilePath = filePath.Trim(); } Application.Run(new MainForm());} 网络路径问题,急! mapinfo 新建了一个临时图层,也显示出来了,为何用另外一个按纽去查找的时候说无法找到表X 关于dropdownlist的问题 安装的问题 大家帮帮看看 高手请进:One or more errors encountered while loading the designer该如何解决 如何使用C#模拟网站登陆过程 第二弹 怎么办?? 请教大家一个 WinForm 的开发模式: 关于formulaOne控件的问题 TreeView Web数据绑定的问题? C# 怎么杀死线程 字符串格式化问题
C#编程实现自定义文件类型关联应用程序在我们自己编写的应用中,经常会用自定义类型的文件的来保存与应用相关的数据,比如.xcf文件就是XCodeFactory应用程序的项目文件。如果没 有向Windows注册表注册该文件类型,那么.xcf文件的图标将是windows的文件默认图标,并且你双击一个a.xcf文件,也不会自动启动 XCodeFactory应用程序来加载a.xcf文件。如何使.xcf文件的图标变成我自己喜爱的图标、如何完成像点击.doc文件就自动打开word 程序的功能,下面将告诉你解决方案。
我们可以通过手动修改注册表来完成上述任务,更好的方式是,通过程序来实现。这样,在安装应用程序时,就可以自动的注册自定义文件类型 了。我通过FileTypeRegister静态类来完成这些功能。首先,将注册需要用到的信息封装成FileTypeRegInfo,定义如下:public class FileTypeRegInfo
{
/// <summary>
/// 目标类型文件的扩展名
/// </summary>
public string ExtendName; //".xcf"
/// <summary>
/// 目标文件类型说明
/// </summary>
public string Description; //"XCodeFactory项目文件"
/// <summary>
/// 目标类型文件关联的图标
/// </summary>
public string IcoPath;
/// <summary>
/// 打开目标类型文件的应用程序
/// </summary>
public string ExePath;
public FileTypeRegInfo()
{
}
public FileTypeRegInfo(string extendName)
{
this.ExtendName = extendName;
}
}
/// <summary>
/// FileTypeRegister 用于注册自定义的文件类型。
/// zhuweisky 2005.08.31
/// </summary>
public class FileTypeRegister
{
#region RegisterFileType
/// <summary>
/// RegisterFileType 使文件类型与对应的图标及应用程序关联起来。
/// </summary>
public static void RegisterFileType(FileTypeRegInfo regInfo)
{
if (FileTypeRegistered(regInfo.ExtendName))
{
return;
}
string relationName = regInfo.ExtendName.Substring(1, regInfo.ExtendName.Length - 1).ToUpper() + "_FileType";
RegistryKey fileTypeKey = Registry.ClassesRoot.CreateSubKey(regInfo.ExtendName);
fileTypeKey.SetValue("", relationName);
fileTypeKey.Close();
RegistryKey relationKey = Registry.ClassesRoot.CreateSubKey(relationName);
relationKey.SetValue("", regInfo.Description);
RegistryKey iconKey = relationKey.CreateSubKey("DefaultIcon");
iconKey.SetValue("", regInfo.IcoPath);
RegistryKey shellKey = relationKey.CreateSubKey("Shell");
RegistryKey openKey = shellKey.CreateSubKey("Open");
RegistryKey commandKey = openKey.CreateSubKey("Command");
commandKey.SetValue("", regInfo.ExePath + " %1");
relationKey.Close();
}
/// <summary>
/// GetFileTypeRegInfo 得到指定文件类型关联信息
/// </summary>
public static FileTypeRegInfo GetFileTypeRegInfo(string extendName)
{
if (!FileTypeRegistered(extendName))
{
return null;
}
FileTypeRegInfo regInfo = new FileTypeRegInfo(extendName);
string relationName = extendName.Substring(1, extendName.Length - 1).ToUpper() + "_FileType";
RegistryKey relationKey = Registry.ClassesRoot.OpenSubKey(relationName);
regInfo.Description = relationKey.GetValue("").ToString();
RegistryKey iconKey = relationKey.OpenSubKey("DefaultIcon");
regInfo.IcoPath = iconKey.GetValue("").ToString();
RegistryKey shellKey = relationKey.OpenSubKey("Shell");
RegistryKey openKey = shellKey.OpenSubKey("Open");
RegistryKey commandKey = openKey.OpenSubKey("Command");
string temp = commandKey.GetValue("").ToString();
regInfo.ExePath = temp.Substring(0, temp.Length - 3);
return regInfo;
}
/// <summary>
/// UpdateFileTypeRegInfo 更新指定文件类型关联信息
/// </summary>
public static bool UpdateFileTypeRegInfo(FileTypeRegInfo regInfo)
{
if (!FileTypeRegistered(regInfo.ExtendName))
{
return false;
} string extendName = regInfo.ExtendName;
string relationName = extendName.Substring(1, extendName.Length - 1).ToUpper() + "_FileType";
RegistryKey relationKey = Registry.ClassesRoot.OpenSubKey(relationName, true);
relationKey.SetValue("", regInfo.Description);
RegistryKey iconKey = relationKey.OpenSubKey("DefaultIcon", true);
iconKey.SetValue("", regInfo.IcoPath);
RegistryKey shellKey = relationKey.OpenSubKey("Shell");
RegistryKey openKey = shellKey.OpenSubKey("Open");
RegistryKey commandKey = openKey.OpenSubKey("Command", true);
commandKey.SetValue("", regInfo.ExePath + " %1");
relationKey.Close();
return true;
}
/// <summary>
/// FileTypeRegistered 指定文件类型是否已经注册
/// </summary>
public static bool FileTypeRegistered(string extendName)
{
RegistryKey softwareKey = Registry.ClassesRoot.OpenSubKey(extendName);
if (softwareKey != null)
{
return true;
}
return false;
}
#endregion
}
[STAThread]
static void Main(string[] args)
{
if ((args != null) && (args.Length > 0))
{
string filePath = "";
for (int i = 0; i < args.Length; i++)
{
filePath += " " + args[i];
}
MainForm.XcfFilePath = filePath.Trim();
}
Application.Run(new MainForm());
}