我正在使用 mono 和 xamarin 作为 IDE 在 mac 上运行一个 c# 应用程序。 在我的主要方法中,我有:
[DllImport("kernel32.dll", CharSet = CharSet.Auto, SetLastError = true, ExactSpelling=false)]
public static extern bool SetEnvironmentVariable(string lpName, string lpValue);
然后当我调用它时:
SetEnvironmentVariable("PATH", path);
我得到一个 EntryPointNotFoundException
System.EntryPointNotFoundException: SetEnvironmentVariable
at at (wrapper managed-to-native) EICService.Program:SetEnvironmentVariable (string,string)
at EICService.Program.Main (System.String[] rawArgs) [0x00031] in (*my path*)/Program.cs:34
现在我知道在 Mac 上调用 kernel32.dll 是个问题,但我假设它正在找到它或任何 mono 有作为它的替代品,因为如果我在那里放一些虚拟 dll 文件名,我会得到一个 dll not found错误。
我对 C# 非常陌生,但这段代码在 Windows 上运行,我们只想让它在 Unix 上运行。
有什么想法吗?
最佳答案
这是一个 Windows API 函数。它根本不存在于 Mac 上。因此错误。
您不需要 p/调用此函数来设置进程环境中的变量。您可以使用 native .net 功能。即 Environment.SetEnvironmentVariable
。
此外,您应该在 Windows 上执行完全相同的操作。 p/调用 native 函数来执行 .net 运行时库提供的功能绝不是一个好主意。
关于c# - kernel32.dll SetEnvironmentVariable 中的 EntryPointNotFoundException,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32848259/