Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6941045
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 27, 20262026-05-27T12:50:06+00:00 2026-05-27T12:50:06+00:00

Hi guys I have a dictionary which has to be shared between two different

  • 0

Hi guys I have a dictionary which has to be shared between two different exe files. The first application creates a key, then stores it in the dictionary, then the other application creates a key and stores it in the dictionary.

At the moment i do this:

private static void WriteToFile(Dictionary<string, byte[]> dictionary, string path)
    {
        Contract.Requires(dictionary != null);
        Contract.Requires(!string.IsNullOrEmpty(path));

        if (!(timestamp == File.GetLastWriteTime(DatabasePath)))
        {
            using (FileStream fs = File.OpenWrite(path))
            using (var writer = new BinaryWriter(fs))
            {
                // Put count.
                writer.Write(dictionary.Count);

                // Write pairs.
                foreach (var pair in dictionary)
                {
                    writer.Write(pair.Key);
                    writer.Write(pair.Value);
                }
                timestamp = DateTime.Now;
                File.SetLastWriteTime(DatabasePath, timestamp);

            }
        }
    }

    /// <summary>
    /// This is used to read a dictionary from a file
    /// http://www.dotnetperls.com/dictionary-binary
    /// </summary>
    /// <param name="path">The path to the file</param>
    /// <returns>The dictionary read from the file</returns>
    private static Dictionary<string, byte[]> ReadFromFile(string path)
    {
        Contract.Requires(!string.IsNullOrEmpty(path));
        var result = new Dictionary<string, byte[]>();
        using (FileStream fs = File.OpenRead(path))
        using (var reader = new BinaryReader(fs))
        {
            // Determine the amount of key value pairs to read.
            int count = reader.ReadInt32();

            // Read in all the pairs.
            for (int i = 0; i < count; i++)
            {
                string key = reader.ReadString();
                //// The byte value is hardcoded as the keysize is consistent
                byte[] value = reader.ReadBytes(513);
                result[key] = value;
            }
        }
        return result;
    }

Then when I want to store a key I call this method:

public static bool StoreKey(byte[] publicKey, string uniqueIdentifier)
    {
        Contract.Requires(ValidPublicKeyBlob(publicKey));
        Contract.Requires(publicKey != null);
        Contract.Requires(uniqueIdentifier != null);
        Contract.Requires(uniqueIdentifier != string.Empty);
        bool success = false;

        if (File.Exists(DatabasePath))
        {
            keyCollection = ReadFromFile(DatabasePath);
        }

        if (!keyCollection.ContainsKey(uniqueIdentifier))
        {
            if (!keyCollection.ContainsValue(publicKey))
            {
                keyCollection.Add(uniqueIdentifier, publicKey);
                success = true;

                WriteToFile(keyCollection, DatabasePath);
            }
        }
        return success;
    }

When the programs generates the key and when we then try to access them, it only has 1 key, what am I doing wrong? The key and string is stored perfectly, but I’m just afraid that they are overwriting the files or something.

Thank you very much in advance, any help is greatly appreciated

PS: The databasePath is the path where I want to save the file, created as a field.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-27T12:50:06+00:00Added an answer on May 27, 2026 at 12:50 pm

    It is hard to say what exactly going on since you’ve not provided an information regarding how many items in dictionary and so on, but it seems like you’ve encountered some kind of a file access issue when accessing the same file from multiple processes.

    You can use named Mutex as a cross process synchronization object so before accessing a file you have to ensure that Mutex handle is released so you can aquire an ownership and an other process would be able to wait before accessing a file.

    // Create a mutex
    Mutex mutex = new Mutex(false, "DictionaryAccessMutex");
    
    // Acquire an ownership
    mutex.WaitOne();
    
    // Release
    mutex.ReleaseMutex();
    

    EDIT: New finding

    Also you trying to write immediately after the read, so perhaps FileSystem operation is not completed yet so write failed, I’m not sure 100% in this perhaps .NET managed classes like File/StreamReader/etc already handled such cases but I believe it worth to double check in your case since is not 100% clear what is happened. So just try out adding some timeout like Thread.Sleep(500) between read and write operations.

    EDIT: One more thing you can do is download Process Monitor SysInternals utility and see which operations are failed when accessign a given file. So just add a new filter Path=file name and you would be able see what is going on on the low level.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

hi guys i have two pipe delimited files,first file contains 1000 records and second
Do you guys have some .Net libraries which has ability to convert a PowerPoint
guys i have arrays in which i have to match this kind of text
Do you guys have an idea on how to search or list down .exe
Hi guys have a problem with renaming a set of files in separate folder
I have a simple php page which outputs a table (which has been created
You guys have helped me out so much this week it has been awesome.Hopefully
Common Lisp guys have their CL-WHO , which makes HTML templating integrated with the
do you guys have any idea how to edit the the labels in the
Thus far you guys have been wildly helpful with me getting this little ditty

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.