内存映射文件 - >检查重复的文件名

时间:2023-01-26 22:02:07

Working on an app that will use memory-mapped files for some inter-(or is it intra?)application communication, and I want to make sure when I randomly generate a memory-mapped memory-only file, I don't accidentally generate or get a duplicate that already exists (that ultra-rare case when the planets align and something like that might happen). Any ideas on how to prevent that? System.IO.MemoryMappedFiles does not appear to have a File.Exists() method...I guess I could try OpenOrCreate() and deal with any access violations, etc., but this would be getting nasty quick.

使用内存映射文件进行某些内部(或内部?)应用程序通信的应用程序,我想确保当我随机生成一个内存映射的仅内存文件时,我不会意外地生成或者得到一个已经存在的副本(当行星对齐时可能发生类似的超罕见情况)。有关如何预防的任何想法? System.IO.MemoryMappedFiles似乎没有File.Exists()方法......我想我可以尝试OpenOrCreate()并处理任何访问冲突等,但这会很快变得讨厌。

3 个解决方案

#1


2  

Using File.Exists() wouldn't be concurrency-safe anyway.

无论如何,使用File.Exists()并不是并发安全的。

The best thing to do: base a Filename on a Guid.

最好的办法是:在Guid上建立一个文件名。

#2


1  

From: Programming Memory-Mapped Files with the .NET Framework - CodeProject.com

来自:使用.NET Framework编程内存映射文件 - CodeProject.com

static void Main(string[] args)
{
    string name = "NetMMFSingle";

    // Create named MMF
    try
    {
        var mmf = MemoryMappedFile.CreateNew(name, 1);
    } catch
    {
        Console.WriteLine("Instance already running...");
        return;
    }

    ConsoleKeyInfo key = Console.ReadKey();

    // Your real code follows...
}

#3


0  

First, you can prevent a naming conflict by using a unique name for your file, such as your company name, or your application name, or both. No other app is going to have those in the file name. If you're worried about two copies of the same app, then you can simply open the file in Create only mode, rather than Create or Open.

首先,您可以使用文件的唯一名称(例如公司名称或应用程序名称或两者)来防止命名冲突。没有其他应用程序会在文件名中包含那些应用程序。如果您担心同一应用程序的两个副本,则只需在“仅创建”模式下打开文件,而不是“创建”或“打开”。

#1


2  

Using File.Exists() wouldn't be concurrency-safe anyway.

无论如何,使用File.Exists()并不是并发安全的。

The best thing to do: base a Filename on a Guid.

最好的办法是:在Guid上建立一个文件名。

#2


1  

From: Programming Memory-Mapped Files with the .NET Framework - CodeProject.com

来自:使用.NET Framework编程内存映射文件 - CodeProject.com

static void Main(string[] args)
{
    string name = "NetMMFSingle";

    // Create named MMF
    try
    {
        var mmf = MemoryMappedFile.CreateNew(name, 1);
    } catch
    {
        Console.WriteLine("Instance already running...");
        return;
    }

    ConsoleKeyInfo key = Console.ReadKey();

    // Your real code follows...
}

#3


0  

First, you can prevent a naming conflict by using a unique name for your file, such as your company name, or your application name, or both. No other app is going to have those in the file name. If you're worried about two copies of the same app, then you can simply open the file in Create only mode, rather than Create or Open.

首先,您可以使用文件的唯一名称(例如公司名称或应用程序名称或两者)来防止命名冲突。没有其他应用程序会在文件名中包含那些应用程序。如果您担心同一应用程序的两个副本,则只需在“仅创建”模式下打开文件,而不是“创建”或“打开”。