Note: Forums will be making significant UX changes to address key usability improvements surrounding search, discoverability and navigation. To learn more about these changes please visit the announcement which can be found HERE.
SetEnvironmentVariable in Debug and Release configuration

คำถาม 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:34
     
     
    And how did you determine it failed?
     
    Willy.
     

    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:50
     
     
    By using Process Explorer.
    Also, I have a C++ DLL that calls to getenv() upon its initialization and fails.