在C#中提取嵌入式资源 [英] Extract embedded resources in C#

查看:87
本文介绍了在C#中提取嵌入式资源的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在C#可执行文件中嵌入了四个资源文件,1个python脚本和3个perl脚本.我可以成功提取所有三个perl脚本.但是我无法提取python脚本.我尝试了很多方法.有人可以看看吗?谢谢.

I have four resource files embedded in my C# executable, 1 python script and 3 perl scripts. I could extract all three perl scripts successfully. But I am not able to extract the python script. I tried so many ways. Could someone please have a look ? Thank you.

public static string ExtractResource(string resourceName)
{
    string destFile = "";

    //look for the resource name
    foreach (string currentResource in System.Reflection.Assembly.GetExecutingAssembly().GetManifestResourceNames() )
        if (currentResource.LastIndexOf(resourceName) != -1)
        {
            string subPath = Common_Utilities.GetTempPath() + "SCRIPTS"; 
            bool isExists = System.IO.Directory.Exists(subPath);

            if (!isExists)
               System.IO.Directory.CreateDirectory(subPath);

            string strFile = subPath + "\\" + resourceName;
            string path = System.IO.Path.GetDirectoryName(strFile);
            string rootName = System.IO.Path.GetFileNameWithoutExtension(strFile);
            destFile = path + @"\" + rootName + System.IO.Path.GetExtension(currentResource);

            System.IO.Stream fs = System.Reflection.Assembly.GetExecutingAssembly().GetManifestResourceStream( currentResource ) ;

            byte[] buff = new byte[fs.Length];
            fs.Read(buff, 0, (int)fs.Length);
            fs.Close();

            System.IO.FileStream destStream = new System.IO.FileStream(destFile, FileMode.Create);
            destStream.Write(buff, 0, buff.Length);
            destStream.Close();
    }

    return destFile;
    // throw new Exception("Resource not found : " + resourceName);
}

推荐答案

不确定为什么无法提取Python脚本....几点:

Not sure why the Python script can't be extracted.... couple points though:

  1. 我建议使用 Path.Combine()粘贴路径和文件名-不要自己做,错误的机会太多!

  1. I would recommend to use Path.Combine() to stick together path and file name - don't do this yourself, too many chances for error!

由于这些是(基于文本的)脚本,因此可以简化整个复制过程:

Since those are (text-based) scripts, you could do the whole copying much simpler:

 System.IO.Stream fs = System.Reflection.Assembly.GetExecutingAssembly().GetManifestResourceStream(currentResource);

 string scriptContents = new StreamReader(fs).ReadToEnd();
 File.WriteAllText(destFile, scriptContents); 

使用这种方法,您应该可以轻松地在调试中查看是否从资源正确加载了脚本.如果不是,请检查您的资源名称等(脚本是否真的设置为嵌入"资源?).如果您有资源的子目录,请注意,资源名称将包含这些子目录作为完全限定名称的一部分-但用点号(.)分隔,而不是像物理路径一样的反斜杠!

With this approach, you should be easily able to see in debugging whether or not the script is properly loaded from resources. If not - check your resource name etc. (is the script really set to "embedded" resource?). If you have subdirectories for your resources, be aware that the resource name will contain those subdirectories as part of the fully qualified name - but separated by a dot (.), not a backslash like a physical path!

这篇关于在C#中提取嵌入式资源的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆