运行在C#code一个EXE [英] Run an exe from C# code

查看:186
本文介绍了运行在C#code一个EXE的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在我的C#项目一个exe文件引用。我如何调用该exe文件从我的code?

解决方案

 使用System.Diagnostics程序;
类节目
{
    静态无效的主要()
    {
    //
    //使用的Process.Start这里。
    //
    的Process.Start(C:\\);
    }
}
 

如果您的应用程序需要CMD参数,使用这样的:

 使用System.Diagnostics程序;

类节目
{
    静态无效的主要()
    {
    LaunchCommandLineApp();
    }

    ///<总结>
    ///启动了一些选项设置的传统应用程序。
    ///< /总结>
    静态无效LaunchCommandLineApp()
    {
    //对于这个例子
    常量字符串EX1 =C:\\;
    常量字符串EX2 =C:\\目录;

    //使用的ProcessStartInfo类
    的ProcessStartInfo的StartInfo =新的ProcessStartInfo();
    startInfo.CreateNoWindow = FALSE;
    startInfo.UseShellExecute = FALSE;
    startInfo.FileName =dcm2jpg.exe;
    startInfo.WindowStyle = ProcessWindowStyle.Hidden;
    startInfo.Arguments =-fj -o \+ EX1 +\-z 1.0 -sy+ EX2;

    尝试
    {
        //开始与我们指定的信息的过程。
        //调用WaitForExit然后using语句将关闭。
        使用(流程exeProcess =的Process.Start(StartInfo的))
        {
        exeProcess.WaitForExit();
        }
    }
    抓住
    {
        //日志错误。
    }
    }
}
 

I have an exe file reference in my C# project. How do I invoke that exe from my code?

解决方案

using System.Diagnostics;
class Program
{
    static void Main()
    {
    //
    // Use Process.Start here.
    //
    Process.Start("C:\\");
    }
}

If your application needs cmd arguments, use something like this:

using System.Diagnostics;

class Program
{
    static void Main()
    {
    LaunchCommandLineApp();
    }

    /// <summary>
    /// Launch the legacy application with some options set.
    /// </summary>
    static void LaunchCommandLineApp()
    {
    // For the example
    const string ex1 = "C:\\";
    const string ex2 = "C:\\Dir";

    // Use ProcessStartInfo class
    ProcessStartInfo startInfo = new ProcessStartInfo();
    startInfo.CreateNoWindow = false;
    startInfo.UseShellExecute = false;
    startInfo.FileName = "dcm2jpg.exe";
    startInfo.WindowStyle = ProcessWindowStyle.Hidden;
    startInfo.Arguments = "-f j -o \"" + ex1 + "\" -z 1.0 -s y " + ex2;

    try
    {
        // Start the process with the info we specified.
        // Call WaitForExit and then the using statement will close.
        using (Process exeProcess = Process.Start(startInfo))
        {
        exeProcess.WaitForExit();
        }
    }
    catch
    {
        // Log error.
    }
    }
}

这篇关于运行在C#code一个EXE的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆