using Microsoft.Win32;
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
namespace Extensometer.BLL
{
public class FileTypeRegInfo
{
///
/// 目标类型文件的拓展名
///
public string ExtendName;
///
/// 目标文件类型说明
///
public string Description;
///
/// 目标类型文件关联的图标
///
public string IcoPath;
///
/// 打开目标类型文件的应用程序
///
public string ExePath;
public FileTypeRegInfo() { }
public FileTypeRegInfo(string extendName)
{
this.ExtendName = extendName;
}
}
public class FileTypeRegisterClass
{
///
/// 创建单例模型
///
private static FileTypeRegisterClass fileTypeRegister = new FileTypeRegisterClass();
private FileTypeRegisterClass()
{ }
public static FileTypeRegisterClass GetFileTypeRegister()
{
return fileTypeRegister;
}
///
/// RegisterFileType 使文件类型与对应的图标及应用程序关联起来。
///
private 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();
}
///
/// GetFileTypeRegInfo 得到指定文件类型关联信息
///
private 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;
}
///
/// UpdateFileTypeRegInfo 更新指定文件类型关联信息
///
private 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;
}
///
/// FileTypeRegistered 指定文件类型是否已经注册
///
private static bool FileTypeRegistered(string extendName)
{
RegistryKey softwareKey = Registry.ClassesRoot.OpenSubKey(extendName);
if (softwareKey != null)
{
return true;
}
return false;
}
///
/// 检查并创建项目类型
///
public void CheckAndCreateFileType()
{
if (!FileTypeRegisterClass.FileTypeRegistered(".Esm")) //如果文件类型没有注册,则进行注册
{
FileTypeRegInfo fileTypeRegInfo = new FileTypeRegInfo(".Esm"); //文件类型信息
fileTypeRegInfo.Description = "引伸计工程文件";
fileTypeRegInfo.ExePath = Application.ExecutablePath;
fileTypeRegInfo.ExtendName = ".Esm";
fileTypeRegInfo.IcoPath = Application.ExecutablePath; //文件图标使用应用程序的
FileTypeRegisterClass fileTypeRegister = new FileTypeRegisterClass(); //注册
FileTypeRegisterClass.RegisterFileType(fileTypeRegInfo);
Process[] process = Process.GetProcesses(); //重启Explorer进程,使更新立即生效
var p = (from proc in process
where proc.ProcessName.Equals("explorer")
select proc).FirstOrDefault();
p.Kill();
}
else
{
FileTypeRegInfo fileTypeRegInfo = GetFileTypeRegInfo(".Esm");
//若项目位置有变,则更新项目信息
if(fileTypeRegInfo.ExePath != Application.ExecutablePath)
{
UpdateFileType();
}
}
}
///
/// 更新项目类型
///
public void UpdateFileType()
{
FileTypeRegInfo fileTypeRegInfo = new FileTypeRegInfo(".Esm"); //文件类型信息
fileTypeRegInfo.Description = "引伸计工程文件";
fileTypeRegInfo.ExePath = Application.ExecutablePath;
fileTypeRegInfo.ExtendName = ".Esm";
fileTypeRegInfo.IcoPath = Application.ExecutablePath; //文件图标使用应用程序的
FileTypeRegisterClass fileTypeRegister = new FileTypeRegisterClass(); //注册
FileTypeRegisterClass.UpdateFileTypeRegInfo(fileTypeRegInfo);
Process[] process = Process.GetProcesses(); //重启Explorer进程,使更新立即生效
var p = (from proc in process
where proc.ProcessName.Equals("explorer")
select proc).FirstOrDefault();
p.Kill();
}
///
/// 文件路径转所在文件夹路径
///
///
///
public string FilePathToFlodePath(string FilePath)
{
string result = string.Empty;
string[] results = FilePath.Split('\\');
for (int i = 0; i < results.Count() - 1; i++)
{
if (i == 0)
{
result = results[0];
}
else
{
result = result + "\\" + results[i];
}
}
return result;
}
}
}