为什么double.Parse()仅通过rdp抛出FormatException? [英] Why double.Parse() throws FormatException only through rdp?

查看:41
本文介绍了为什么double.Parse()仅通过rdp抛出FormatException?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个很奇怪的错误.我有一个.Net应用程序,该应用程序在将字符串解析为双精度时会出现异常.但这只是通过RDP发生!

我做了一个简单的exe:

 静态void Main(string [] args){尝试{字符串s ="1.5";double d = double.Parse(s);Console.WriteLine("RES:" + d);Console.Read();}捕获(异常e){Console.WriteLine("Message:" + e.Message +,trace:" + e.StackTrace);Console.Read();}} 

当我在本地执行该命令时,在RDP中我有一个错误

有什么主意吗?该计算机是Windows 2012R2,也是域控制器.我尝试用相同的用户执行这个小应用程序,唯一的区别是RDP/Local

谢谢您的帮助

解决方案

double.Parse 使用您的 InvariantCulture 作为解析方法中的第二个参数,该参数具有.作为 NumberDecimalSeparator .

  double d = double.Parse(s,CultureInfo.InvariantCulture); 

或者您可以将远程环境的区域性设置更改为在本地环境中使用的相同区域性.

I have a realy weird error. I have a .Net application, that make an exception while parsing a string to a double. But this is happening only trough RDP!

I made a simple exe:

static void Main(string[] args)
{
     try
     {
          string s = "1.5";
          double d = double.Parse(s);
          Console.WriteLine("RES: " + d);
          Console.Read();
     }
     catch (Exception e)
     {
          Console.WriteLine("Message: "+e.Message+", trace: "+e.StackTrace);
          Console.Read();
     }
}

When I execute on local it works, ut in RDP i have an error

Any idea? the machine is a Windows 2012R2, that is also a domain controller. I try to execute this little app with the same user, only difference is RDP/Local

Thank you for your help

解决方案

double.Parse uses your CurrentCulture settings on the current environment by default.

Sounds like your remote environment uses a culture that doesn't have . as a NumberDecimalSeparator, that's why you get FormatException.

As an anternative, you can use InvariantCulture as a second parameter in your parsing method which has . already as a NumberDecimalSeparator.

double d = double.Parse(s, CultureInfo.InvariantCulture);

Or you can change the culture settings of your remote environment the same culture using in your local environment.

这篇关于为什么double.Parse()仅通过rdp抛出FormatException?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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