Copying embedded resource as file to disk in C#

佐手、 提交于 2019-11-30 13:32:55

You could call

System.Reflection.Assembly.GetExecutingAssembly().GetManifestResourceNames();

And inspect which embedded resources are accessible. Then you can compare that against what you are passing in to see if you are indeed accomplishing what you expected to.

Also, you should consider the using keyword to dispose of your streams:

using(FileStream ResourceFile = new FileStream(FileToExtractTo, FileMode.Create))
{
    //do stuff
}

Good luck.

This is the easiest way to save an embedded resource:

  var stream = assembly.GetManifestResourceStream("name of the manifest resourse");
  var fileStream = File.Create(@"C:\Test.xml");
  stream.Seek(0, SeekOrigin.Begin);
  stream.CopyTo(fileStream);
  fileStream.Close();

Unpacks all embedded resources within an assembly and saves them alongside the assembly containing this class while maintaining the directory structure. This code does not require you to know anything about the files contained within the assembly and is a more generic solution but it does assume that all files have file extensions.

public class EmbeddedResources
{
    private bool isUnpacked = false;

    public async Task EnsureUnpacked(string saveDirectory)
    {
        if (!this.isUnpacked)
        {
            var assembly = Assembly.GetExecutingAssembly();
            var assemblyDirectory = Path.GetDirectoryName(assembly.Location);
            foreach (var name in assembly.GetManifestResourceNames())
            {
                var stream = assembly.GetManifestResourceStream(name);

                var stringBuilder = new StringBuilder();
                var parts = name
                    .Replace(typeof(EmbeddedResources).Namespace + ".", string.Empty)
                    .Split('.')
                    .ToList();
                for (int i = 0; i < parts.Count; ++i)
                {
                    var part = parts[i];
                    if (string.Equals(part, string.Empty))
                    {
                        stringBuilder.Append(".");      // Append '.' in file name.
                    }
                    else if (i == parts.Count - 2)
                    {
                        stringBuilder.Append(part);     // Append file name and '.'.
                        stringBuilder.Append('.');
                    }
                    else if (i == parts.Count - 1)
                    {
                        stringBuilder.Append(part);     // Append file extension.
                    }
                    else
                    {
                        stringBuilder.Append(part);     // Append file path.
                        stringBuilder.Append('\\');
                    }
                }

                var filePath = Path.Combine(saveDirectory, stringBuilder.ToString());
                using (FileStream fileStream = new FileStream(filePath, FileMode.OpenOrCreate, FileAccess.ReadWrite))
                {
                    await stream.CopyToAsync(fileStream);
                }
            }

            this.isUnpacked = true;
        }
    }
}
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!