![](/img/trans.png)
[英]How to mark a file as an embedded resource in Visual Studio Code?
[英]Code or command to use embedded resource in Visual Studio
有人可以為我提供使用C#訪問嵌入式資源的起點或代碼嗎?
我已經成功嵌入了幾個批處理文件,腳本和CAD圖紙,我想運行批處理並將腳本和CAD文件復制到批處理文件中指定的位置。
我很難找到如何指定項目的內容並在EXE中設置路徑。 下面的代碼是我認為可行的,但它失敗了,我在網上找到的其他代碼都與XML文件有關。
System.Diagnostics.Process p = new System.Diagnostics.Process();
p.StartInfo.FileName = AppDomain.CurrentDomain.BaseDirectory + "\\Batchfile.bat";
p.Start();
老實說,我甚至不知道我是否正在尋找正確的方法,因為這是我第一次使用C#或Visual Studio。
Open Solution Explorer添加要嵌入的文件。 右鍵單擊文件,然后單擊“ Properties
。 在Properties
窗口中,將Build Action
更改為Embedded Resource
。
之后,您應該將嵌入的資源寫入文件,以便能夠運行它。
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.