【问题标题】:How to get culture name from a resource path file name如何从资源路径文件名中获取文化名称
【发布时间】:2013-06-06 22:24:33
【问题描述】:

有一个资源文件名 ex:Employee.resx 我必须在同一文件夹中获取其他文化的列表。所以我是这样想的:

private void GetExtraCultures(string resxFileDirectory, string neutralResxFileName)
        {
            string resxFullPath = resxFileDirectory + Path.DirectorySeparatorChar;
            string[] extraResxCulturesPath = Directory.GetFiles(resxFullPath, neutralResxFileName.Replace(".resx", "") + "*" + ".resx");
            List<string> extraResxCulturesList = new List<string>();

            foreach (string extraCulturePath in extraResxCulturesPath)
            {
                string currentFileName = Path.GetFileName(extraCulturePath);
                if (currentFileName != neutralResxFileName)
                {
                    string t = currentFileName.Substring(currentFileName.IndexOf(".") + 1);
                    string tt = t.Substring(0, t.IndexOf("."));

                    extraResxCulturesList.Add(tt);
                }
            }
        }

您有更轻的解决方案吗?例如,对于如何从另一个字符串中取出字符串,我没有解决方案。我的意思是,除了使用子字符串方法之外,如何从这个字符串"Employee.en-US.resx" 中获取en-US? 非常感谢任何更轻/更好的解决方案。

【问题讨论】:

  • 只是一个快速的'n'nasty解决方案:使用string.Split('.', fileName),然后你将有一个字符串数组,如果数组长度为3,那么中间元素包含文化(并不总是连字符)。如果数组长度为 2,则未指定区域性。您也可以使用正则表达式,但对于这么简单的事情来说,这太过分了。
  • 您可以使用 Path.GetExtension(Path.GetExtension(filename)) 来获取“en-Us”。
  • split 方法与我写的相比不会更有效,但是在此期间我做了一些修改。然后 GetExtension 也不会帮助我,因为它返回一个带有点的值,所以我必须再次进行替换或子字符串。
  • 我当前的代码是:string tmp = string.Empty;位置; if ((pos = currentFileName.IndexOf(".")) != -1) tmp = currentFileName.Substring(pos + 1); if ((pos = tmp.IndexOf(".")) != -1) tmp = tmp.Substring(0, pos);

标签: c# string file path


【解决方案1】:

我正在使用

string file = @"i18n\i18n.de-DE.resx";
string culture = Path.GetExtension(Path.GetFileNameWithoutExtension(file)).Substring(1);

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2011-01-25
    • 2011-04-13
    • 1970-01-01
    • 2016-12-29
    • 2015-05-03
    • 1970-01-01
    相关资源
    最近更新 更多