c# - kernel32.dll SetEnvironmentVariable 中的 EntryPointNotFoundException

标签 c# xamarin mono

我正在使用 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/

相关文章:

c# - NavigationWindow - 它在哪里?

c# - 使用 MySql 在 Entity Framework 6 中延迟加载数据不起作用

c# - 如何在 C# 中制作音频流? (一般的)

swift - 如何在具有多行自动换行的 UITextView 中找到光标 Y 位置?

ios - MonoTouch UIImage.FromFile 不加载视网膜 Assets

android - 为什么 Xamarin.Forms WebView 会增加 Android 应用程序存储数据大小?

来自 monodevelop 的 iPhone 应用程序开发

c# - Windows 窗体安全 C#

c# - 没有死的单声道友好图形库

F# StackOverflow in mono with continuations(启用尾调用消除)