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

来源:互联网 发布:用友票据打印软件 编辑:程序博客网 时间:2024/05/21 10:53

原文地址:http://blog.csdn.net/2004v2004/archive/2009/03/21/4012650.aspx

 

 

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);
            }

 

本文来自CSDN博客,转载请标明出处:http://blog.csdn.net/2004v2004/archive/2009/03/21/4012650.aspx

 

 

 

 

原创粉丝点击