C#可以将DLL嵌套在微信小程序嵌套循环中吗

如何在c#的dll中嵌入txt文件,并且把文件的内容读取出来?_百度知道
如何在c#的dll中嵌入txt文件,并且把文件的内容读取出来?
最好能写出一个示例代码段。1.不懂的不要瞎嚷嚷。2.不要乱沾乱贴。
我有更好的答案
实现方法如下:(1)新建一个文本文件Message.txt。这个文件将作为资源嵌入到DLL中(2)在Visual Studio中创建一个“类库”项目。项目名称:ClassLibrary1(3)在解决方案资源管理器中,选中项目ClassLibrary1--&项目--&ClassLibrary1 属性在 ClassLibrary1属性设置页中鼠标单击 资源--& “……单击此处可创建一个”鼠标单击& 添加资源--&添加现有文件在对话框中选择(1)创建的文本文件Message.txt文本文件导入后,资源中增加了 一项 Message至此,文本文件Message.txt已经成功导入!(4)获取文本文件的内容打开Class1.cs,编写以下代码using&Susing&System.Collections.Gusing&System.Lusing&System.Tnamespace&ClassLibrary1{&&&&public&class&Class1&&&&{&&&&&&&&///&&summary&&&&&&&&&///&获取资源中文本文件的内容&&&&&&&&///&&/summary&&&&&&&&&///&&returns&文本文件内容&/returns&&&&&&&&&public&string&GetTextFromResource()&&&&&&&&{&&&&&&&&&&&&return&Properties.Resources.M&&&&&&&&}&&&&}}(5)编译 ClassLibrary1 项目,生成 ClassLibrary1.dll==================================================以下为测试项目,测试 ClassLibrary1.dll(1)在Visual Studio中新建一个“控制台应用程序”(2)添加对 ClassLibrary1.dll 的引用在解决方案资源管理器中,选中ConsoleApplication1项目,鼠标点击菜单 项目--&添加引用在“引用管理器”对话框中,点击& 浏览--& 定位到 ClassLibrary1.dll --& 添加(3)Program.csusing&Susing&System.Collections.Gusing&System.Lusing&System.Tnamespace&ConsoleApplication1{&&&&class&Program&&&&{&&&&&&&&static&void&Main(string[]&args)&&&&&&&&{&&&&&&&&&&&&ClassLibrary1.Class1&c&=&new&ClassLibrary1.Class1();&&&&&&&&&&&&string&text&=&c.GetTextFromResource();&&&&&&&&&&&&Console.WriteLine(text);&&&&&&&&}&&&&}}(4)运行效果成功地从DLL中读出了文本文件内容!
采纳率:87%
来自团队:
提条件是xml。
你可以查看Resource1.Designer,然后为资源文件添加现有文件。首先给项目添加资源文件将txt文件作为资源嵌入到dll程序集中就可以了,选择你要的txt文件。添加完了.cs,可以看到以txt文件名命名的属性。Resource1
为您推荐:
其他类似问题
txt文件的相关知识
换一换
回答问题,赢新手礼包
个人、企业类
违法有害信息,请在下方选择后提交
色情、暴力
我们会通过消息、邮箱等方式尽快将举报结果通知您。I have a C# application (project A) that requires X.dll. I have added the project that produces X.dll to A as a reference in visual studio. I have also added the release build of X.dll to a resource file in A as a binary. I have told project A not to copy X.dll to the output directory.
Now I want A.exe to load, say "hey I can't find this file", then look in the resource file and use Assembly.Load(byte[]) get X.dll back. I have the code that re-magicifies the DLL back, however this code never get called.
Currently I have a bone simple project, just trying to get it to work. It compile OK. When I run it, I get a FileNotFoundException on X.dll.
[STAThread]
static void Main()
AppDomain.CurrentDomain.AssemblyResolve += new ResolveEventHandler(CurrentDomain_AssemblyResolve);
But the breakpoint in *CurrentDomain_AssemblyResolve* never gets hit. I get a FileNotFoundException immediately. Surely there is something I a missing?
解决方案 This question seems to be very similar to what you are trying to achieve, and it worked for the asker.
Are you doing something differently? Specifically, if you're targetting an older version of the .NET Framework, maybe that's a hint for understanding why your application is behaving differently.
Another direction, use a tool such as
to analyze what was going on when you tried to load the assembly. This may give some more hints. If you manage to get log information, posting it in the question may help someone figure it out.
EDIT: Another explanation, following your comment.
Well, now I think I know what the problem is.
In your Main method, you are refering to the type in the other dll. But you are doing it in static code, i.e. you use the type explicitly in the code (as opposed to loading it dynamically by its name).
Why is this a problem? The CLR tries to load your assembly in order to JIT Main itself.
Your FileNotFoundException was thrown while Main was being compiled. Main didn't even start running, and therefore your event handler wasn't registered.
A simple way to check if I'm right is to change the code to something like this:
static public void Main(string[] args)
AppDomain.CurrentDomain.AssemblyResolve += MyEventH
// The CLR will actually try to load your assembly before even starting the execution
// of this method. It needs the assembly in order to JIT the method because it has to
// know the Thing type.
static public void MyMain()
using(var thing = new Thing())
The important difference is that Main has no dependency on another assembly, so there will be no problem to JIT and run it.
By the time MyMain is being JIT'ed, your event handler is already in place so you can manually load the assembly.
By the way, to avoid another possible similar pitfall, make sure that the class in which Main is defined doesn't have any field with a type from the other assembly, because in this case also, the CLR will try loading the assembly before Main starts, in order to compile it.
本文地址: &
我有需要X.dll C#应用程序(项目A)。我已经加入产生X.dll到A在Visual Studio中的参考项目。我还增加了X.dll的发行版本在一个资源文件为二进制。我已经告诉项目中的不应用于X.dll复制到输出目录。
现在我想A.exe时加载,说:“嘿,我无法找到这个文件”,然后查看资源文件并使用的Assembly.Load(字节[ ])获得X.dll回来。我有一个重新的magicifies代码中的DLL回来,但是这代码永远不会被调用。
目前我有一个骨简单的项目,只是想获得它的工作。它编译OK。当我运行它,我得到X.dll出现FileNotFoundException
[STAThread] 静态无效的主要() { AppDomain.CurrentDomain.AssemblyResolve + =新ResolveEventHandler(CurrentDomain_AssemblyResolve); }
但在* CurrentDomain_AssemblyResolve断点*从未被击中。我立即得到一个FileNotFoundException异常。当然,也有一些是我失踪?
解决方案 这个问题似乎很相似,你想达到什么目的,它就职于提问者。
您做不同的东西吗?特别是,如果你在目标定位在.NET Framework的旧版本,也许这就是理解为什么你的应用程序行为不同的暗示。
另一个方向,使用工具如来分析发生了什么事情,当你试图加载程序集。这可能给一些提示。如果你设法让日志信息,张贴在问题可以帮助别人找到答案
编辑:另一种解释,下面您的评论
好了,现在我想我知道是什么问题。
在你的主方法,你指的是在其它的DLL类型。但你在做它的静态的代码,即您在代码中明确使用类型(而不是它的名字动态加载它)。
为什么这是一个问题吗?在CLR试图以加载程序集的为JIT 的主本身。
您而主正在编译 FileNotFoundException异常被抛出。 主甚至没有开始运行,因此事件处理程序未注册。
有一个简单的方法来检查,如果我是对的是代码更改为类似这样的:
静态公共无效的主要(字串[] args)
{ AppDomain.CurrentDomain.AssemblyResolve + =一个MyEventH
MyMain(); }
// CLR将实际尝试甚至开始执行 //此方法之前加载程序集。它需要以JIT方法,因为它有 //知道的事类型的程序集。 静态公共无效MyMain() {使用(VAR的事情=新的东西()) { // ... } }
重要的区别是,主对另一个程序集没有依赖性,所以会有以JIT没有问题,运行它。
到了时间 MyMain 正在JIT'ed,事件处理程序已经到位,所以你可以手动加载程序集。
顺便说一句,以避免另一个可能类似的陷阱,使确保在主被定义的类没有任何现场与来自其它组件的类型,因为在这种情况下,CLR将尝试之前加载程序集主开始,以对其进行编译。
本文地址: &
扫一扫关注官方微信温馨提示!由于新浪微博认证机制调整,您的新浪微博帐号绑定已过期,请重新绑定!&&|&&
Q:, 微:iJasonLee, M:
经验:HP(惠普)云渲染开发, 游戏技术美术, 游戏场景制作, 照明工程设计, 建筑效果图/动画, 室内外设计, MAX/CAD讲师,
技能:熟悉CAD, 3dsMax, Photoshop, Maya, VRay, Unity3D, 云渲染等,maxScript, Python, C#, bat, Javascript... {Hello, World.}
LOFTER精选
网易考拉推荐
用微信&&“扫一扫”
将文章分享到朋友圈。
用易信&&“扫一扫”
将文章分享到朋友圈。
阅读(12960)|
用微信&&“扫一扫”
将文章分享到朋友圈。
用易信&&“扫一扫”
将文章分享到朋友圈。
历史上的今天
在LOFTER的更多文章
loftPermalink:'',
id:'fks_082065',
blogTitle:'C#将exe运行程序嵌入到自己的winform窗体中',
blogAbstract:'
{if x.moveFrom=='wap'}
{elseif x.moveFrom=='iphone'}
{elseif x.moveFrom=='android'}
{elseif x.moveFrom=='mobile'}
${a.selfIntro|escape}{if great260}${suplement}{/if}
{list a as x}
推荐过这篇日志的人:
{list a as x}
{if !!b&&b.length>0}
他们还推荐了:
{list b as y}
转载记录:
{list d as x}
{list a as x}
{list a as x}
{list a as x}
{list a as x}
{if x_index>4}{break}{/if}
${fn2(x.publishTime,'yyyy-MM-dd HH:mm:ss')}
{list a as x}
{if !!(blogDetail.preBlogPermalink)}
{if !!(blogDetail.nextBlogPermalink)}
{list a as x}
{if defined('newslist')&&newslist.length>0}
{list newslist as x}
{if x_index>7}{break}{/if}
{list a as x}
{var first_option =}
{list x.voteDetailList as voteToOption}
{if voteToOption==1}
{if first_option==false},{/if}&&“${b[voteToOption_index]}”&&
{if (x.role!="-1") },“我是${c[x.role]}”&&{/if}
&&&&&&&&${fn1(x.voteTime)}
{if x.userName==''}{/if}
网易公司版权所有&&
{list x.l as y}
{if defined('wl')}
{list wl as x}{/list}I have a .dll I'm trying to embed as a resource inside an executable. The following two questions are somewhat helpful, but are not a full help:
This doesn't see the args.Name cannot be used as written, but even if it's fixed, the program still complains of a missing .dll, indicating that the assembly is not properly loaded.
and the link in one of the answers of:
However, there is no "App.xaml*" file in my project of any sort - I'm not using WPF; I'm using WinForms for my executable (changing isn't really an option, due to the nature of the executable).
I'm therefore looking for a complete set of instructions for embedding a class library in an executable as a resource and loading that .dll from the resource, without needing a .dll file outside of the embedded resource.
For example, would it be practical to simply add an "App.xaml" file to a WinForms project, or would there be negative interactions I'm not aware of?
Edit: This is what I'm currently using:
/// &summary&
/// Stores the very few things that need to be global.
/// &/summary&
static class AssemblyResolver
/// &summary&
/// Call in the static constructor of the startup class.
/// &/summary&
public static void Initialize( )
AppDomain.CurrentDomain.AssemblyResolve +=
new ResolveEventHandler( Resolver ) ;
/// &summary&
/// Use this to resolve assemblies.
/// &/summary&
/// &param name="sender"&&/param&
/// &param name="args"&&/param&
/// &returns&&/returns&
public static Assembly Resolver( object sender, ResolveEventArgs args )
Assembly executingAssembly = Assembly.GetExecutingAssembly( ) ;
if ( args.Name == null )
throw new NullReferenceException(
"Item name is null and could not be resolved."
if ( !executingAssembly.GetManifestResourceNames().Contains(
"Many_Objects_Display.Resources." +
new AssemblyName( args.Name ).Name.Replace( ".resources", ".dll" ) )
throw new ArgumentException( "Resource name does not exist." ) ;
Stream resourceStream =
executingAssembly.GetManifestResourceStream(
"Many_Objects_Display.Resources." +
new AssemblyName( args.Name ).Name.Replace( ".resources", ".dll" )
if ( resourceStream == null )
throw new NullReferenceException( "Resource stream is null." ) ;
if ( resourceStream.Length &
throw new ArgumentException(
"Exceedingly long resource - greater than 100 MB. Aborting..."
byte[] block = new byte[ resourceStream.Length ] ;
resourceStream.Read( block, 0, block.Length ) ;
Assembly resourceAssembly = Assembly.Load( block ) ;
if ( resourceAssembly == null )
throw new NullReferenceException( "Assembly is a null value." ) ;
return resourceA
解决方案 You need to put the code in your main entry point. Something like this:
class Program
static Program()
AppDomain.CurrentDomain.AssemblyResolve += new ResolveEventHandler(CurrentDomain_AssemblyResolve);
static void Main(string[] args)
// what was here is the same
static System.Reflection.Assembly CurrentDomain_AssemblyResolve(object sender, ResolveEventArgs args)
// the rest of sample code
You can't just add an App.xaml file to a windows forms application.
Also that sample code for CurrentDomain_AssemblyResolve is bizarre, I would try
code first. I haven't tested it but it looks more like the code I've used before.
本文地址: &
我有一个.dll我试图嵌入作为一个可执行内部的资源。下面的两个问题是有点用,但不是一个完整的帮助:
这似乎并不书面工作;不能使用args.Name作为编写的,但即使是固定的,程序仍抱怨缺少.dll文件,表明该组件未正确加载。
和在其中一个答案的链接:
的代码第一位。我没有测试它,但它看起来更像是我以前使用的代码。
本文地址: &
扫一扫关注官方微信在.Net平台实现嵌入DLL C#相关实例描述知识讲解
<span type="1" blog_id="1077137" userid='
分享到朋友圈
喜欢我的文章,请分享到朋友圈

我要回帖

更多关于 微信小程序嵌套h5 的文章

 

随机推荐