埋め込んだファイルをStreamとして取り出す
はじめに
.NETで一時的なHTTPサーバーを立ち上げて、index.htmlだけを返す必要がありました。
単一のexeで完結させたいので、下記2点を実現したいと考えています。
- index.htmlをexeに埋め込む
- 埋め込んだリソースをStreamとして取り出す
index.htmlをexeに埋め込む
index.htmlのプロパティを開き、ビルドアクションを埋め込みリソースとして設定します。
埋め込んだリソースをStreamとして取り出す
GetManifestResourceStream
を使うことで、Streamとして埋め込みリソースを取り出せます。
GetManifestResourceStream
の引数には、プロジェクト名.[フォルダ階層.]ファイル名
を指定します。
Program.cs
var context = await http.GetContextAsync();
context.Response.ContentType = "text/html";
var assembly = Assembly.GetExecutingAssembly();
using (var file = assembly.GetManifestResourceStream("WindowsFormsApp1.index.html"))
{
file.CopyTo(context.Response.OutputStream);
}
Author And Source
この問題について(埋め込んだファイルをStreamとして取り出す), 我々は、より多くの情報をここで見つけました https://qiita.com/basio/items/8f4287ac8e685c7874a1著者帰属:元の著者の情報は、元のURLに含まれています。著作権は原作者に属する。
Content is automatically searched and collected through network algorithms . If there is a violation . Please contact us . We will adjust (correct author information ,or delete content ) as soon as possible .