zoukankan      html  css  js  c++  java
  • 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

     
  • 相关阅读:
    数据访问技术系列课程 笔记(2) ADO.NET 连接方式进行数据访问
    Modern C# 系列课程笔记 第11节 深入委托和事件
    idea 将项目托管到 Git 报错:Can't finish Gitee sharing process
    ADO.Net
    WebService
    2013年了
    201301杂谈
    流程图
    出错列表
    杂谈4 2012年8月15日开
  • 原文地址:https://www.cnblogs.com/pegasus923/p/1857715.html
Copyright © 2011-2022 走看看