如何设置环境变量? [英] How to set environment variables?

查看:163
本文介绍了如何设置环境变量?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想在c#中使用swi-interface来调用prolog命令。我按照

必须将SWI_HOME_DIREnvironmentVariable设置为swipl目录才能正确运行程序。我将此变量设置如下:

I want to use swi-interface in c# for calling prolog commands.I followed related documentation in
The "SWI_HOME_DIR" EnvironmentVariable must be set to swipl directory to program working correctlly.I set this variable as follows:

Environment.SetEnvironmentVariable("SWI_HOME_DIR", @"C:\Program Files (x86)\swipl\"); 
or Environment.SetEnvironmentVariable("SWI_HOME_DIR", @"C:\Program Files (x86)\swipl\bin\"); 





但是我收到了这样的错误:



but i get such error:

Unhandled Exception: System.IO.FileNotFoundException: The specified module could
 not be found. (Exception from HRESULT: 0x8007007E)
   at System.Runtime.InteropServices.Marshal.ThrowExceptionForHRInternal(Int32 errorCode, IntPtr errorInfo)
   at System.Runtime.InteropServices.Marshal.ThrowExceptionForHR(Int32 errorCode)
   at SbsSW.SwiPlCs.LibPl.LoadUnmanagedLibrary(String fileName)
   at SbsSW.SwiPlCs.PlEngine.Initialize(String[] argv)
   at ConsoleApplication1.Program.Main(String[] args) in g:\testing\\ConsoleApplication1\Program.cs:line 18
Press any key to continue . . .





我将swi主目录添加到应用程序的参考路径,但是有以上错误。

如果coppy所有dll文件(在swipl / bin中)调试目录,该程序可以工作。

还有其他设置environmet变量的方法吗?



I add swi home directory to Refrences Paths of Application,but there is above error.
If coppy all dll files(in swipl/bin) to debug directory, the program works.
is there other way for setting the environmet variable?

推荐答案

如果查看文档: Environment.SetEnvironmentVariable Method (字符串,字符串)(系统) [ ^ ]它非常清楚地表明:

调用此方法相当于调用SetEnvironmentVariable(String,String,EnvironmentVariableTarget)重载用于目标参数的EnvironmentVariableTarget.Process的值。

这意味着您设置的环境变量仅适用于当前进程。

如果 - as它似乎 - 您正在尝试为外部应用程序设置环境变量,然后它将在另一个进程中破坏,并且不会设置环境变量。您可以尝试使用过载 Environment.SetEnvironmentVariable方法(String,String,EnvironmentVariableTarget) (系统) [ ^ ]指定EnvironmentVariableTarget.User或EnvironmentVariableTarget.Machine并查看是否可以解决您的问题。
If you look at the documentation: Environment.SetEnvironmentVariable Method (String, String) (System)[^] it says very clearly that:
"Calling this method is equivalent to calling the SetEnvironmentVariable(String, String, EnvironmentVariableTarget) overload with a value of EnvironmentVariableTarget.Process for the target argument."
Which means that the environment variable you set is only available for the current process.
If - as it seems - you are trying to set an environment variable for an external application then it will ruin in a different process and the environment variable will not be set. You could try using the overload Environment.SetEnvironmentVariable Method (String, String, EnvironmentVariableTarget) (System)[^] an specifying either EnvironmentVariableTarget.User or EnvironmentVariableTarget.Machine and seeing if that fixes your problem.


这篇关于如何设置环境变量?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆