53

環境変数の概念とは何ですか?

C#プログラムでは、実行可能ファイルを呼び出す必要があります。実行可能ファイルは、同じフォルダーにある他の実行可能ファイルを呼び出します。実行可能ファイルは、正しく設定される2つの環境変数「PATH」と「RAYPATH」に依存しています。私は次の2つのことを試しました:

  1. プロセスを作成し、StartInfoで2つの変数を設定しました。変数はすでに存在しますが、必要な情報が不足しています。
  2. System.Environment.SetEnvironmentVariable()で変数を設定しようとしました。

プロセスを実行すると、システムは実行可能ファイル( "executeable1")を見つけることができません。StartInfo.FileNameを「executeable1」のフルパスに設定しようとしましたが、「executeable1」内のformと呼ばれるEXEファイルが見つかりません...

どうすればこれに対処できますか?

string pathvar = System.Environment.GetEnvironmentVariable("PATH");
System.Environment.SetEnvironmentVariable("PATH", pathvar + @";C:\UD_\bin\DAYSIM\bin_windows\;C:\UD_\bin\Radiance\bin\;C:\UD_\bin\DAYSIM;");
System.Environment.SetEnvironmentVariable("RAYPATH", @"C:\UD_\bin\DAYSIM\lib\;C:\UD_\bin\Radiance\lib\");

System.Diagnostics.Process p = new System.Diagnostics.Process();
p.StartInfo.WorkingDirectory = @"C:\UD_\bin\DAYSIM\bin_windows";

//string pathvar = p.StartInfo.EnvironmentVariables["PATH"];
//p.StartInfo.EnvironmentVariables["PATH"] = pathvar + @";C:\UD_\bin\DAYSIM\bin_windows\;C:\UD_\bin\Radiance\bin\;C:\UD_\bin\DAYSIM;";
//p.StartInfo.EnvironmentVariables["RAYPATH"] = @"C:\UD_\bin\DAYSIM\lib\;C:\UD_\bin\Radiance\lib\";


p.StartInfo.UseShellExecute = false;
p.StartInfo.RedirectStandardOutput = true;
p.StartInfo.CreateNoWindow = true;

p.StartInfo.FileName = "executeable1";
p.StartInfo.Arguments = arg1 + " " + arg2;
p.Start();
p.WaitForExit();
4

2 に答える 2

103

実際にあなたの問題は何ですか?現在のプロセスSystem.Environment.SetEnvironmentVariableの環境変数を変更します。作成したプロセスの変数を変更したい場合は、ディクショナリ プロパティを使用します。EnvironmentVariables

var startInfo = new ProcessStartInfo();

// Sets RAYPATH variable to "test"
// The new process will have RAYPATH variable created with "test" value
// All environment variables of the created process are inherited from the
// current process
startInfo.EnvironmentVariables["RAYPATH"] = "test";

// Required for EnvironmentVariables to be set
startInfo.UseShellExecute = false;

// Sets some executable name
// The executable will be search in directories that are specified
// in the PATH variable of the current process
startInfo.FileName = "cmd.exe";

// Starts process
Process.Start(startInfo);
于 2013-01-29T12:29:29.887 に答える
6

システム レベルやユーザーなど、多くの種類の環境変数があります。それはあなたの要件に依存します。

環境変数を設定するには、Get Set メソッドを使用します。変数 Name と Value をパラメーターとして渡します。アクセス レベルを定義するために使用する場合は、それを渡す必要があります。値にアクセスするには、Set メソッドを使用してアクセス レベル パラメータも渡します。

たとえば、ユーザー レベルの変数を定義しています。

//For setting and defining variables
System.Environment.SetEnvironmentVariable("PathDB", txtPathSave.Text, EnvironmentVariableTarget.User);
System.Environment.SetEnvironmentVariable("DBname", comboBoxDataBaseName.Text, EnvironmentVariableTarget.User);

//For getting
string Pathsave = System.Environment.GetEnvironmentVariable("PathDB", EnvironmentVariableTarget.User);
string DBselect = System.Environment.GetEnvironmentVariable("DBname", EnvironmentVariableTarget.User);
于 2013-11-01T13:32:43.303 に答える