c# 打开exe文件与关闭exe文件 c#服务打开exe时候默认是没有窗体界面的

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using System.Diagnostics;
namespace WindowsApplication1
{
    public partial class Form1 : Form
    {
        private static System.Diagnostics.Process LandEatSnake, LandFileDivisison;

        public Form1()
        {
            InitializeComponent();
        }

        private void button1_Click(object sender, EventArgs e)// 打开exe
        {
            try
            {
                if (LandFileDivisison == null)
                {
                    LandFileDivisison = new System.Diagnostics.Process();
                    LandFileDivisison.StartInfo.FileName = "1.exe";
                    LandFileDivisison.Start();
                }
                else
                {
                    if (LandFileDivisison.HasExited)  //是否正在运行
                    {
                        LandFileDivisison.Start();
                    }
                }
            }
            catch (Exception err)
            {
                MessageBox.Show("运行程序出错!" + err.ToString(), "出错信息", MessageBoxButtons.OK, MessageBoxIcon.Error);
            }

        }

        private void button2_Click(object sender, EventArgs e)   //关闭exe
        {
            Process[] pProcess;
            pProcess = Process.GetProcesses();
            for (int i = 1; i <= pProcess.Length - 1; i++)
            {
                if (pProcess[i].ProcessName == "1")   //任务管理器 应用程序的名
                {
                    pProcess[i].Kill();
                    break;
                }
            }
           
         
    }

      
}
}

 

 c#服务打开exe时候默认是没有窗体界面的

 解决办法 是“点这个服务” “右键”--” 属性“ “登陆”  选择  “允许服务与桌面交互”  即可

 

 

  try
            {
                if (LandFileDivisison == null)
                {
                    LandFileDivisison = new System.Diagnostics.Process();
                    LandFileDivisison.StartInfo.FileName = strfilename;// "C://Temp//server.exe"; //换成你要执行的
                    LandFileDivisison.StartInfo.UseShellExecute = false;
                    LandFileDivisison.StartInfo.RedirectStandardInput = true;
                    LandFileDivisison.StartInfo.RedirectStandardOutput = true;
                    LandFileDivisison.StartInfo.RedirectStandardError = true;
                    LandFileDivisison.StartInfo.CreateNoWindow = true;
                  //  LandFileDivisison.StartInfo.Arguments = "strdbname";    //传参数 打开exe
                    string strregvalue = strdbname +"," + struser + "," + strpassword + "," + strskyip + "," + strskyport;
                    WriteLog_text.WriteToLog("注册表数据 " + strregvalue + "/r/n");
                    LandFileDivisison.StartInfo.Arguments = strregvalue; 
                    LandFileDivisison.Start();
                      
                    WriteLog_text.WriteToLog("EXE" +"启动成功" + "/r/n");
                }
                else
                {
                    if (LandFileDivisison.HasExited)  //是否正在运行
                    {
                        LandFileDivisison.Start();
                    }
                }
            }
            catch (Exception err)
            {
                WriteLog_text.WriteToLog("!" + err.ToString() + "启动成功" + "/r/n");
              //  MessageBox.Show("运行程序出错!" + err.ToString(), "出错信息", MessageBoxButtons.OK, MessageBoxIcon.Error);
            }

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值