SetEnvironmentVariable in Debug and Release configuration
-
16 สิงหาคม 2548 8:40
Hello folks,
I was trying to set some environment variable for a process I create.
(The variables are my own, not system).I use SetEnvironmentVaraible() from C# code.
In Debug configuration this works fine, but in Release configuration the action fails.
The return value is TRUE, and no Exception is thrown.This is the relevant code:
bool
bStat = false;StreamWriter swError =
new StreamWriter("a.txt"); try{
// Get the write permission to set the environment variable.EnvironmentPermission environmentPermission =
new EnvironmentPermission(EnvironmentPermissionAccess.Write,i_sVariableName);
environmentPermission.Demand();
// Set the Environment Variable and return the result.bStat = SetEnvironmentVariable(i_sVariableName,
i_sVariableValue);
// Log into file.swError.WriteLine(
"The result of setting environment variable " +i_sVariableName +
" is :" + bStat.ToString());}
catch (Exception e){
// Write the exception into file.swError.WriteLine(
"An exception has occured while trying to set " + "environment variable " + i_sVariableName + ": " + e.Message);swError.Flush();
}
finally{
// Close the exception log file stream.swError.Close();
}
return bStat;Any ideas ?!
ตอบทั้งหมด
-
16 สิงหาคม 2548 4:34And how did you determine it failed?Willy.<GreatGreenLass@discussions.microsoft.com> wrote in message news:9ba3b990-9420-4981-9921-bdab6ae93c9e@discussions.microsoft.com...
Hello folks,
I was trying to set some environment variable for a process I create.
(The variables are my own, not system).I use SetEnvironmentVaraible() from C# code.
In Debug configuration this works fine, but in Release configuration the action fails.
The return value is TRUE, and no Exception is thrown.This is the relevant code:
bool
bStat = false;StreamWriter swError =
new StreamWriter("a.txt"); try{
// Get the write permission to set the environment variable.EnvironmentPermission environmentPermission =
new EnvironmentPermission(EnvironmentPermissionAccess.Write,i_sVariableName);
environmentPermission.Demand();
// Set the Environment Variable and return the result.bStat = SetEnvironmentVariable(i_sVariableName,
i_sVariableValue);
// Log into file.swError.WriteLine(
"The result of setting environment variable " +i_sVariableName +
" is :" + bStat.ToString());}
catch (Exception e){
// Write the exception into file.swError.WriteLine(
"An exception has occured while trying to set " + "environment variable " + i_sVariableName + ": " + e.Message);swError.Flush();
}
finally{
// Close the exception log file stream.swError.Close();
}
return bStat;Any ideas ?!
-
16 สิงหาคม 2548 13:50By using Process Explorer.
Also, I have a C++ DLL that calls to getenv() upon its initialization and fails.