在.Net中查找Unicode字符名

在.Net中查找Unicode字符名,.net,unicode,.net,Unicode,在.Net中有没有办法找出某个字符的Unicode名称 如果没有,是否有一个库可以做到这一点?我认为.NET中没有任何东西可以识别这一点。。。但是有一个。它不是.NET中的内置功能。您可以从Charmap.exe中找到,它在状态栏中显示代码点名称。如果你在自己的程序中需要它,你可以将它编译到你的应用程序中。正如NoBugz和MichaelBray所说,.net不提供任何内置功能来获取字符的Unicode名称 您必须使用Unicode字符数据库中提供的bUnicode.o,它包含所有Unicode

在.Net中有没有办法找出某个字符的Unicode名称


如果没有,是否有一个库可以做到这一点?

我认为.NET中没有任何东西可以识别这一点。。。但是有一个。

它不是.NET中的内置功能。您可以从Charmap.exe中找到,它在状态栏中显示代码点名称。如果你在自己的程序中需要它,你可以将它编译到你的应用程序中。

正如NoBugz和MichaelBray所说,.net不提供任何内置功能来获取字符的Unicode名称

您必须使用Unicode字符数据库中提供的bUnicode.o,它包含所有Unicode 5.2字符信息的完整信息(附件44)

另一种选择是使用windows角色映射,您可以通过Start\App Programs\Accessories\System Tools\Character Map(win+R=>charmap)访问它

您还可以使用Unicode Converter Tools,这是一种开源工具,它还提供了一个用户界面,用于从Unicode UCD获取信息和使用它(附件44)本软件的要点是,您可以将此应用程序的EnterPriseAppUnitdll添加到您的联盟中,并从提供的API中使用


这个程序集包含一些静态方法,该方法提供字符和返回名、十六进制代码、十进制代码等。下面是一个可以立即实现的解决方案,如复制/粘贴/编译

首先,在此处下载Unicode数据库(UCD):

接下来,将此代码添加到您的项目中,以读取UCD并创建用于查找.NET字符值名称的字典:

string[] unicodedata = File.ReadAllLines( "UnicodeData.txt", Encoding.UTF8 );
Dictionary<char,string> charname_map = new Dictionary<char,string>( 65536 );
for (int i = 0; i < unicodedata.Length; i++)
{
    string[] fields = unicodedata[i].Split( ';' );
    int char_code = int.Parse( fields[0], NumberStyles.HexNumber );
    string char_name = fields[1];
    if (char_code >= 0 && char_code <= 0xFFFF) //UTF-16 BMP code points only
    {
        bool is_range = char_name.EndsWith( ", First>" );
        if (is_range) //add all characters within a specified range
        {
            char_name = char_name.Replace( ", First", String.Empty ); //remove range indicator from name
            fields = unicodedata[++i].Split( ';' );
            int end_char_code = int.Parse( fields[0], NumberStyles.HexNumber );
            if (!fields[1].EndsWith( ", Last>" ))
                throw new Exception( "Expected end-of-range indicator." );
            for (int code_in_range = char_code; code_in_range <= end_char_code; code_in_range++)
                charname_map.Add( (char)code_in_range, char_name );
        }
        else
            charname_map.Add( (char)char_code, char_name );
    }
}
string[]unicodedata=File.ReadAllLines(“unicodedata.txt”,Encoding.UTF8);
字典charname_map=新字典(65536);
for(int i=0;i如果(char\u code>=0&&char\u code如果您使用Process Monitor查看由
charmap.exe
访问的文件,您将看到它打开一个名为
C:\Windows\system32\getuname.dll
的文件。此文件的资源中包含字符名(实际上,资源本身位于区域性特定子目录中的.mui文件中)

因此,您所要做的就是使用
LoadString
API从该文件中获取名称。我编写了一个帮助器类来实现这一点:

public class Win32ResourceReader : IDisposable
{
    private IntPtr _hModule;

    public Win32ResourceReader(string filename)
    {
        _hModule = LoadLibraryEx(filename, IntPtr.Zero, LoadLibraryFlags.AsDataFile | LoadLibraryFlags.AsImageResource);
        if (_hModule == IntPtr.Zero)
            throw Marshal.GetExceptionForHR(Marshal.GetHRForLastWin32Error());
    }

    public string GetString(uint id)
    {
        var buffer = new StringBuilder(1024);
        LoadString(_hModule, id, buffer, buffer.Capacity);
        if (Marshal.GetLastWin32Error() != 0)
            throw Marshal.GetExceptionForHR(Marshal.GetHRForLastWin32Error());
        return buffer.ToString();
    }

    ~Win32ResourceReader()
    {
        Dispose(false);
    }

    public void Dispose()
    {
        Dispose(true);
        GC.SuppressFinalize(this);
    }

    public void Dispose(bool disposing)
    {
        if (_hModule != IntPtr.Zero)
            FreeLibrary(_hModule);
        _hModule = IntPtr.Zero;
    }

    [DllImport("user32.dll", CharSet = CharSet.Auto, SetLastError = true)]
    static extern int LoadString(IntPtr hInstance, uint uID, StringBuilder lpBuffer, int nBufferMax);

    [DllImport("kernel32.dll", CharSet = CharSet.Auto, SetLastError = true)]
    static extern IntPtr LoadLibraryEx(string lpFileName, IntPtr hReservedNull, LoadLibraryFlags dwFlags);

    [DllImport("kernel32.dll", CharSet = CharSet.Auto, SetLastError = true)]
    static extern bool FreeLibrary(IntPtr hModule);

    [Flags]
    enum LoadLibraryFlags : uint
    {
        AsDataFile = 0x00000002,
        AsImageResource = 0x00000020
    }
}
您可以这样使用它:

char c = 'Â';
string character_name;
if (!charname_map.TryGetValue( c, out character_name ))
    character_name = "<Character Name Missing>"; //character not found in map
//character_name should now contain "LATIN CAPITAL LETTER A WITH CIRCUMFLEX";
string path = @"C:\Windows\System32\getuname.dll";
using (var reader = new Win32ResourceReader(path))
{
    string name = reader.GetString(0xA9);
    Console.WriteLine(name); // Copyright Sign
}

现在比以往任何时候都容易,因为nuget中有一个名为

使用此功能,您只需拨打:

UnicodeInfo.GetName(character)

非常好(至少如果你手头有Windows的话)。另一个优点是它内置在Windows中(即使在XP上)。缺点是字符串是本地化的,并且包含一个字符串中的所有信息。而不是给我信息“名称”和“类别”分别返回一个字符串中的所有内容。例如,在西班牙语窗口上:“Letra latina mayúscula G con gancho”。因此,我更喜欢Rik Hemsley的公认答案。他的库提供了更多信息。