WinUI3命令行参数无法正常工作。

huangapple go评论70阅读模式
英文:

winui3 command line arguments not working

问题

我正在进行一个WinUI3(打包)项目。

我正在尝试在启动新进程时传递参数
但由于某种原因,要么是启动进程的方法,要么是读取参数的方法不起作用。

这是我已经尝试过的多种方法。

启动进程

Process.Start(Environment.ProcessPath, StartupMethod.UPLOAD);

或者

ProcessStartInfo processStartInfo = new()
{
    FileName = Environment.ProcessPath,
    Arguments = StartupMethod.UPLOAD
};

Process.Start(processStartInfo);

读取参数

string[] arguments = args.Arguments.Split(" ", StringSplitOptions.RemoveEmptyEntries);

或者

string[] arguments = Environment.GetCommandLineArgs();

但似乎没有一种组合起作用。

此外,我尝试手动运行Environment.ProcessPath,但由于某种原因,.exe文件没有运行。

英文:

I am working on a winui3 (packaged) project.

I am trying to pass arguments while starting a new process
But for some reason, either the process starting method or arguments reading method isn't working.

These are the multiple ways I already tried.

To start the process

Process.Start(Environment.ProcessPath, StartupMethod.UPLOAD);

or

ProcessStartInfo processStartInfo = new()
{
    FileName = Environment.ProcessPath,
    Arguments = StartupMethod.UPLOAD
};

Process.Start(processStartInfo);

To read the arguments

string[] arguments = args.Arguments.Split(" ", StringSplitOptions.RemoveEmptyEntries);

or

string[] arguments = Environment.GetCommandLineArgs();

But none of the combinations seems to be working.

Also, I tried to run the Environment.ProcessPath manually, but the .exe file isn't running for some reason.

答案1

得分: 0

开始这个过程

Process.Start(Environment.ProcessPath, "参数");

读取参数

string[] 参数 = Environment.GetCommandLineArgs();
// `Environment.GetCommandLineArgs()` 返回的第一个位置是文件名,所以可能需要跳过它
英文:

To start the process

Process.Start(Environment.ProcessPath, "Arguments");

To read the arguments

string[] arguments = Environment.GetCommandLineArgs();

The Environment.GetCommandLineArgs() return filename at the first position, so might want to skip that

答案2

得分: 0

这对我来说有效。

开始进程

Process P = new();
P.StartInfo.UseShellExecute = true;
P.StartInfo.Verb = "runas";
P.StartInfo.FileName = Environment.ProcessPath;
P.StartInfo.Arguments = "这是一个测试"; 
P.Start();

在App.xaml.cs中读取参数

protected override void OnLaunched(Microsoft.UI.Xaml.LaunchActivatedEventArgs args)
{
    m_window = new MainWindow();
    m_window.Activate();

    string[] arguments = Environment.GetCommandLineArgs();
    if (arguments.Length > 1)
    {
        //做一些事情
    }                
}
英文:

This works for me.

Start the process

Process P = new();
P.StartInfo.UseShellExecute = true;
P.StartInfo.Verb = "runas";
P.StartInfo.FileName = Environment.ProcessPath;
P.StartInfo.Arguments = "This is a Test"; 
P.Start();

Read the arguments in App.xaml.cs

protected override void OnLaunched(Microsoft.UI.Xaml.LaunchActivatedEventArgs args)
{
    m_window = new MainWindow();
    m_window.Activate();

    string[] arguments = Environment.GetCommandLineArgs();
    if (arguments.Length > 1)
    {
        //do something
    }
            
}

huangapple
  • 本文由 发表于 2023年4月6日 20:43:31
  • 转载请务必保留本文链接:https://go.coder-hub.com/75949672.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定