1

I have a WPF application. For my application I am setting an environment variable and updating the path variable.

Environment.SetEnvironmentVariable(var_name, value, EnvironmentVariableTarget.Machine);

The first time it runs it will search for that variable, if it's not found then it will set it.

if (IsSonarRunnerHomePathSet() == false)
{
    SetSonarRunnerHomePath(strSonarPath);   
}

And code for

public bool IsSonarRunnerHomePathSet()
{
    try
    {
    string envJavaHome = Environment.GetEnvironmentVariable("SONAR_RUNNER_HOME", EnvironmentVariableTarget.Machine);
    return (envJavaHome != null) ? true : false;
    }
    catch (Exception ex)
    {       
        throw new Exception(" GetEnvironmentVariable Error -" + ex.Message);
    }
}

public void SetSonarRunnerHomePath(string strSonarDirPath)
{
    try
    {                
        Environment.SetEnvironmentVariable(sonar_runner_home, strSonarDirPath, EnvironmentVariableTarget.Machine);
    }
    catch (Exception ex)
    {                
        throw new Exception("SetEnvironmentVariable SONAR_RUNNER_HOME Error - " + ex.Message);
    }
}

After that I execute a bat file from my application using Process.Start which uses this environment variable. Bat file gets created at run time and bat file contains

cd /D  someDirPath      //someDirPath is provided from user as input
sonar-runner.bat -X     // run sonar-runner from someDirPath

and

proc = new Process();
//command to execute
proc.StartInfo.FileName = batchFileName;
proc.StartInfo.UseShellExecute = false;
proc.Start();

But that process is not able to find that environment variable.

When I run my application a second time, it's able to find it in my WPF application with:

Environment.GetEnvironmentVariable(var_name, EnvironmentVariableTarget.Machine);

But when the bat file starts executing, it's still not able to find that environment variable.

I added a command in the bat file to display environment variable, my variables are not displayed. But when I open a command prompt and give the set command to see environment variables. It's showing my variable.

Getting below error

'sonar-runner.bat' is not recognized as an internal or external command,

How is that batch process not able to see it, not even during the second time?

Any suggestions/help?

atulya
  • 535
  • 2
  • 8
  • 25

0 Answers0