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

问题
答案
-
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. -
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.- 已编辑 Yong LuMicrosoft contingent staff, Moderator 2016年11月17日 1:46
- 已标记为答案 轮回的齿轮 2016年11月17日 5:18
全部回复
-
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. -
严重性 代码 说明 项目 文件 行 禁止显示状态
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", 请求解决
-
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.- 已编辑 Yong LuMicrosoft contingent staff, Moderator 2016年11月17日 1:46
- 已标记为答案 轮回的齿轮 2016年11月17日 5:18