简体   繁体   中英

Convert string to current culture in c#

I have to get instance of "Windows Firewall" windows service, for that am using

ServiceController service = new ServiceController("Windows Firewall");

This works fine in en-US culture, but when I try to do that in Windows Portuguese(Brazil) pt-PT culture, it throws exception Service not found.

Yes, In Portuguese there is no "Windows Firewall" instead "Firewall do Windows", If I change my code like below

ServiceController service = new ServiceController("Firewall do Windows"); 

it will work but only in pt-PT culture.

Is there any generic way, to get "Windows Firewall" instance irrespective of culture?

There's no system provided function to perform such a conversion. You'd have to create a list/dictionary of all possible values in your own code, and look up the value.

The ServiceController Constructor has the following parameter description:

name

Type: System.String

The name that identifies the service to the system. This can also be the display name for the service.

In other words: A service is identified either through its language-invariant Service name , or the localized Display name . Your code uses the display name ( Windows Firewall ). If you change that to use the service name ( MpsSvc ) instead, you won't run into localization issues.

You can look up a service's service name and display name through the Computer Management Console (compmgmt.msc). Rightclicking on a service and selecting Properties will reveal both names.

Honestly, I just learn a display name of a windows service can be culture specific :-p

I totally agree with David . Creating your own key/value list sounds as a best option. On the other hand, there might be some ways to get your culture specific display name. WARNING! I am not 100% sure this will work ALWAYS!

Instead of display name, how about using it's service name and get it's display name based on it? I don't think it will be culture specific. For example, with ServiceController.GetServices method;

var displayName = ServiceController.GetServices()
            .Where(s => s.ServiceName == "MpsSvc")
            .Select(s => s.DisplayName)
            .ToList()[0];

ServiceController sc = new ServiceController(displayName);

I searched MpsSvc for an hour for maybe it might be used for another service as a name but I couln't find anything about that.

Also you can use WMI to get it like;

var searcher = new ManagementObjectSearcher("SELECT * FROM Win32_Service");
ManagementObjectCollection collection = searcher.Get();
var obj = collection.Cast<ManagementObject>()
                      .Where(o => o.Path.Path.Contains("MpsSvc"))
                      .ToList()[0];

string displayName = obj["DisplayName"].ToString();

ServiceController sc = new ServiceController(displayName);

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM