首页 > 代码库 > [C#]ExtractEmbeddedResourceToFile
[C#]ExtractEmbeddedResourceToFile
关键代码:
/// <summary> /// 将嵌入的资源写入到本地 /// </summary> /// <param name="resourceName">嵌入的资源名称</param> /// <param name="filename">写入本地的路径</param> /// <returns>是否成功</returns> public static bool ExtractEmbeddedResourceToFile(string resourceName, string filename) { bool _result = false; try { Assembly _curCall = Assembly.GetCallingAssembly(); string _curCallAssemblyName = _curCall.GetName(false).Name; resourceName = string.Format("{0}.{1}", _curCallAssemblyName, resourceName); using (Stream stream = _curCall.GetManifestResourceStream(resourceName)) { if (stream != null) { using (FileStream fs = new FileStream(filename, FileMode.Create)) { byte[] _byte = new byte[stream.Length]; stream.Read(_byte, 0, _byte.Length); fs.Write(_byte, 0, _byte.Length); _result = true; } } } } catch (Exception ex) { _result = false; Debug.WriteLine(string.Format("ExtractEmbeddedResourceToFile Exception:{0}", ex.Message)); } return _result; }
测试代码:
1.将文件设置成嵌入成资源
2.编写代码:
CSharpToolV2.ExtractEmbeddedResourceToFile("cat.jpg", @"C:\cat.jpg");
希望有所帮助!<style type="text/css">.csharpcode, .csharpcode pre{ font-size: small; color: black; font-family: consolas, "Courier New", courier, monospace; background-color: #ffffff; /*white-space: pre;*/}.csharpcode pre { margin: 0em; }.csharpcode .rem { color: #008000; }.csharpcode .kwrd { color: #0000ff; }.csharpcode .str { color: #006080; }.csharpcode .op { color: #0000c0; }.csharpcode .preproc { color: #cc6633; }.csharpcode .asp { background-color: #ffff00; }.csharpcode .html { color: #800000; }.csharpcode .attr { color: #ff0000; }.csharpcode .alt { background-color: #f4f4f4; width: 100%; margin: 0em;}.csharpcode .lnum { color: #606060; }</style>
<style type="text/css">.csharpcode, .csharpcode pre{ font-size: small; color: black; font-family: consolas, "Courier New", courier, monospace; background-color: #ffffff; /*white-space: pre;*/}.csharpcode pre { margin: 0em; }.csharpcode .rem { color: #008000; }.csharpcode .kwrd { color: #0000ff; }.csharpcode .str { color: #006080; }.csharpcode .op { color: #0000c0; }.csharpcode .preproc { color: #cc6633; }.csharpcode .asp { background-color: #ffff00; }.csharpcode .html { color: #800000; }.csharpcode .attr { color: #ff0000; }.csharpcode .alt { background-color: #f4f4f4; width: 100%; margin: 0em;}.csharpcode .lnum { color: #606060; }</style>
[C#]ExtractEmbeddedResourceToFile