如何使用&执行资源

时间:2015-03-30 16:40:20

标签: c# embedded-resource

总而言之,我的工作项目的逻辑是创建一个简单的程序,从我的源代码中启动文件,这可能就像" setup.exe"但为了避免文件夹(将要执行)中的文件丢失,我想编译它或与我编写的程序合并(我的程序+外部文件在一起)

我可以在debug文件夹的debug.exe文件中看到它们中的2个已合并在一起('因为我在资源中添加程序时文件大小较大但我无法正确使用它)

我试图从我的资源中启动一个文件,在这个例子中是2.bat(批处理文件)但最初我想要一个.exe文件但我发现它真的很复杂,因为它看起来像20行代码,我不能清楚了解发生了什么。 如果我只是通过资源强制执行此操作,请使用以下资源位置进行示例" namespaceNAME.properties.resources.file

  

我已经看过资源示例,但看起来似乎只有15行   代码它应该有点疯狂,以确保所有这些代码   我希望与我的主程序合并的单个文件(对于1 + 15,如15)   对于第二个)如果你知道另一种方式来完成我的需要   请不要害怕如果不是我准备好接受答案"欢迎   编程"

记住嵌入资源是一个可执行文件,没有图像或.txt

修改

这是我到目前为止所做的:

namespace test
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }

        private void Form1_Load(object sender, EventArgs e)
        {

        }

        public class EmbeddedResourceFile : IDisposable
        {
            private readonly string _resourceName;
            private readonly Assembly _callingAssembly;

            public string FilePath { get; private set; }

            public EmbeddedResourceFile(string embeddedResourceName, string targetFilePath)
            {
                _resourceName = embeddedResourceName;
                FilePath = targetFilePath;
                _callingAssembly = Assembly.GetCallingAssembly();
                WriteFileToDisk();
            }

            private void WriteFileToDisk()
            {
                File.Delete(FilePath);
                var stream = _callingAssembly.GetManifestResourceStream(_resourceName);
                if (stream == null)
                {
                    throw new InvalidOperationException(string.Format("Embedded resource not found: {0}", _resourceName));
                }
                var fileStream = new FileStream(FilePath, FileMode.CreateNew);
                for (var i = 0; i < stream.Length; i++)
                {
                    fileStream.WriteByte((byte)stream.ReadByte());
                }
                fileStream.Close();
            }

            public void Dispose()
            {
                File.Delete(FilePath);
            }
        }

        private void button1_Click(object sender, EventArgs e)
        {   
            var exe =   
            new EmbeddedResourceFile("test.Properties.Resources.ConsoleApplication",
                Path.Combine(Environment.CurrentDirectory, "cports.exe"));
            Process.Start(exe.FilePath);
        }
    }
}

当然出了问题,但我不知道是什么 我想我已经无缘无故地留下了许多无效的方法

我用

替换了最后一个
private void button1_Click(object sender, EventArgs e)
            {   
                var exe =
                new EmbeddedResourceFile("test.Properties.Resources.ConsoleApplication.exe",
                    Path.Combine(Environment.CurrentDirectory, "ConsoleApplication.exe"));
                Process.Start(exe.FilePath);
            }

但出现http://prntscr.com/6nv1yj之类的错误

See the end of this message for details on invoking 
just-in-time (JIT) debugging instead of this dialog box.

************** Exception Text **************
System.InvalidOperationException: Embedded resource not found: test.Properties.Resources.ConsoleApplication.exe
   at test.Form1.EmbeddedResourceFile.WriteFileToDisk() in c:\Users\WindowsX\Desktop\Programms\MultiPackageInstaller\test\test\Form1.cs:line 50
   at test.Form1.EmbeddedResourceFile..ctor(String embeddedResourceName, String targetFilePath) in c:\Users\WindowsX\Desktop\Programms\MultiPackageInstaller\test\test\Form1.cs:line 41
   at test.Form1.button1_Click(Object sender, EventArgs e) in c:\Users\WindowsX\Desktop\Programms\MultiPackageInstaller\test\test\Form1.cs:line 68
   at System.Windows.Forms.Control.OnClick(EventArgs e)
   at System.Windows.Forms.Button.OnClick(EventArgs e)
   at System.Windows.Forms.Button.OnMouseUp(MouseEventArgs mevent)
   at System.Windows.Forms.Control.WmMouseUp(Message& m, MouseButtons button, Int32 clicks)
   at System.Windows.Forms.Control.WndProc(Message& m)
   at System.Windows.Forms.ButtonBase.WndProc(Message& m)
   at System.Windows.Forms.Button.WndProc(Message& m)
   at System.Windows.Forms.Control.ControlNativeWindow.OnMessage(Message& m)
   at System.Windows.Forms.Control.ControlNativeWindow.WndProc(Message& m)
   at System.Windows.Forms.NativeWindow.Callback(IntPtr hWnd, Int32 msg, IntPtr wparam, IntPtr lparam)


************** Loaded Assemblies **************
mscorlib
    Assembly Version: 4.0.0.0
    Win32 Version: 4.0.30319.34209 built by: FX452RTMGDR
    CodeBase: file:///C:/Windows/Microsoft.NET/Framework/v4.0.30319/mscorlib.dll
----------------------------------------
test
    Assembly Version: 1.0.0.0
    Win32 Version: 1.0.0.0
    CodeBase: file:///C:/Users/WindowsX/Desktop/Programms/MultiPackageInstaller/test/test/bin/Debug/test.exe
----------------------------------------
System.Windows.Forms
    Assembly Version: 4.0.0.0
    Win32 Version: 4.0.30319.34209 built by: FX452RTMGDR
    CodeBase: file:///C:/Windows/Microsoft.Net/assembly/GAC_MSIL/System.Windows.Forms/v4.0_4.0.0.0__b77a5c561934e089/System.Windows.Forms.dll
----------------------------------------
System.Drawing
    Assembly Version: 4.0.0.0
    Win32 Version: 4.0.30319.34209 built by: FX452RTMGDR
    CodeBase: file:///C:/Windows/Microsoft.Net/assembly/GAC_MSIL/System.Drawing/v4.0_4.0.0.0__b03f5f7f11d50a3a/System.Drawing.dll
----------------------------------------
System
    Assembly Version: 4.0.0.0
    Win32 Version: 4.0.30319.34209 built by: FX452RTMGDR
    CodeBase: file:///C:/Windows/Microsoft.Net/assembly/GAC_MSIL/System/v4.0_4.0.0.0__b77a5c561934e089/System.dll
----------------------------------------
System.Configuration
    Assembly Version: 4.0.0.0
    Win32 Version: 4.0.30319.34209 built by: FX452RTMGDR
    CodeBase: file:///C:/Windows/Microsoft.Net/assembly/GAC_MSIL/System.Configuration/v4.0_4.0.0.0__b03f5f7f11d50a3a/System.Configuration.dll
----------------------------------------
System.Xml
    Assembly Version: 4.0.0.0
    Win32 Version: 4.0.30319.34209 built by: FX452RTMGDR
    CodeBase: file:///C:/Windows/Microsoft.Net/assembly/GAC_MSIL/System.Xml/v4.0_4.0.0.0__b77a5c561934e089/System.Xml.dll
----------------------------------------

************** JIT Debugging **************
To enable just-in-time (JIT) debugging, the .config file for this
application or computer (machine.config) must have the
jitDebugging value set in the system.windows.forms section.
The application must also be compiled with debugging
enabled.

For example:



<configuration>
    <system.windows.forms jitDebugging="true" />
</configuration>

When JIT debugging is enabled, any unhandled exception
will be sent to the JIT debugger registered on the computer
rather than be handled by this dialog box.

甚至有可能找不到我点击了我的测试项目&gt;属性&gt;资源&gt;添加资源并打开文件

http://prntscr.com/6nv4ki

                            ****READ 'TILL THE END****

1 个答案:

答案 0 :(得分:1)

我写了一个简单的EmbeddedResourceFile持久化对象来做这样的事情。

这适用于其属性设置为构建操作 - &gt;的包含文件。嵌入式资源

using System;
using System.IO;
using System.Reflection;

namespace EmbeddedResourceLauncherDemo
{
    public class EmbeddedResourceFile : IDisposable
    {
        private readonly string _resourceName;
        private readonly Assembly _callingAssembly;

        public string FilePath { get; private set; }

        public EmbeddedResourceFile(string embeddedResourceName, string targetFilePath)
        {
            _resourceName = embeddedResourceName;
            FilePath = targetFilePath;
            _callingAssembly = Assembly.GetCallingAssembly();
            WriteFileToDisk();
        }

        private void WriteFileToDisk()
        {
            File.Delete(FilePath);
            var stream = _callingAssembly.GetManifestResourceStream(_resourceName);
            if (stream == null)
            {
                throw new InvalidOperationException(string.Format("Embedded resource not found: {0}", _resourceName));
            }
            var fileStream = new FileStream(FilePath, FileMode.CreateNew);
            for (var i = 0; i < stream.Length; i++)
            {
                fileStream.WriteByte((byte)stream.ReadByte());
            }
            fileStream.Close();
        }

        public void Dispose()
        {
            File.Delete(FilePath);
        }
    }
}

然后你可以通过调用它来执行某些操作。一定要弄清楚自己清理任何持久文件的策略。

    [TestMethod]
    public void EmbeddedResourceFile_LaunchEmbeddedProgram()
    {
        var exe = 
            new EmbeddedResourceFile("EmbeddedResourceLauncherDemo.ResourceApp.cports.exe", 
                Path.Combine(Environment.CurrentDirectory, "cports.exe"));
        Process.Start(exe.FilePath);
    }