C# 如何从资源路径文件名中获取区域性名称
有一个资源文件名ex:Employee.resx,我必须在同一文件夹中获得一个包含其他区域性的列表。所以我就这样想,C# 如何从资源路径文件名中获取区域性名称,c#,string,file,path,C#,String,File,Path,有一个资源文件名ex:Employee.resx,我必须在同一文件夹中获得一个包含其他区域性的列表。所以我就这样想, private void GetExtraCultures(string resxFileDirectory, string neutralResxFileName) { string resxFullPath = resxFileDirectory + Path.DirectorySeparatorChar; st
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?
非常感谢任何更轻/更好的解决方案 我正在使用
string file = @"i18n\i18n.de-DE.resx";
string culture = Path.GetExtension(Path.GetFileNameWithoutExtension(file)).Substring(1);
只是一个快速的解决方案:使用string.Split.”,fileName,那么您将拥有一个字符串数组,如果数组长度为3,那么中间的元素包含的区域性将不总是连字符。如果数组长度为2,则未指定区域性。您也可以使用正则表达式,但对于这么简单的东西来说,这太过分了。您可以使用Path.GetExtensionPath.GetExtensionfilename来获取en-Us.split方法,与我编写的方法相比效率不会更高,但是在这段时间里,我做了一些修改。那么GetExtension也帮不了我,因为它返回一个带点的值,所以我必须再次进行替换或子字符串。我当前的代码是:string tmp=string.Empty;int pos;如果pos=currentFileName.IndexOf.!=-1 tmp=currentFileName.Substringpos+1;如果位置=tmp.IndexOf.!=-1 tmp=tmp.Substring0,位置;