如何使用和执行资源

How to use & execute resources

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

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

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

I have seen resources examples but it looks like the lest 15 lines of code its should a little bit of crazy to right all these code for every single file I want merge with my main program(Like 15 for the 1 + 15 for the 2nd) If you know somehow another way to complete my wants please don't be afraid If not I am ready to accept the answer "Welcome to programming"

记住嵌入资源是一个可执行文件,没有图像或 .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);
        }
    }
}

当然有问题,但我不知道是什么
我想我无缘无故地留下了许多 void 方法

我已将最后一个替换为

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.

怎么可能找不到我点击了我的测试项目>属性>资源>添加资源并打开了文件

http://prntscr.com/6nv4ki

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

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

这将适用于属性设置为 Build Action -> Embedded Resource

的包含文件
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);
    }