记录:批量Resource转Resx

本文介绍了一个简单的.NET程序,用于批量将资源文件(.resources)转换为ResX格式。通过遍历指定目录下的所有资源文件,并利用自定义的扩展方法去除文件名前后缀,实现了资源文件的高效转换。

        static void Main(string[] args)
        {
            
string pResources = @"D:\Resource\";
            DirectoryInfo diResources 
= new DirectoryInfo(pResources);
            var queryList 
= diResources.GetFiles();
            
foreach (var ary in queryList)
            {
                TestToResources(pResources 
+ ary.Name, @"D:\ResX\" + ary.Name.Replace("resources.""").Replace(".resources"".resx"));
            }
        }

        
static void TestToResources(string strResource, string strResx)
        {
            ResourceReader reader 
= new ResourceReader(strResource);
            ResXResourceWriter writer 
= new ResXResourceWriter(strResx);

            
foreach (DictionaryEntry en in reader)
            {
                writer.AddResource(en.Key.ToString(), en.Value);
            }
            reader.Close();
            writer.Close();
        }

这里可以使用Artech的扩展方法

 1: namespace System
 2: {
 3: public static class StringExtensions
 4: {
 5: public static string TrimPrefix(this string sourceString, string prefix, bool ignoreCase = false)
 6: {
 7: prefix = prefix ?? string.Empty;
 8: if(!sourceString.StartsWith(prefix,ignoreCase, CultureInfo.CurrentCulture))
 9: {
 10: return sourceString;
 11: }
 12:  
 13: return sourceString.Remove(0, prefix.Length);
 14: }
 15:  
 16: public static string TrimSuffix(this string sourceString, string suffix, bool ignoreCase = false)
 17: {
 18: suffix = suffix ?? string.Empty;
 19: if (!sourceString.EndsWith(suffix, ignoreCase, CultureInfo.CurrentCulture))
 20: {
 21: return sourceString;
 22: }
 23: return sourceString.Substring(0, sourceString.Length - suffix.Length);
 24: }
 25: }
 26: }

程序调用:

 1: static void Main(string[] args)
 2: {
 3: string sourceString = "ABC123";
 4: Console.WriteLine(sourceString.TrimPrefix("ABC"));
 5: Console.WriteLine(sourceString.TrimSuffix("123"));
 6: }

输出结果:

 1: 123
 2: ABC

转载于:https://www.cnblogs.com/leeolevis/archive/2011/03/13/1982720.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值