C# System.Diagnostics.ProcessStartInfo 环境变量不区分大小写?

发布于 2024-12-24 03:42:58 字数 559 浏览 2 评论 0原文

当我向其中插入内容时,ProcessStartInfo.EnvironmentVariables(类型StringDictionary)中的EnvironmentVariables 始终设置为小写。例如:

proc.StartInfo.EnvironmentVariables.Clear();
proc.StartInfo.EnvironmentVariables.Add("REDIRECT_STATUS", "");
// [snipped more variables being added here]
proc.Start();

在该过程中现在有一个 redirect_status,而不是我想要的 REDIRECT_STATUS。这会导致问题。

我已经在其他地方读到过,您可以通过制作批处理文件来解决这个问题,但这不适用于我的情况,因为这意味着用作 CGI(此代码可能每秒被调用 10 次。

)有没有办法让 EnvironmentVariables 不区分大小写,而只允许我全部大写?

The EnvironmentVariables in ProcessStartInfo.EnvironmentVariables (type StringDictionary) are always set to lower-case when I insert something into it. For example:

proc.StartInfo.EnvironmentVariables.Clear();
proc.StartInfo.EnvironmentVariables.Add("REDIRECT_STATUS", "");
// [snipped more variables being added here]
proc.Start();

In the process there now is a redirect_status, and not a REDIRECT_STATUS as I wanted. This causes problems.

I already read somewhere else that you could get around this issue by making a batch file, however this is not applicable in my case, as this is meant to be used as CGI (this code could potentially be called 10 times a second.)

Is there a way to get the EnvironmentVariables to not be case insensitive, and just allow me to go all-caps?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

万劫不复 2024-12-31 03:42:58

嗯,请注意所有也遇到此问题的人:显然,这是 .Net 3.5 中的一个错误 并在 4.0 中修复。

我不得不切换到 .Net 4.0 来解决这个问题。

Hmm, note to everybody out there who's also having this problem: Apparantly, this is a bug in .Net 3.5 and is fixed in 4.0.

I had to switch to .Net 4.0 to resolve this issue.

云归处 2024-12-31 03:42:58

Windows 中的环境变量不区分大小写。这个变量的行为显然是基于这个事实。如果您在 Windows 环境中使用环境变量,这不会造成问题。你没有提到它在什么平台上——它是一个在类 Unix 系统上运行的 Mono 应用程序吗?

Environment variables in Windows are case insensitive. The behavior of this variable is clearly based on that fact. If you are using environment variables in a Windows environment, this shouldn't cause trouble. You don't mention what platform this is on - is it a Mono application running on a Unix-like system?

日暮斜阳 2024-12-31 03:42:58

我遇到了同样的问题并发现了这个问题。此时不想将项目升级到 .Net 4,我找到了以下解决方法。此解决方法取决于 StringDictionary 类的内部实现,这样做通常被认为是不好的做法,但它可以在 .Net 2 上完成工作...

System.Reflection.FieldInfo contentsField = typeof(System.Collections.Specialized.StringDictionary).GetField("contents", System.Reflection.BindingFlags.Instance | System.Reflection.BindingFlags.NonPublic);
System.Collections.Hashtable envtable = (System.Collections.Hashtable)contentsField.GetValue(proc.StartInfo.EnvironmentVariables);
envtable.Add("REDIRECT_STATUS", "");

I ran into the same problem and found this question. Not wanting to upgrade the project to .Net 4 at this point, I found the following workaround. This workaround depends on the internal implementation of the StringDictionary class, and doing so is often considered bad practice, but it gets the job done on .Net 2...

System.Reflection.FieldInfo contentsField = typeof(System.Collections.Specialized.StringDictionary).GetField("contents", System.Reflection.BindingFlags.Instance | System.Reflection.BindingFlags.NonPublic);
System.Collections.Hashtable envtable = (System.Collections.Hashtable)contentsField.GetValue(proc.StartInfo.EnvironmentVariables);
envtable.Add("REDIRECT_STATUS", "");
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文