我是一名新手程序员,并且为满足自己的需求(具有更多功能)开发了一个记事本替换应用程序。当我双击.txt文件(或可自定义的扩展名,例如.abc)时,我希望该文件像记事本一样在我的应用程序中运行。我了解了与程序的文件关联,但了解得很少。
有人可以指出我该怎么做吗?请给我一些想法,我真的很想这样做,并为用户提供了一个将应用程序设置为默认文本编辑器的选项。帮助将不胜感激。
如果您使用单击一次部署(即在VS中为Windows exe在VS中“发布”),则可以指定文件关联那里,它将为您完成所有工作。不过,我无法保证它的优先级高于记事本。对于that,您可能需要适当的安装程序...
你去]
//Auto set default program to open edi format file
try
{
string currentSavedDefault = "";
using (RegistryKey key = Registry.ClassesRoot.OpenSubKey(@"filetype\shell\open\command"))
{
if (key != null)
{
currentSavedDefault = key.GetValue("").ToString();
}
}
String myExecutable = Assembly.GetEntryAssembly().Location;
if (currentSavedDefault != myExecutable)
{
Registry.ClassesRoot.CreateSubKey(".type").SetValue("", "filetype", Microsoft.Win32.RegistryValueKind.String);
Registry.ClassesRoot.CreateSubKey(@"filetype\shell\open\command").SetValue("", myExecutable + " %1", Microsoft.Win32.RegistryValueKind.String);
}
}
catch (Exception)
{
}