C# 程序启动另外一个exe的时候传参数

C# 程序启动另外一个exe的时候传参数

一、传递一个参数

using System.Diagnostics;

public void StartAnotherProcessWithArguments()
{
    // 创建ProcessStartInfo实例
    ProcessStartInfo startInfo = new ProcessStartInfo();

    // 设置要执行的程序路径
    startInfo.FileName = @"C:\Path\To\Your\Executable.exe";

    // 设置传递给程序的参数
    startInfo.Arguments = @"C:\Some\Other\Path"; // 这里填入作为参数传递的路径

    // 设置其他选项,如是否使用Shell执行(这里假设不需要)
    startInfo.UseShellExecute = false;
    startInfo.CreateNoWindow = true; // 如果不需要显示窗口

    // 创建并启动进程
    using (Process process = new Process())
    {
        process.StartInfo = startInfo;
        process.Start();
    }
}

// 接收参数的被启动程序的Main方法示例:
using System;

class YourProgram
{
    static void Main(string[] args)
    {
        if (args.Length > 0)
        {
            string receivedPath = args[0]; // 获取第一个参数,假设这就是我们传递的路径

            Console.WriteLine($"Received path: {receivedPath}");

            // 在这里处理接收到的路径
            // ...
        }
        else
        {
            Console.WriteLine("No argument was passed.");
        }
    }
}

二、传递多个参数

启动另一个exe并需要传递多个参数时,可以将所有参数作为单个字符串,在参数之间用空格分隔,然后设置到ProcessStartInfo.Arguments属性中。

using System.Diagnostics;

public void StartAnotherProcessWithArguments()
{
    // 创建ProcessStartInfo实例
    ProcessStartInfo startInfo = new ProcessStartInfo();

    // 设置要执行的程序路径
    startInfo.FileName = @"C:\Path\To\Your\Executable.exe";

    // 设置传递给程序的参数
    // 假设我们有两个参数,一个是路径,另一个是选项
    string arg1 = @"C:\Some\Path";
    string arg2 = "OptionValue";
    startInfo.Arguments = $"{arg1} {arg2}";

    // 设置其他选项,如是否使用Shell执行(这里假设不需要)
    startInfo.UseShellExecute = false;
    startInfo.CreateNoWindow = true; // 如果不需要显示窗口

    // 创建并启动进程
    using (Process process = new Process())
    {
        process.StartInfo = startInfo;
        process.Start();
    }
}

// 接收参数的被启动程序的Main方法示例:
static void Main(string[] args)
{
    // 参数会被解析为字符串数组
    // args[0] 应该是 "C:\Some\Path"
    // args[1] 应该是 "OptionValue"

    Console.WriteLine($"参数数量: {args.Length}");
    for (int i = 0; i < args.Length; i++)
    {
        Console.WriteLine($"参数{i}: {args[i]}");
    }

    // 根据参数进行相应操作...
}

 

 

相关推荐

  1. C# 程序启动另外一个exe时候参数

    2024-05-02 19:36:03       36 阅读
  2. C# 只允许开启一个exe程序

    2024-05-02 19:36:03       50 阅读
  3. c++参数引用参数

    2024-05-02 19:36:03       34 阅读
  4. Linux 设置一个程序开机启动几种方式

    2024-05-02 19:36:03       39 阅读
  5. C#thread线程参数更新UI文本框

    2024-05-02 19:36:03       36 阅读

最近更新

  1. docker php8.1+nginx base 镜像 dockerfile 配置

    2024-05-02 19:36:03       98 阅读
  2. Could not load dynamic library ‘cudart64_100.dll‘

    2024-05-02 19:36:03       106 阅读
  3. 在Django里面运行非项目文件

    2024-05-02 19:36:03       87 阅读
  4. Python语言-面向对象

    2024-05-02 19:36:03       96 阅读

热门阅读

  1. 图搜索算法详解

    2024-05-02 19:36:03       31 阅读
  2. 【AIGC半月报】AIGC大模型启元:2024.04(下)

    2024-05-02 19:36:03       35 阅读
  3. 如何在前端展示后端返回的pdf Base64格式字符串

    2024-05-02 19:36:03       28 阅读
  4. 第二弹:走进CSS世界,学习记录

    2024-05-02 19:36:03       30 阅读
  5. 【C++】循环语句中引起的循环引用问题

    2024-05-02 19:36:03       37 阅读
  6. npm详解

    2024-05-02 19:36:03       41 阅读
  7. C++可变参数模板中的省略号

    2024-05-02 19:36:03       36 阅读
  8. 子查询

    2024-05-02 19:36:03       40 阅读
  9. 中了内存马如何排查(不死马)

    2024-05-02 19:36:03       34 阅读
  10. MyBatis-plus笔记——分页插件

    2024-05-02 19:36:03       35 阅读