none
windows桌面上双击某类型的文件,并用我编写的wpf程序打开。我编写的wpf要怎么接收被双击打开的文件的文件路径? RRS feed

  • 问题

  • 我将某个的文件格式默认打开方式设为我编写的wpf程序。当然这个文件类型肯定不会是exe文件类型。

    现在我想在点击该格式的文件,并打开我编写的wpf程序,我编写wpf程序被以双击文件的形式打开时,要怎么接收被双击文件打开的路径。并根据这个文件路径做些处理。

    粗体字所描述的要怎么做?

    比如桌面上双击文件类型为".xxx"的文件,双击后用我的wpf程序打开,并同时获取被双击文件的路径。然后用MessageBox.Show输出该路径。











    2016年11月15日 9:43

答案


  • Hi 轮回的齿轮,

    >>比如桌面上双击文件类型为".xxx"的文件,双击后用我的wpf程序打开,并同时获取被双击文件的路径。然后用MessageBox.Show输出该路径。

    你可以试试下面的代码:

    1: 在 App.xaml 中增加一个Startup 事件处理。

    <Application x:Class="WpfApplication1.App"
                 xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
                 xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
                 StartupUri="MainWindow.xaml"
                 Startup="Application_Startup"> 
        <Application.Resources>
    
        </Application.Resources>
    </Application>

    2: 处理,获取文件路径(你可以用一个全局来保存)

     private void Application_Startup(object sender, StartupEventArgs e)
            {
                if (e.Args.Length == 1)
                {
                    FileInfo file = new FileInfo(e.Args[0]);
                    if (file.Exists) 
                    {
                        MessageBox.Show(file.FullName);
                    }
                }
            }


    Best Regards,

    Yohann Lu


    MSDN Community Support
    Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.

    2016年11月16日 6:55
    版主

  • Hi 轮回的齿轮,

    我这边是可以正常运行(截图所示)。

    你的错误: 'App'不包含'Application_Startup'的定义,并且没有找到接受类型'App'的第一个参数的扩展方法'Application_Startup'(是否缺少using指令或程序集引用?

    从你的错误提示来看,你并没有在App.xaml.cs 中 定义Application_Startup方法,你需要增加处理的方法。

    我如果注释下面的Application_Startup方法,会和你报一样的错误。

      public partial class App : Application
        {
            private void Application_Startup(object sender, StartupEventArgs e)
            {
                if (e.Args.Length == 1) //make sure an argument is passed
                {
                    FileInfo file = new FileInfo(e.Args[0]);
                    if (file.Exists) //make sure it's actually a file
                    {
                        //Do whatever
                        MessageBox.Show(file.FullName);
                    }
                }
            }
    
            public App()
            {
                DispatcherUnhandledException += App_DispatcherUnhandledException;
            }
    
            void App_DispatcherUnhandledException(object sender, System.Windows.Threading.DispatcherUnhandledExceptionEventArgs e)
            {
                MessageBox.Show("Error encountered:" + Environment.NewLine + e.Exception.Message);
                e.Handled = true;
            }
        }

    Best Regards,

    Yohann Lu



    MSDN Community Support
    Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.


    2016年11月17日 1:43
    版主

全部回复


  • Hi 轮回的齿轮,

    >>比如桌面上双击文件类型为".xxx"的文件,双击后用我的wpf程序打开,并同时获取被双击文件的路径。然后用MessageBox.Show输出该路径。

    你可以试试下面的代码:

    1: 在 App.xaml 中增加一个Startup 事件处理。

    <Application x:Class="WpfApplication1.App"
                 xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
                 xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
                 StartupUri="MainWindow.xaml"
                 Startup="Application_Startup"> 
        <Application.Resources>
    
        </Application.Resources>
    </Application>

    2: 处理,获取文件路径(你可以用一个全局来保存)

     private void Application_Startup(object sender, StartupEventArgs e)
            {
                if (e.Args.Length == 1)
                {
                    FileInfo file = new FileInfo(e.Args[0]);
                    if (file.Exists) 
                    {
                        MessageBox.Show(file.FullName);
                    }
                }
            }


    Best Regards,

    Yohann Lu


    MSDN Community Support
    Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.

    2016年11月16日 6:55
    版主

  • Hi 轮回的齿轮,

    严重性 代码 说明 项目 文件 禁止显示状态
    错误 'App' does not contain a definition for 'Application_Startup' and no extension method 'Application_Startup' accepting a first argument of type 'App' could be found (are you missing a using directive or an assembly reference?) WpfApplication1 E:\WpfApplication11\WpfApplication1\App.xaml 7

    >>比如桌面上双击文件类型为".xxx"的文件,双击后用我的wpf程序打开,并同时获取被双击文件的路径。然后用MessageBox.Show输出该路径。

    你可以试试下面的代码:

    1: 在 App.xaml 中增加一个Startup 事件处理。

    <Application x:Class="WpfApplication1.App"
                 xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
                 xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
                 StartupUri="MainWindow.xaml"
                 Startup="Application_Startup"> 
        <Application.Resources>
    
        </Application.Resources>
    </Application>

    2: 处理,获取文件路径(你可以用一个全局来保存)

     private void Application_Startup(object sender, StartupEventArgs e)
            {
                if (e.Args.Length == 1)
                {
                    FileInfo file = new FileInfo(e.Args[0]);
                    if (file.Exists) 
                    {
                        MessageBox.Show(file.FullName);
                    }
                }
            }


    Best Regards,

    Yohann Lu


    MSDN Community Support
    Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.

    严重性 代码 说明 项目 文件 禁止显示状态
    错误 'App' does not contain a definition for 'Application_Startup' and no extension method 'Application_Startup' accepting a first argument of type 'App' could be found (are you missing a using directive or an assembly reference?)

    运行时报错,点击此错误指向 Startup="Application_Startup", 请求解决

    2016年11月16日 11:32

  • Hi 轮回的齿轮,

    我这边是可以正常运行(截图所示)。

    你的错误: 'App'不包含'Application_Startup'的定义,并且没有找到接受类型'App'的第一个参数的扩展方法'Application_Startup'(是否缺少using指令或程序集引用?

    从你的错误提示来看,你并没有在App.xaml.cs 中 定义Application_Startup方法,你需要增加处理的方法。

    我如果注释下面的Application_Startup方法,会和你报一样的错误。

      public partial class App : Application
        {
            private void Application_Startup(object sender, StartupEventArgs e)
            {
                if (e.Args.Length == 1) //make sure an argument is passed
                {
                    FileInfo file = new FileInfo(e.Args[0]);
                    if (file.Exists) //make sure it's actually a file
                    {
                        //Do whatever
                        MessageBox.Show(file.FullName);
                    }
                }
            }
    
            public App()
            {
                DispatcherUnhandledException += App_DispatcherUnhandledException;
            }
    
            void App_DispatcherUnhandledException(object sender, System.Windows.Threading.DispatcherUnhandledExceptionEventArgs e)
            {
                MessageBox.Show("Error encountered:" + Environment.NewLine + e.Exception.Message);
                e.Handled = true;
            }
        }

    Best Regards,

    Yohann Lu



    MSDN Community Support
    Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.


    2016年11月17日 1:43
    版主