c# 调用cmd带参数 读取配置文件 2021-10-29

本文介绍如何使用C#通过ConfigurationManager获取配置,调用外部可执行文件(test_dll.exe)并传递参数。展示了如何捕获和处理调用过程中的异常,以确保高效执行。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

c# 调用cmd及参数

using System;
using System.Collections.Generic;
using System.Configuration;
using System.Diagnostics;
using System.Linq;
using System.Web;

namespace WebApplication2
{
    /// <summary>
    /// runexe 的摘要说明
    /// </summary>
    public class runexe : IHttpHandler
    {

        public void ProcessRequest(HttpContext context)
        {
            context.Response.ContentType = "text/plain";

            try
            {
                //string exe_path = "D:\\dll\\test_dll6\\test_dll\\test_dll.exe";  // 被调exe
                //string[] the_args = { "D:\\dll\\test_dll6\\test_dll\\input.txt" };   // 被调exe接受的参数
                string exe_path = ConfigurationManager.AppSettings["exe"].ToString();
                string[] the_args = ConfigurationManager.AppSettings["args"].ToString().Split(';');

                StartProcess(exe_path, the_args);
                context.Response.Write("Hello World");
            }
            catch (Exception ex)
            {
                throw;
            }
        }

        // 调用exe的函数
        public bool StartProcess(string runFilePath, params string[] args)
        {
            string s = "";
            foreach (string arg in args)
            {
                s = s + arg + " ";
            }
            s = s.Trim();
            Process process = new Process();//创建进程对象    
            ProcessStartInfo startInfo = new ProcessStartInfo(runFilePath, s); // 括号里是(程序名,参数)
            process.StartInfo = startInfo;
            process.Start();
            return true;
        }

        public bool IsReusable
        {
            get
            {
                return false;
            }
        }
    }
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值