Delete environment Variables

M

Manoj Nair

Hi,
How can i delete a specific environment variable through code

Thanks in advance
Manoj
 
C

CometJoe

Setting the value of an existing environment variable to "null" will
delete it from the current process's environment.
This will create a new var & then delete it.
Hope this helps,
Joe


using System.Runtime.InteropServices;


public class Test
{
[DllImport("kernel32.dll", CharSet=CharSet.Auto,
SetLastError=true)]
public static extern bool SetEnvironmentVariable( string
sVarName, string sVarValue );

public Test()
{
}

public string TryThis()
{
string NewEnvVar = "junk";
string NewEnvValue = "testing";
//SetEnvironmentVariable ( NewEnvVar, NewEnvValue );

string sTest = Environment.GetEnvironmentVariable (
"junk" );

SetEnvironmentVariable( "junk", "" );
sTest = Environment.GetEnvironmentVariable ( "junk"
);

return sTest;
}

}
 
M

Manoj Nair

Joe,
Thanks for the help
Manoj

CometJoe said:
Setting the value of an existing environment variable to "null" will
delete it from the current process's environment.
This will create a new var & then delete it.
Hope this helps,
Joe


using System.Runtime.InteropServices;


public class Test
{
[DllImport("kernel32.dll", CharSet=CharSet.Auto,
SetLastError=true)]
public static extern bool SetEnvironmentVariable( string
sVarName, string sVarValue );

public Test()
{
}

public string TryThis()
{
string NewEnvVar = "junk";
string NewEnvValue = "testing";
//SetEnvironmentVariable ( NewEnvVar, NewEnvValue );

string sTest = Environment.GetEnvironmentVariable (
"junk" );

SetEnvironmentVariable( "junk", "" );
sTest = Environment.GetEnvironmentVariable ( "junk"
);

return sTest;
}

}
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top