如何使C#应用程序用作某些文件的默认程序? [英] How to make a C# Application work as the default program for certain files?

查看:60
本文介绍了如何使C#应用程序用作某些文件的默认程序?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的应用程序中有一个图片框。当我在Windows中打开图像时,我想使用自己的应用程序,而不是打开的默认程序来显示该图像,而是使该程序包含图片框来显示图像。

My application has a picture box in it. When I open an image in Windows, instead of the default program that opens to show that image, I want to use my own application and have that program containing the picture box show the image.

推荐答案

我最近做了此操作,尽管我使用了其他操作,而不是默认的打开操作。

I did this recently, though I used a different action, not the default Open action.

首先,您会发现扩展名的文件类型,例如.jpg:

First you find out file type of some extension, say .jpg:

var imgKey = Registry.ClassesRoot.OpenSubKey(".jpg")
var imgType = key.GetValue("");

然后,找出可执行文件的路径并构建命令字符串:

Then you find out the path to your executable and build the "command string":

String myExecutable = Assembly.GetEntryAssembly().Location;
String command = "\"" + myExecutable + "\"" + " \"%1\"";

并注册可执行文件以打开以下类型的文件:

And register your executable to open files of that type:

String keyName = imgType + @"\shell\Open\command";
using (var key = Registry.ClassesRoot.CreateSubKey(keyName)) {
    key.SetValue("", command);
}

这篇关于如何使C#应用程序用作某些文件的默认程序?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆