2010-07-12 65 views
2

正如标题所述。 我知道如何在C#中做到这一点,但是当试图用WPF来做到这一点时,我无法找到在程序启动时读取文件名的位置或内容。拖放.exe文件顶部的文件以获取文件信息

public static void Main(string[] args) 
{ 
    if (Path.GetExtension(args[0]) == ".avi" || Path.GetExtension(args[0]) == ".mkv") 
    { 
     string pathOfFile = Path.GetFileNameWithoutExtension(args[0]); 
     string fullPathOfFile = Path.GetFullPath(args[0]); 
     Application.EnableVisualStyles(); 
     Application.SetCompatibleTextRenderingDefault(false); 
     Application.Run(new Form1(pathOfFile, fullPathOfFile)); 
    } 
    else 
    { 
     MessageBox.Show("This is not a supported file, please try again.."); 
    } 
} 
+0

退房http://blogs.msdn.com/b/avip/archive/2008/10/27/wpf-supporting-command-line-arguments-and-file-extensions.aspx。它解释了如何阅读WPF中的命令行参数。 – 2010-07-12 17:52:19

回答

1

你需要找到一些切入点(如OnLoad事件你的主窗口),然后访问命令行参数,像这样:

string[] args = Environment.GetCommandLineArgs(); 
1

在解决方案中双击App.xaml文件资源管理器。您可以添加启动事件。它的e.Args属性使您可以访问命令行参数。

3

找到解决方案。感谢您的帮助:)

我会后我做了什么,如果任何人都需要它..

在App.xaml中,我增加Startup="Application_Startup

<Application 
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" 
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" 
    x:Class="WpfApplication1.App" 
    StartupUri="MainWindow.xaml" 
    Startup="Application_Startup"> 
    <Application.Resources> 
    <!-- Resources scoped at the Application level should be defined here. --> 
    </Application.Resources> 
</Application> 

而且在App.xaml.cs我添加这些行:

public static String[] mArgs; 

    private void Application_Startup(object sender, StartupEventArgs e) 
    { 
     if (e.Args.Length > 0) 
     { 
      mArgs = e.Args; 
     } 
    } 

最后,我不得不在MainWindow类中添加一些信息。

public MainWindow() 
{ 
    this.InitializeComponent(); 
    String[] args = App.mArgs; 
} 

为了得到你想要的数据,你用System.IO.Path

当心使用的语句。如果你只使用Ex。 Path.GetFileNameWithoutExtension您将收到参考错误。获取您的数据时使用System.IO.Path

+0

btw,'(e.Args.Length> 0)'没有意义 – abatishchev 2010-07-14 06:54:53

0

将文件从Windows拖放到EXE上时,将启动EXE并提供文件名作为命令行参数。

public MainWindow() 
{ 
    string[] args = Environment.GetCommandLineArgs(); 
    foreach (var s in args) 
    { 
     //do something with s (the file name) 
    } 
} 
1

我知道这个话题很古老,但对于寻找类似的东西的人来说,这可能会有帮助。我试图将这个功能添加到我的程序中(通过在EXE上放置一个文件来启动程序),解决方案非常简单,并且来自这里。 我的程序只是搞乱了Excel文件中的一些单元格。所以很明显,它应该运行并通过删除其上的excel文件来完成这些工作。 所以我加入到这个组件初始化后的形态构造和它完美的作品对我来说:

public Form1() 
    { 
     InitializeComponent(); 
     string[] args = Environment.GetCommandLineArgs(); 
     filePathTextBox.Text = (args.Length > 1 && (Path.GetExtension(args[1]) == ".xlsx" || 
      Path.GetExtension(args[1]) == ".xls")) ? args[1] : ""; 
    } 

我注意到,在args第一个参数是我的程序的路径,所以我测试,显然第二个参数是我放在exe文件的文件路径,这就是为什么我使用args[1]。 我想纠正如果我错了,但现在一切正常与我的程序工作。

1

您可以将此用于Mediaelement。

public MainWindow() 
    { 
     this.InitializeComponent();  
     doubleclickevent(); 
    } 

    private void doubleclickevent() 
     { 

     string[] args = Environment.GetCommandLineArgs(); 
     string[] arr = new string[] 
        { 
        ".MP4", 
        ".MKV", 
        ".AVI" 
        }; 

    foreach (string element in args) 
      { 
       foreach (string s in arr) 
       { 
        if (element.EndsWith(s)) 
        { 
        MediaElement.Source = new Uri(@element, UriKind.RelativeOrAbsolute); 
        MediaElement.LoadedBehavior = MediaState.Play; 
        } 
        else { return; } 

       } 
      } 

     }