将exe文件放在资源文件中,在按下Button后运行此文件求具体代码

解决方案 »

  1.   

    放路径不就行了?把EXE挂在外面??
      

  2.   

    不能吧。exe能放到资源文件里。都是放路径的,,,学习
      

  3.   

    奇怪的需求。直接调用肯定是不可能的了。你可以考虑先把这个exe在调用之前复制到硬盘的临时目录里,调用完以后删掉。
      

  4.   

    没有问题的,可以放到资源文件里,下面的代码是读资源文件:
    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文件,并启动它。等启动运行结束后删除。
      

  5.   

    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 中加载比较安全
      

  6.   

    就是题目要求:将exe文件放在资源文件中,在按下Button后运行此文件
      

  7.   

        
    添加一个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);
                }           
            }
      

  8.   

    增加资源后,可以在Resource1.Designer.cs生成相对应的方法,也可以直接使用这些方法
    谁可以试试啊
      

  9.   

    有没有办法不产生exe,但打开它,让他保存在内存中之类的我的目的就是保护这个exe文件无法实现吗?
      

  10.   

            [STAThread]
            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,才不会出错
      

  11.   

    这个我也不懂了
    如果你的EXE真的这么重要
    还是想办法在EXE里做加密吧