cool hit counter Another way to prevent multiple openings in C# programs_Intefrankly

Another way to prevent multiple openings in C# programs


Function: Prevents exe from renaming, moving location and running again

Implementation idea.

At the beginning of the Main() method, it iterate through all processes, get the assembly GUID and PID of each process, and if it finds a process with the same GUID and a different PID, it orders itself to exit.

Notes.

1. The GUID is used to ensure the reliability of the determination as much as possible, and the use of process names is too unreliable. Because the assembly GUID is generated when the project is created, does not change with the version, the content of the change, so unless artificially changed, the same project compiled several times are still the same GUID, used to determine the identity of the assembly can not be more suitable. Off-topic, the common commercially available mutual exclusion body method, where the GUID is also recommended for the mutual exclusion name.

2. The reason for adding the process ID determination is that the traversed process already contains its own process, so it must exclude itself.

3. After trying it seems that only . Net assembly to get the GUID (a bit of a crap shoot~), but that's enough

4, the first time in cnblogs write blogs, I hope you seniors more guidance ~ less throw bricks

using System;
using System.Diagnostics;
using System.IO;
using System.Reflection;
using System.Runtime.InteropServices;
using System.Windows.Forms;

namespace TestCallAPIRefreshPolicy
{
    static class Program
    {
        [STAThread]
        static void Main()
        {
            Guid ownGUID = new Guid(((GuidAttribute)Attribute.GetCustomAttribute(Assembly.GetExecutingAssembly(), typeof(GuidAttribute))).Value);
            int ownPID = Process.GetCurrentProcess().Id;

            foreach (Process p in Process.GetProcesses())
            {
                try
                {
                    //The reason for the use ofAssembly.Load(byte[]) Get the assembly because the method does not lock the file
                    Guid proGUID = new Guid(((GuidAttribute)Attribute.GetCustomAttribute(Assembly.Load(File.ReadAllBytes(p.MainModule.FileName)), typeof(GuidAttribute))).Value);
                    if (ownGUID == proGUID && ownPID != p.Id)
                    {
                        MessageBox.Show(" Program is running");
                        return;
                    }
                }
                catch { }// Access to certain processes'MainModule It can cause anomalies, Must wave or
            }

            Application.EnableVisualStyles();
            Application.SetCompatibleTextRenderingDefault(false);
            Application.Run(new FmMain());
        }
    }
}

Recommended>>
1、python developmenttkinterradiobutton
2、9How redux streamlines code
3、Data Analysis Case What Determines Your Purchase Decision
4、Solving the problem of text not being right aligned on UILabel in ios development
5、Upgrade method under the master program of Riptide device

    已推荐到看一看 和朋友分享想法
    最多200字,当前共 发送

    已发送

    朋友将在看一看看到

    确定
    分享你的想法...
    取消

    分享想法到看一看

    确定
    最多200字,当前共

    发送中

    网络异常,请稍后重试

    微信扫一扫
    关注该公众号