将文件扩展名与 WPF 应用程序关联

发布于 2024-08-18 15:06:30 字数 115 浏览 4 评论 0原文

我有一个漂亮的小作业组织器,我想为其添加备份选项。但我不希望它位于普通的 xml 文件或其他文件中,因为可能会损坏文件。那么我怎样才能创建一个程序知道的文件扩展名,并且可以保存到并使用 .asog 文件扩展名打开呢?

I have a nice little assignment organizer that I want to add a backup option to. But I don't want it in an ordinary xml file or someother file because of the possibility of file corruption. So how can I make a file extension that the program knows and can save to and open with a .asog file extension?

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

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

发布评论

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

评论(3

死开点丶别碍眼 2024-08-25 15:06:30

试试这个:
Vista 如何生成文档的图标与我的应用程序关联?

接受的答案解释了图标和文件关联。

您的应用程序使用 WPF 并不重要。文件关联并不关心您的应用程序使用什么 GUI 框架。

Try this:
How does Vista generate the icon for documents associated to my application?

The accepted answer explains icons and file associations.

It doesn't matter that your app uses WPF. The file associations don't care what GUI framework your app uses.

念﹏祤嫣 2024-08-25 15:06:30

如果您想将扩展名 (.magi) 的文件与您的 WPF 应用程序关联,我建议您使用 InnoSetup 来做到这一点。

例如,我开发了一个名为 MAGI 的 WPF 应用程序。我们将一个图标与“.magi”文件相关联,当用户单击“.magi”文件时,它会启动应用程序并直接在应用程序中打开它。

在 Wpf 应用程序中打开文件扩展名


使用 InnoSetup 轻松修改注册表

只需在您的 iss 文件中添加此指令:

[Setup]
ChangesAssociations=yes

[Registry]
Root: HKCR; Subkey: ".magi"; ValueType: string; ValueName: ""; ValueData: "MyMAGIApplication"; Flags: uninsdeletevalue
Root: HKCR; Subkey: "MyMAGIApplication"; ValueType: string; ValueName: ""; ValueData: "Program MAGI"; Flags: uninsdeletekey
Root: HKCR; Subkey: "MyMAGIApplication\DefaultIcon"; ValueType: string; ValueName: ""; ValueData: "{app}\MAGI.EXE,0"
Root: HKCR; Subkey: "MyMAGIApplication\shell\open\command"; ValueType: string; ValueName: ""; ValueData: """{app}\MAGI.EXE"" ""%1"""

解析中的参数“Startup”方法

我们在主 Xaml 中使用 Startup 属性,以便像调用有用的 main 方法一样调用解析器。

<Application x:Class="MAGI.View.App"
         xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
         xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" 
         Startup="AppStartupMainMagi" >
</Application>

在后面的代码中我们添加这个方法

/// <summary>
/// Call with Startup property in App.xaml
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void AppStartupMainMAGI(object sender, StartupEventArgs e)
{
    String[] arguments = Environment.GetCommandLineArgs();

    if (arguments.GetLength(0) > 1)
    {
        if (arguments[1].EndsWith(".magi"))
        {
            string filePathFormMainArgs = arguments[1];
            if(isFileMagiValid(filePathFormMainArgs)) 
            {
                // Step 1 : deserialize filePathFormMainArgs
                // Step 2 : call the view "File oepn" in the application"
            }
        }
    }
    else {
        // Call the view "welcome page application"
    }
}

If you want to associate a file with extension (.magi) with your WPF application, I advise you to use InnoSetup to do that.

For example, I developed an WPF application called MAGI. We associate an icon to ".magi" file and when a user clicks on a ".magi" file it launches the application and opens it directly in the application.

Open file extension in Wpf application


Use InnoSetup to modify the registry easily

Just add this instructino in your iss file :

[Setup]
ChangesAssociations=yes

[Registry]
Root: HKCR; Subkey: ".magi"; ValueType: string; ValueName: ""; ValueData: "MyMAGIApplication"; Flags: uninsdeletevalue
Root: HKCR; Subkey: "MyMAGIApplication"; ValueType: string; ValueName: ""; ValueData: "Program MAGI"; Flags: uninsdeletekey
Root: HKCR; Subkey: "MyMAGIApplication\DefaultIcon"; ValueType: string; ValueName: ""; ValueData: "{app}\MAGI.EXE,0"
Root: HKCR; Subkey: "MyMAGIApplication\shell\open\command"; ValueType: string; ValueName: ""; ValueData: """{app}\MAGI.EXE"" ""%1"""

Parse the arguments in the "Startup" method

We use the Startup property in the main Xaml, in order to call your parser like a useful main method.

<Application x:Class="MAGI.View.App"
         xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
         xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" 
         Startup="AppStartupMainMagi" >
</Application>

And in the code-behind we add this method

/// <summary>
/// Call with Startup property in App.xaml
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void AppStartupMainMAGI(object sender, StartupEventArgs e)
{
    String[] arguments = Environment.GetCommandLineArgs();

    if (arguments.GetLength(0) > 1)
    {
        if (arguments[1].EndsWith(".magi"))
        {
            string filePathFormMainArgs = arguments[1];
            if(isFileMagiValid(filePathFormMainArgs)) 
            {
                // Step 1 : deserialize filePathFormMainArgs
                // Step 2 : call the view "File oepn" in the application"
            }
        }
    }
    else {
        // Call the view "welcome page application"
    }
}
梦开始←不甜 2024-08-25 15:06:30

您可以通过安装项目或 ClickOnce 安装添加文件扩展名。设置完成后,用户可以双击 .asog 文件,您的应用程序将被调用,文件名作为 main 参数数组中的第一个条目。

You can add a file extension with either a Setup project or a ClickOnce install. Once you have it setup, a user can double click on a .asog file and your app will be invoked with the filename as the first entry in the arguments array of main.

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