如何在C#中设置文件类型关联

发布于 2024-12-27 07:09:55 字数 202 浏览 4 评论 0 原文

我有一个 MDI 应用程序。该 MDI 应用程序还有许多其他工具,包括编辑器。我想使用 MDI 应用程序的编辑器打开所有“.txt”文件,从而使我的应用程序成为所有“.txt”文件的默认查看器。

每当用户编辑“.txt”文件时,MDI 应用程序都应启动,并且编辑器窗口应填充所选“.txt”文件的内容。

请问有什么办法可以做到这一点吗?

谢谢

I have a MDI application. This MDI application has lots of other tools including an editor as well. I would like to open all the ".txt" files with the editor of my MDI application, thereby making my application as the default viewer of all the ".txt" files.

Whenever the user edits a ".txt" File the MDI application should launch and a editor window should be populated with the contents of the chosen ".txt" file .

Is there a way I can do that please.

Thanks

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

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

发布评论

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

评论(3

可爱咩 2025-01-03 07:09:55

在我的应用程序中,我在启动时执行此操作:

[System.Runtime.InteropServices.DllImport("Shell32.dll")]
private static extern int SHChangeNotify(int eventId, int flags, IntPtr item1, IntPtr item2);

private void GetDefaultsFromRegistry()
{
    if (Registry.GetValue("HKEY_CLASSES_ROOT\\MyApp", String.Empty, String.Empty) == null)
    {
        Registry.SetValue("HKEY_CURRENT_USER\\Software\\Classes\\MyApp", "", "My File Type");
        Registry.SetValue("HKEY_CURRENT_USER\\Software\\Classes\\MyApp", "FriendlyTypeName", "My Friendly Type Name");
        Registry.SetValue("HKEY_CURRENT_USER\\Software\\Classes\\MyApp\\shell\\open\\command", "", 
            "path\\to\\my\\app \"%1\"");
        Registry.SetValue("HKEY_CURRENT_USER\\Software\\Classes\\.ext", "", "MyApp");

        //this call notifies Windows that it needs to redo the file associations and icons
        SHChangeNotify(0x08000000, 0x2000, IntPtr.Zero, IntPtr.Zero);
    }
}

当然,将所有路径、名称和扩展名更改为您的应用程序。

不过,请务必小心弄乱用户的 .txt 文件关联。我在每次启动时检查分配,因为我的应用程序使用自定义类型并且仅部署在内部映像系统上。我不知道您的应用程序部署在哪里,但如果我从互联网上下载的某些随机实用程序不断更改我的 .txt 文件关联,我会非常恼火,而且我相信很多其他人也会如此。

In my application, I do this at launch:

[System.Runtime.InteropServices.DllImport("Shell32.dll")]
private static extern int SHChangeNotify(int eventId, int flags, IntPtr item1, IntPtr item2);

private void GetDefaultsFromRegistry()
{
    if (Registry.GetValue("HKEY_CLASSES_ROOT\\MyApp", String.Empty, String.Empty) == null)
    {
        Registry.SetValue("HKEY_CURRENT_USER\\Software\\Classes\\MyApp", "", "My File Type");
        Registry.SetValue("HKEY_CURRENT_USER\\Software\\Classes\\MyApp", "FriendlyTypeName", "My Friendly Type Name");
        Registry.SetValue("HKEY_CURRENT_USER\\Software\\Classes\\MyApp\\shell\\open\\command", "", 
            "path\\to\\my\\app \"%1\"");
        Registry.SetValue("HKEY_CURRENT_USER\\Software\\Classes\\.ext", "", "MyApp");

        //this call notifies Windows that it needs to redo the file associations and icons
        SHChangeNotify(0x08000000, 0x2000, IntPtr.Zero, IntPtr.Zero);
    }
}

With all paths, names, and extensions changed to your application, of course.

Do be careful with messing with the user's .txt file association, though. I check the assignment on every launch because my application is using a custom type and is only deployed on in-house imaged systems. I don't know where your application is being deployed, but I would be very annoyed if some random utility I downloaded from the internet was constantly changing my .txt file association, and I'm sure a lot of other people would be too.

零度℉ 2025-01-03 07:09:55

您需要更改此注册表项:

HKCR\txtfile\shell\open\command

将参数的默认值更改为程序末尾的%1。然后在您的程序中处理命令行参数以执行您想要的操作。

You need to change this reg key:

HKCR\txtfile\shell\open\command

Change the default value to your program with %1 at the end for parameters. Then in your program handle the command line args to do what you want with it.

岁吢 2025-01-03 07:09:55

文件关联可以通过编程方式完成,例如 这个或者可以通过像

File assocition can programmatically be done like this or it can be done by through registry key like this

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