C# 将我的应用程序设置为默认应用程序

发布于 2024-09-30 02:55:00 字数 208 浏览 1 评论 0原文

我是一名新手程序员,并根据自己的需要开发了一个记事本替换应用程序(具有更多功能)。当我双击 .txt 文件(或可自定义的扩展名,例如 .abc)时,我希望该文件像记事本一样在我的应用程序中运行。我读到了有关程序的文件关联的内容,但我没有得到太多内容。

有人能指出我该怎么做吗?请给我一些想法,我真的很想这样做,并为我的用户提供一个将应用程序设置为默认文本编辑器的选项。非常感谢您的帮助。

I'm a novice programmer and have developed a notepad replacement application for my own needs (with some more features). When I double click a .txt file (or a customizable extension such as .abc), I want the file to run in my application just like notepad. I read about File Association with programs but I didn't get much of it.

Can someone point me to how I would do this? Just give me a few ideas, I would really like to do this and have an option for my users to set the application as the default text editor. Help would be much appreciated.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

策马西风 2024-10-07 02:55:00

如果您使用单击一次部署(即在 VS 中“发布”Windows exe),您可以在那里指定文件关联,它会为您完成所有操作。不过,我不能保证它会优先于记事本。为此,您可能需要一个合适的安装程序...

MSDN显示如何实现

If you are using click-once deployment (i.e. "publish" in VS for a windows exe), you can specify file associations there, and it will do everything for you. I can't guarantee that it'll take precedence over notepad, though. For that you might need a proper installer...

MSDN shows how

旧夏天 2024-10-07 02:55:00

就这样吧

 //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)
        {
        }

There you go

 //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)
        {
        }
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文