C# 从字节运行
我正在努力让我的客户端通过下载字节并使用反射打开另一个程序来打开它。我目前已在 C# 控制台应用程序上完成此操作,但当我尝试在 Windows 窗体应用程序上执行此操作时,出现此错误。
“调用目标已引发异常。”
这是代码
using System;
using System.IO;
using System.Net;
using System.Reflection;
using System.Text;
private void listBox1_DoubleClick(object sender, EventArgs e)
{
if (listBox1.SelectedItem.ToString() != null)
{
if (MessageBox.Show("Run " + listBox1.SelectedItem.ToString() + "?", "Run this program?", MessageBoxButtons.YesNo) == DialogResult.Yes)
{
byte[] bytes;
using (WebClient client = new WebClient())
{
bytes = client.DownloadData(new Uri("http://example.net/program.exe"));
}
RunFromBytes(bytes);
}
}
}
private static void RunFromBytes(byte[] bytes)
{
Assembly exeAssembly = Assembly.Load(bytes);
exeAssembly.EntryPoint.Invoke(null, null);
}
I am working on making my client open another program by downloading the bytes and using reflection to open it. I have currently got this working on a C# Console Application, but when I try and do it on a Windows Form Application I get this error.
"Exception has been thrown by the target of an invocation."
Here is the code
using System;
using System.IO;
using System.Net;
using System.Reflection;
using System.Text;
private void listBox1_DoubleClick(object sender, EventArgs e)
{
if (listBox1.SelectedItem.ToString() != null)
{
if (MessageBox.Show("Run " + listBox1.SelectedItem.ToString() + "?", "Run this program?", MessageBoxButtons.YesNo) == DialogResult.Yes)
{
byte[] bytes;
using (WebClient client = new WebClient())
{
bytes = client.DownloadData(new Uri("http://example.net/program.exe"));
}
RunFromBytes(bytes);
}
}
}
private static void RunFromBytes(byte[] bytes)
{
Assembly exeAssembly = Assembly.Load(bytes);
exeAssembly.EntryPoint.Invoke(null, null);
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
您必须执行以下操作:
ExecuteAssembly
执行它这是代码:
祝你好运!
You must do the following:
ExecuteAssembly
This is the code:
Good luck!
那是因为您正在尝试从另一个线程访问表单控件。
请参阅此处: http://www.yoda.arachsys.com/csharp/threads/ winforms.shtml
That's because you are trying to access your form controls from another thread.
see here: http://www.yoda.arachsys.com/csharp/threads/winforms.shtml
你可以这样做:
You can do like: