反编译winform资源文件

winform程序,经过.net reflector反编译之后,产生.resources文件不便于VS识别,不能重新编译。在此简单做了一个小工具,就是把.resources文件转化为.resx文件。同时支持批量转换。即一次性浏览选择多个文件,生成之后所有.resx文件放于文件夹resx下面。生成.resx文件之后,一定要拷贝放在相应.cs文件同级目标下,VS就会自动关联起来。然后就可以做相应修改,重编译工作。

在这里我仅是学习性尝试做一个简单的小工具,同时也是拿自己的程序做测试,当然你可尝试反编译别人.net程序,先用reflector反编译之后,再用此工具转化资源文件,但我不支持大家去反编译别人的东西,毕竟要尊重别人的劳动成果和保护知识产权。废话多说了,下面请看界面。

程序界面如下所示:

选择多个文件

转换成功显示如下图所示:

程序代码如下:

View Code
 private void btnOK_Click(object sender, EventArgs e)
        {
            
string basePath = string.Empty;
            
string sFile = string.Empty; //不带路径的文件名
            string newFold = "resx";//新文件夹名称
            string savePath = string.Empty;
            
int len = 0;
            
int index = -1;
            
string strResx = string.Empty;
            
if (files != null && files.Length > 0)
            {
                sFile 
= files[0];
                index 
= sFile.LastIndexOf("\\");
                basePath 
= sFile.Substring(0, index);//基路径
                savePath = string.Format("{0}\\{1}", basePath, newFold);
                
if (!System.IO.Directory.Exists(savePath))
                {
                    Directory.CreateDirectory(savePath);
                }
                ResourceReader reader 
= null;
                ResXResourceWriter writer 
= null;
                
foreach (string strResources in files)
                {
                    
//strResx = sFile.Replace(".resources", ".resx");
                    sFile = strResources;
                    index 
= sFile.LastIndexOf("\\");
                    len 
= sFile.Length;
                    
if (index > -1 && len > index)
                    {
                        strResx 
= sFile.Substring(index + 1, len - index - 1).Replace(".resources"".resx");
                    }
                    strResx 
= string.Format("{0}\\{1}", savePath, strResx);
                    reader 
= new ResourceReader(strResources);
                    writer 
= new ResXResourceWriter(strResx);
                    
foreach (DictionaryEntry en in reader)
                    {
                        writer.AddMetadata(en.Key.ToString(), en.Value);
                    }
                    
if (reader != null)
                    {
                        reader.Close();
                    }
                    
if (writer != null)
                    {
                        writer.Close();
                    }
                }
                MessageBox.Show(
"转换资源文件转换成功""系统提示");
                txtResources.Text 
= string.Empty;
            }
            
else
            {
                MessageBox.Show(
"待转换资源文件不能为空""系统提示");
            }

            
// string strResources = this.txtResources.Text;
            
// string strResx = this.txtResx.Text;
            
//if (string.IsNullOrEmpty(strResources) || string.IsNullOrEmpty(strResx))
            
//{
            
//    return;
            
//}
            
//ResourceReader reader = new ResourceReader(strResources);
            
//ResXResourceWriter writer = new ResXResourceWriter(strResx);

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

整个工程源码下载如下:

/Files/cgli/ConvertResource.rar

转载请保留原文地址 http://www.cnblogs.com/cgli/archive/2011/04/28/2032262.html

原文地址:https://www.cnblogs.com/cgli/p/2032262.html