简体   繁体   English

在Visual Studio中使用嵌入式资源的代码或命令

[英]Code or command to use embedded resource in Visual Studio

Can somebody provide me a starting point or code to access an embedded resource using C#? 有人可以为我提供使用C#访问嵌入式资源的起点或代码吗?

I have successfully embedded a couple of batch files, scripts and CAD drawings which I would like to run the batch and copy the scripts and CAD files to a location specified in the batch file. 我已经成功嵌入了几个批处理文件,脚本和CAD图纸,我想运行批处理并将脚本和CAD文件复制到批处理文件中指定的位置。

I'm struggling to find how to specify what the item is and set the path within the EXE. 我很难找到如何指定项目的内容并在EXE中设置路径。 The below code is what I thought would work, but it failed and the others I found online all related to XML files. 下面的代码是我认为可行的,但它失败了,我在网上找到的其他代码都与XML文件有关。

System.Diagnostics.Process p = new System.Diagnostics.Process();
p.StartInfo.FileName = AppDomain.CurrentDomain.BaseDirectory + "\\Batchfile.bat";
p.Start();

I honestly don't even know if I'm looking at the correct way to do this as this is my first time using either C# or Visual Studio. 老实说,我甚至不知道我是否正在寻找正确的方法,因为这是我第一次使用C#或Visual Studio。

Open Solution Explorer add files you want to embed. Open Solution Explorer添加要嵌入的文件。 Right click on the files then click on Properties . 右键单击文件,然后单击“ Properties In Properties window and change Build Action to Embedded Resource . Properties窗口中,将Build Action更改为Embedded Resource

嵌入资源Visual Studio

After that you should write the embedded resources to file in order to be able to run it. 之后,您应该将嵌入的资源写入文件,以便能够运行它。

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

namespace YourProject
{
    public class MyClass
    {
        // Other Code...

        private void StartProcessWithFile()
        {
            var assembly = Assembly.GetExecutingAssembly();
            //Getting names of all embedded resources
            var allResourceNames = assembly.GetManifestResourceNames();
            //Selecting first one. 
            var resourceName = allResourceNames[0];
            var pathToFile = Path.GetDirectoryName(AppDomain.CurrentDomain.BaseDirectory) +
                              resourceName;

            using (var stream = assembly.GetManifestResourceStream(resourceName))
            using (var fileStream = File.Create(pathToFile))
            {
                stream.Seek(0, SeekOrigin.Begin);
                stream.CopyTo(fileStream);
            }

            var process = new Process();
            process.StartInfo.FileName = pathToFile;
            process.Start();
        }
    }
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何在 Visual Studio Code 中将文件标记为嵌入资源? - How to mark a file as an embedded resource in Visual Studio Code? 是否可以通过代码中的名称引用Visual Studio中的嵌入式文件资源? - Can an embedded file resource in Visual Studio be referenced by name in code? 强制Visual Studio重建嵌入式资源 - Force Visual Studio Rebuild on Embedded Resource Changed 大文件作为Visual Studio 2012中的嵌入式资源 - Large file as embedded resource in Visual Studio 2012 C# App.config Visual Studio 中的嵌入式资源 - C# App.config Embedded Resource in Visual Studio 如何以编程方式使自动生成的文件成为Visual Studio中的嵌入式资源? - How to programmatically make an autogenerated file an embedded resource in visual studio? Visual Studio因使用“嵌入式资源”嵌入视图而锁定DLL - Visual studio locking DLL because of embedding views using “embedded resource” 如何在调试时让 Visual Studio 使用嵌入式源代码? - How can I get Visual Studio to use Embedded Source code whilst debugging? 如何在Visual Studio 2015中使用资源文件 - How to use a resource file in Visual Studio 2015 内置Visual Studio的Firebird - Firebird embedded with Visual Studio
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM