• c# winform程序启动时判断本身是否在运行,是否已经运行


    c# winform程序启动时判断本身是否在运行,是否已经运行

    Program.cs里面修改

    using System;
    using System.Collections.Generic;
    using System.Diagnostics;
    using System.Linq;
    using System.Threading.Tasks;
    using System.Windows.Forms;
    
    namespace AutoPackSystem
    {
        static class Program
        {  //by txwtech
            /// <summary>
            /// 应用程序的主入口点。
            /// </summary>
            [STAThread]
            static void Main()
            {
    
                Application.EnableVisualStyles();
                Application.SetCompatibleTextRenderingDefault(false);
    
                if (IsRunning())
                {
                    MessageBox.Show("AutoPackSystem.exe已经运行");
                    return;
                }
                 bool IsRunning()
                {
                    Process current = default(Process);
                    current = System.Diagnostics.Process.GetCurrentProcess();
                    Process[] processes = null;
                    processes = System.Diagnostics.Process.GetProcessesByName(current.ProcessName);
    
                    Process process = default(Process);
    
                    foreach (Process tempLoopVar_process in processes)
                    {
                        process = tempLoopVar_process;
    
                        if (process.Id != current.Id)
                        {
                            if (System.Reflection.Assembly.GetExecutingAssembly().Location.Replace("/", "\") == current.MainModule.FileName)
                            {
                                return true;
    
                            }
    
                        }
                    }
                    return false;
    
                }
    
    
    
    
    
    
                Application.EnableVisualStyles();
                Application.SetCompatibleTextRenderingDefault(false);
                Application.Run(new Form1());
               
            }
        }
    }
    欢迎讨论,相互学习。 txwtech@163.com
  • 相关阅读:
    Linux的学习--系统目录
    PHP内核的学习--创建PHP扩展
    PHP的学习--连接MySQL的三种方式
    MIME Type
    颜色的命名
    JavaScript的学习--生成二维码
    MySQL的学习--触发器
    Google Guava之--cache
    java代码调用oracle存储过程
    oracle序列
  • 原文地址:https://www.cnblogs.com/txwtech/p/15215312.html
Copyright © 2020-2023  润新知