"找不到路径&QUOT的一部分;在创建互斥错误找不到、路径、错误、互斥

2023-09-03 03:40:20 作者:成绩好就是一切?

我被这个困惑,有人可以告诉我为什么,当我打电话:

I'm baffled by this, can someone tell me why, when I call:

using (Mutex mtx = new Mutex(false, strId))
{
}

我得到这个异​​常:

I get this exception:

找不到路径的一部分。

如果 strId 设置为类似本地主机\ SQLEX preSS-MYNAME-2

推荐答案

的从文档:

在运行终端服务,一个名为系统互斥的服务器   可以有两个级别的可见性。如果它的名字开头的preFIX   全球\的互斥体在所有终端服务器会话可见。如果   它的名字开头的preFIX本地\,互斥体是可见的,只有在   在创建它的终端服务器会话。在这种情况下,一个   具有相同名称的单独的互斥可以在每个其它的存在   在服务器上的终端服务器会话。如果没有指定preFIX   当你创建一个命名的互斥体,它采取了preFIX本地\。在一个   终端服务器会话两个互斥的名字只相差他们   prefixes是分开的互斥体,无一不是对所有进程可见   在终端服务器会话。也就是说,preFIX名全球\   和本地\形容互斥体的名称相对于终端的范围   服务器会话,不相进程。

On a server that is running Terminal Services, a named system mutex can have two levels of visibility. If its name begins with the prefix "Global\", the mutex is visible in all terminal server sessions. If its name begins with the prefix "Local\", the mutex is visible only in the terminal server session where it was created. In that case, a separate mutex with the same name can exist in each of the other terminal server sessions on the server. If you do not specify a prefix when you create a named mutex, it takes the prefix "Local\". Within a terminal server session, two mutexes whose names differ only by their prefixes are separate mutexes, and both are visible to all processes in the terminal server session. That is, the prefix names "Global\" and "Local\" describe the scope of the mutex name relative to terminal server sessions, not relative to processes.

由于你是在你的名字用一个反斜杠( \ ),它假定您要指定可见性级别,然后发现本地主机不是一个有效的能见度的水平 - 因此产生的异常

Because you are using a backslash in your name (\) it assumes you are trying to specify a visibility level, and then discovers localhost isn't a valid visibility level - hence generating the exception.

这是相当奇怪的.NET文档不明确提及 \ 是互斥的名字保留字符,在Win32文档不解释好(link):

It's rather strange the .NET docs don't mention explicitly \ is a reserved character in Mutex names, the Win32 docs do explain it better (link):

的名称可以有一个全球\或本地\preFIX明确创建   全局或会话名称空间中的对象。的其余   名称可以包含除反斜杠字符的任意字符()。对于   更多信息,请参见内核对象命名空间。快速用户切换   使用终端服务会话来实现。内核对象名称必须   按照为终端服务的指导方针,使   应用程序可以支持多个用户。

The name can have a "Global\" or "Local\" prefix to explicitly create the object in the global or session namespace. The remainder of the name can contain any character except the backslash character (). For more information, see Kernel Object Namespaces. Fast user switching is implemented using Terminal Services sessions. Kernel object names must follow the guidelines outlined for Terminal Services so that applications can support multiple users.