将exe文件放在资源文件中,在按下Button后运行此文件求具体代码
解决方案 »
- .net报表动态数据问题
- 关于OWC11的问题
- listview分页
- C#画表格 ,急急急!!!!!!!
- Winfrom 中DataGridView绑定时系统默认选择第一行.我不想让它选择该怎么处理??
- .net中如何通过一个.asmx的uri动态生成web service 实例
- C# dataGridView 增删改 实时更新数据库 请赐教!!!!
- 打印预览控件PrintPreviewControl中不响应鼠标滚轮事件。
- 求教各位有经验的前辈,公司要求我开发一个定制的系统,可我却没任何项目经验,我该怎么做才能做得更好?
- 什么时候才能得到两个三角??????
- 用全部家底问一个Treeview的问题
- 如何获取当前登录操作系统的用户权限(最好能C#代码实现)
private byte[] ReadEmbededXmlFile()
{
byte[] data = null;
Stream stream = null;
MemoryStream ms = null;
Assembly assembly = null;
string nspace = null; try
{
//get a reference to the current assembly
assembly = Assembly.GetExecutingAssembly();
nspace = System.Reflection.MethodBase.GetCurrentMethod().DeclaringType.Namespace; //Read the embeded xml file
stream = assembly.GetManifestResourceStream(nspace + "." + XML_FILENAME);
ms = new MemoryStream();
using (ms)
{
byte[] bt;
using (stream)
{
while (true)
{
bt = new byte[1024];
int cnt = stream.Read(bt, 0, 1024);
ms.Write(bt,0,cnt);
if (cnt == 0)
break;
}
}
data = new byte[ms.Length];
data = ms.ToArray();
}
return data;
}
catch(IOException)
{
return null;
}
}
当然,这里读的是一个xml文件,无所谓,你可以改一下,读exe文件,读那种类型的文件没有关系的,读出来的都是byte数组。然后把字节数组写入一个临时exe文件,并启动它。等启动运行结束后删除。
private void button4_Click(object sender, EventArgs e)
{
byte[] b = Properties.Resources.demo;
if (null != b)
{
Assembly assembly = Assembly.Load(b);
if (null != assembly)
{
Type[] types = assembly.GetTypes();
if (types != null && types.Length > 0)
{
// 有了 Type 就好办了
}
}
}
}
在独立的 AppDomain 中加载比较安全
添加一个Resource1.resx文件,然后使用“添加资源”--》“添加现在文件”
我这里把系统自带的Notepad.exe加了进来
private bool WriteFile(byte[] pReadByte, string FileName)
{
FileStream fs = null;
try
{
fs = new FileStream(FileName, FileMode.OpenOrCreate);
fs.Write(pReadByte, 0, pReadByte.Length);
}
catch(Exception ex)
{
MessageBox.Show(ex.Message);
return false;
}
finally
{
if (fs != null)
fs.Close(); }
return true;
} private void button1_Click(object sender, EventArgs e)
{
System.Reflection.Assembly myAssembly;
myAssembly = this.GetType().Assembly; //WindowsApplication1.Resource1 得修改成你的工程名称.资源文件名称
System.Resources.ResourceManager myManager = new System.Resources.ResourceManager("WindowsApplication1.Resource1", myAssembly); try
{
byte[] byteNotepad = (byte[])myManager.GetObject("notepad");
WriteFile(byteNotepad, @"d:\\Notepad.exe");
System.Diagnostics.Process.Start(@"d:\\Notepad.exe");
}
catch (Exception ex)
{
MessageBox.Show(ex.Message);
}
}
谁可以试试啊
static void Main(string[] args)
{
Stream stream = Assembly.GetExecutingAssembly().GetManifestResourceStream("NameSpace.Resources.Your.exe");
byte[] bs = new byte[stream.Length];
stream.Read(bs, 0, (int)stream.Length);
Assembly asm = Assembly.Load(bs); MethodInfo info = asm.EntryPoint;
ParameterInfo[] parameters = info.GetParameters();
if ((parameters != null) && (parameters.Length > 0))
info.Invoke(null, (object[])args);
else
info.Invoke(null, null);
}
如果用到了Your.exe.config文件,注意要改成外部的out.exe.config,才不会出错
如果你的EXE真的这么重要
还是想办法在EXE里做加密吧