简体   繁体   English

C#使用命令行参数启动app

[英]C# start app with command-line parameters

I have a small app( C# WPF ) that starts automatically with system, but i want that main window of my program will not shown when it starts from autorun(with command-line parameter autorun ). 我有一个小型应用程序( C# WPF ),它可以自动启动系统,但我希望我的程序的主窗口从autorun启动时不显示(使用命令行参数autorun )。

I written this code: 我写了这段代码:

    protected override void OnStartup(StartupEventArgs e)
    {

        if (e.Args.Length == 0) 
            this.Run(new MainWindow());

        base.OnStartup(e);

    }

But it didn't work... So how i can check existence of my autorun parameter from App.xaml.cs and prevent opening MainWindow ? 但它没有用......所以我如何从App.xaml.cs检查我的autorun参数的存在并阻止打开MainWindow

Thanks. 谢谢。

Find StartupUri attribute at the top of your App.xaml file and remove it: 在App.xaml文件的顶部找到StartupUri属性并将其删除:

override the OnStartup as below override OnStartup ,如下所示

protected override void OnStartup(StartupEventArgs e)
{
    base.OnStartup(e);

    if (e.Args.Length == 0)
    {
        // no argument 
        // do stuff 
    }
    else
    {
        // with arguments
        // do stuff 
    }
    this.Shutdown();
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM