将来自不同文化的 DateTime 字符串传递给服务
Passing DateTime strings from different cultures to a service
我有一个包含 Windows 10 桌面应用程序项目和服务项目的解决方案。桌面应用程序启动服务并传递一个参数,该参数是一个 DateTime
转换为字符串的参数。在服务中,字符串被传递给 DateTime.TryParse(string, out DateTime)
但这一直失败。我的测试系统的文化是德国的,但我意识到在服务中,文化似乎总是 en-US(我认为是因为它在 SYSTEM 上下文中是 运行ning)。我知道我的申请将在全球 16 个不同的国家 运行。我在想我是否能以某种方式获得用户文化并在我的服务中使用此信息来使用 DateTime.TryParse
的第二个重载,我可以在其中指定 IFormatProvider
但这是最好的解决方案吗?我基本上需要一个强大的解决方案来将来自不同文化的 DateTime 字符串传递到我的服务,以便我可以进一步使用它。在将 DateTime
字符串传递给服务之前根据服务文化对其进行格式化是否可以接受?
The Desktop Application starts the service and passes an argument which is a DateTime converted to a String.
您可以使用 InvariantCulture 在应用程序和服务之间进行通信:
The InvariantCulture is useful for storing data that will not be displayed directly to end users. Storing data in a culture-independent format guarantees a known format that does not change. When users from different cultures access the data, it can be formatted appropriately based on the user. For example, if you store DateTime types in a text file, formatted for the InvariantCulture, use the InvariantCulture property when you call the DateTime.ToString method to store the strings and the Date.Parse method to retrieve the strings. This will ensure that the underlying values of the DateTime types do not change when the data is read or written by users from different cultures.
我有一个包含 Windows 10 桌面应用程序项目和服务项目的解决方案。桌面应用程序启动服务并传递一个参数,该参数是一个 DateTime
转换为字符串的参数。在服务中,字符串被传递给 DateTime.TryParse(string, out DateTime)
但这一直失败。我的测试系统的文化是德国的,但我意识到在服务中,文化似乎总是 en-US(我认为是因为它在 SYSTEM 上下文中是 运行ning)。我知道我的申请将在全球 16 个不同的国家 运行。我在想我是否能以某种方式获得用户文化并在我的服务中使用此信息来使用 DateTime.TryParse
的第二个重载,我可以在其中指定 IFormatProvider
但这是最好的解决方案吗?我基本上需要一个强大的解决方案来将来自不同文化的 DateTime 字符串传递到我的服务,以便我可以进一步使用它。在将 DateTime
字符串传递给服务之前根据服务文化对其进行格式化是否可以接受?
The Desktop Application starts the service and passes an argument which is a DateTime converted to a String.
您可以使用 InvariantCulture 在应用程序和服务之间进行通信:
The InvariantCulture is useful for storing data that will not be displayed directly to end users. Storing data in a culture-independent format guarantees a known format that does not change. When users from different cultures access the data, it can be formatted appropriately based on the user. For example, if you store DateTime types in a text file, formatted for the InvariantCulture, use the InvariantCulture property when you call the DateTime.ToString method to store the strings and the Date.Parse method to retrieve the strings. This will ensure that the underlying values of the DateTime types do not change when the data is read or written by users from different cultures.