4

正如标题所说。我只知道如何在 C# 中执行此操作,但是当尝试使用 WPF 执行此操作时,我无法找到在程序启动时添加读取文件名的位置或内容。

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..");
    }
}
4

6 回答 6

4

找到了解决方案。谢谢你的帮助 :)

如果其他人需要,我会发布我所做的..

在 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

注意 Using 语句。如果你只使用 Ex。Path.GetFileNameWithoutExtension你会得到一个参考错误。System.IO.Path获取数据时使用。

于 2010-07-14T06:50:04.997 回答
2

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

string[] args = Environment.GetCommandLineArgs();
于 2010-07-12T17:52:03.010 回答
1

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

于 2010-07-12T18:27:50.493 回答
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; }

                }
            }

      }
于 2016-11-06T16:51:09.053 回答
1

我知道这个话题很老,但对于寻找类似内容的人来说,它可能会有所帮助。我试图将此功能添加到我的程序中(通过在 EXE 顶部放置一个文件来启动程序),解决方案非常简单,它来自这里。我的程序只是弄乱了 Excel 文件中的几个单元格。所以很明显,它应该运行并执行这些操作,只需将 excel 文件放到上面即可。所以我在组件初始化后将它添加到 Form 构造函数中,它对我来说完美无缺:

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]. 如果我错了,我希望得到纠正,但现在我的程序一切正常。

于 2016-11-06T14:22:41.267 回答
0

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

public MainWindow()
{
    string[] args = Environment.GetCommandLineArgs();
    foreach (var s in args)
    {
        //do something with s (the file name)
    }
}
于 2013-07-26T16:29:29.640 回答