• C#实现对程序打包调用


    写了一段程序实现对另一个程序打包调用,同时可以加入一些延迟之类的操作,需要注意的是导入所有被调程序需要的参数,导出所有其返回的值。

    --------------------

    CallProgram
    1 using System;
    2  using System.Collections.Generic;
    3  using System.Text;
    4 using System.Diagnostics;
    5 using System.Threading;
    6
    7 public static int StartProcess(string filename, string[] args)
    8 {
    9 // Create a process for the EXE file you want to call.
    10 Process myProcess = new Process();
    11 int res = -1;
    12 try
    13 {
    14 string sArg = "";
    15 foreach (string arg in args)
    16 {
    17 sArg = sArg + arg + " ";
    18 }
    19 sArg = sArg.Trim();
    20 // The ProcessStartInfo that represents the data with which to start the process. These arguments include the name of the executable file or document used to start the process.
    21 ProcessStartInfo startInfo = new ProcessStartInfo(filename, sArg);
    22 myProcess.StartInfo = startInfo;
    23 // True to use the shell when starting the process; otherwise, the process is created directly from the executable file. The default is true.
    24 myProcess.StartInfo.UseShellExecute = false;
    25 myProcess.Start();
    26 myProcess.WaitForExit(); // Remember to wait for exit here.
    27 res = myProcess.ExitCode; // Return the ExitCode from the EXE file you called.
    28 }
    29 catch (Exception ex)
    30 {
    31 Console.Out.WriteLine(ex.Message);
    32 }
    33 finally
    34 {
    35 if (myProcess != null)
    36 myProcess.Close();
    37 }
    38 return res;
    39 }
    40
    41 static int Main(string[] args)
    42 {
    43 // Full name of the EXE file to be called.
    44 string strFileName = "c:\\test.exe";
    45
    46 // If want to add delay, sleep could be used here.
    47
    48 Thread.Sleep(60000);
    49 // Get args from Main.
    50 int res = StartProcess(strFileName, args);
    51 // Return the value what EXE file returns.
    52 return res;
    53 }

    参考:http://msdn.microsoft.com/en-us/library/system.diagnostics.process(VS.71).aspx

     
  • 相关阅读:
    数组常用操作实现
    链表简单实现栈与队列
    亿万级别的表数据处理方式
    ubuntu安装与卸载.dep软件
    pg数据库表接口和数据导出
    深入分析理解Tomcat体系结构
    Servlet源码级别进行详解
    Maven命令行创建java或javaWeb项目
    解释语言与编译语言
    C++中Reference与Pointer的不同
  • 原文地址:https://www.cnblogs.com/pegasus923/p/1857715.html
Copyright © 2020-2023  润新知