简体   繁体   English

如何在 C# 中允许来自同一目录的单实例应用程序但来自不同目录的多个实例?

[英]How to allow single instance application from the same directory but several instances from different directories in C#?

How could I allow a single instance application started from the same directory but several instances from different directories of the same executable?如何允许从同一目录启动单个实例应用程序,但从同一可执行文件的不同目录启动多个实例?

More clearly,更清楚,

I want MyProgram.Exe<instance 1> to be run mono instance from "C:\Directory1"我希望MyProgram.Exe<instance 1>"C:\Directory1"运行 mono 实例

I want MyProgram.Exe<instance 2> to be run mono instance from "C:\Directory2"我希望MyProgram.Exe<instance 2>"C:\Directory2"运行 mono 实例

And MyProgram.Exe<instance 1> and MyProgram.Exe<instance 2> can be run at the same time.并且MyProgram.Exe<instance 1>MyProgram.Exe<instance 2>可以同时运行。

What I'm doing at this time is using Mutex, but I don't know how to achieve this:我此时正在做的是使用互斥锁,但我不知道如何实现这一点:

public partial class App : Application
{
    private static Mutex _mutex = null;
    // Application GUID
    private string _applicationGUID = "4hfd130b-1eh6-4979-bbqc-08g16478c36f";

    public App()
    {
        bool isNewInstance;

        _mutex = new Mutex(true, _applicationGUID, out isNewInstance);

        if (!isNewInstance)
        {
            MessageBox.Show("An application is already running. Closing...", "MyProgram", MessageBoxButton.OK, MessageBoxImage.Exclamation);

            Current.Shutdown();
        }
        else
        {
            // Other stuff ...
        }

    }
}

Your help will be appreciated.您的帮助将不胜感激。

You might be able to use Assembly.Location (or the exe path in general) as the named mutex, with or without your Id.您可能可以使用Assembly.Location (或一般的 exe 路径)作为命名互斥锁,无论是否有您的 ID。

Gets the full path or UNC location of the loaded file that contains the manifest.获取包含清单的已加载文件的完整路径或 UNC 位置。

_mutex = new Mutex(true, directory, out isNewInstance);

or completely overkill或完全矫枉过正

var location = Assembly.GetEntryAssembly().Location;
var hs = System.Security.Cryptography.MD5.Create();
var bytes = hs.ComputeHash(System.Text.Encoding.UTF8.GetBytes(location));

_mutex = new Mutex(true, Convert.ToBase64String(bytes), out isNewInstance);

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 从单个应用程序的多个实例,C#,Visual Studio 2008将数据写入一个文件 - Writing data to one file from several instances of single application, C#, Visual Studio 2008 C#:如何从WPF应用程序的多个运行实例中获取最近使用的实例? - C# : How to get most recently used instance from multiple running instances of my WPF application? 如何在C#中从单个完整路径创建多个目录? - How to create multiple directories from a single full path in C#? C# 如何从同一个表单中获取数据打开多次,每个表单中都有不同的数据? - C# How to get data from the same form open several times and in each form there are different data? C#将文件重命名为与其他目录中的文件相同的名称 - C# rename a file to the same name as a file from a different directory 使用C#,如何允许从应用程序外部编辑字符串? - Using C#, how to allow editing of strings from outside the application? C#从不同的网址打开同一应用程序并传递参数 - C# Open Same Application From Different Url and pass parameters 是否可以从同一个应用程序登录到两个不同的 Application Insights 实例? - Is it possible to log to two different instances of Application Insights from the same Application? 如何从目录复制所有文件并将这些文件的副本移动到同一 azure blob 容器 c# 中的不同目录 - how to copy all files from a directory and move the copy of those file to different directory in same azure blob container c# 使用C#从几个相同类型的不同列表创建一个列表 - Create one List from several different lists of the same type with C#
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM