Workaround to “Item has already been added. Key in dictionary” using the same key everytime but with a different value

StackOverflow https://stackoverflow.com/questions/10716800

Domanda

Under Environment Variables for User, i have added a variable with the key called eg.

TMP_VAR

and value eg.

C:\temp\sim

When starting a new process in my C# application created in VS2010 .Net 4.0 and setting the StartInfo.EnvironmentVariables.Add("TMP_VAR", C:\temp\sim); I get this error when double clicking the exe file:

Item has already been added. Key in dictionary: 'TMP_VAR' Key being added: 'TMP_VAR'

If however I start it from VS2010 (pressing F5) no error occurs.

My theory is that VS2010 somehow starts the process in somekind of a "shell" and overwrites the variable in that "shell".

Also my application start several processes with the same key but different value. The key cannot be changed. It has to be TMP_VAR, but the value is allowed to be changed.

My question is: How can my application start a new process, setting the same key using StartInfo.EnvironmentVariables in somekind of a "shell". Or is there another smart solution to my problem?

È stato utile?

Soluzione

Instead of adding, processes can check the existence of the key 'TMP_VAR' and just change the value accordingly if it exists.

if(StartInfo.EnvironmentVariables.ContainsKey("TMP_VAR"))
    StartInfo.EnvironmentVariables["TMP_VAR"] = "C:/temp/sim";
else
    StartInfo.EnvironmentVariables.Add("TMP_VAR", "C:/temp/sim");
Autorizzato sotto: CC-BY-SA insieme a attribuzione
Non affiliato a StackOverflow
scroll top